From 0129b7c5fee3352a45ee87c08a6d89f1281a7485 Mon Sep 17 00:00:00 2001 From: Arash Date: Thu, 24 Oct 2024 14:09:15 +0200 Subject: [PATCH] Move is_valid_slug function into SlugBuilder class as classmethod --- lib/galaxy/managers/base.py | 6 ------ lib/galaxy/managers/pages.py | 2 +- lib/galaxy/managers/sharable.py | 7 ++++++- lib/galaxy/webapps/base/controller.py | 2 +- lib/galaxy/webapps/galaxy/services/visualizations.py | 7 ++----- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/galaxy/managers/base.py b/lib/galaxy/managers/base.py index c5ea85ab5e94..135616fbed5a 100644 --- a/lib/galaxy/managers/base.py +++ b/lib/galaxy/managers/base.py @@ -46,7 +46,6 @@ ) import sqlalchemy -from slugify import slugify from sqlalchemy.orm import Query from sqlalchemy.orm.scoping import scoped_session from typing_extensions import Protocol @@ -1295,11 +1294,6 @@ def raise_filter_err(attr, op, val, msg): raise exceptions.RequestParameterInvalidException(msg, column=attr, operation=op, val=val) -def is_valid_slug(slug): - """Returns true if slug is valid.""" - return slugify(slug, allow_unicode=True) == slug - - class SortableManager: """A manager interface for parsing order_by strings into actual 'order by' queries.""" diff --git a/lib/galaxy/managers/pages.py b/lib/galaxy/managers/pages.py index 581b5b93562b..101d785c688e 100644 --- a/lib/galaxy/managers/pages.py +++ b/lib/galaxy/managers/pages.py @@ -248,7 +248,7 @@ def create_page(self, trans, payload: CreatePagePayload): raise exceptions.ObjectAttributeMissingException("Page name is required") elif not payload.slug: raise exceptions.ObjectAttributeMissingException("Page id is required") - elif not base.is_valid_slug(payload.slug): + elif not sharable.SlugBuilder.is_valid_slug(payload.slug): raise exceptions.ObjectAttributeInvalidException( "Page identifier must consist of only lowercase letters, numbers, and the '-' character" ) diff --git a/lib/galaxy/managers/sharable.py b/lib/galaxy/managers/sharable.py index 04e00357f518..cfdcda372e97 100644 --- a/lib/galaxy/managers/sharable.py +++ b/lib/galaxy/managers/sharable.py @@ -291,7 +291,7 @@ def set_slug(self, item, new_slug, user, flush=True): Validate and set the new slug for `item`. """ # precondition: has been validated - if not base.is_valid_slug(new_slug): + if not SlugBuilder.is_valid_slug(new_slug): raise exceptions.RequestParameterInvalidException("Invalid slug", slug=new_slug) if item.slug == new_slug: @@ -563,6 +563,11 @@ def create_item_slug(self, sa_session, item) -> bool: item.slug = new_slug return item.slug == cur_slug + @classmethod + def is_valid_slug(self, slug): + """Returns true if slug is valid.""" + return slugify(slug, allow_unicode=True) == slug + def slug_exists(session, model_class, user, slug, ignore_deleted=False): stmt = select(exists().where(model_class.user == user).where(model_class.slug == slug)) diff --git a/lib/galaxy/webapps/base/controller.py b/lib/galaxy/webapps/base/controller.py index 1b518b897adf..6dc7c9702d53 100644 --- a/lib/galaxy/webapps/base/controller.py +++ b/lib/galaxy/webapps/base/controller.py @@ -1090,7 +1090,7 @@ class SharableMixin: def _is_valid_slug(self, slug): """Returns true if slug is valid.""" - return managers_base.is_valid_slug(slug) + return SlugBuilder.is_valid_slug(slug) @web.expose @web.require_login("modify Galaxy items") diff --git a/lib/galaxy/webapps/galaxy/services/visualizations.py b/lib/galaxy/webapps/galaxy/services/visualizations.py index 220db64d20dd..d2b7e6819519 100644 --- a/lib/galaxy/webapps/galaxy/services/visualizations.py +++ b/lib/galaxy/webapps/galaxy/services/visualizations.py @@ -8,10 +8,7 @@ ) from galaxy import exceptions -from galaxy.managers.base import ( - is_valid_slug, - security_check, -) +from galaxy.managers.base import security_check from galaxy.managers.context import ProvidesUserContext from galaxy.managers.sharable import ( slug_exists, @@ -302,7 +299,7 @@ def _create_visualization( # Error checking. if slug: slug_err = "" - if not is_valid_slug(slug): + if not SlugBuilder.is_valid_slug(slug): slug_err = ( "visualization identifier must consist of only lowercase letters, numbers, and the '-' character" )