Skip to content

Commit

Permalink
Move is_valid_slug function into SlugBuilder class as classmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
arash77 committed Oct 24, 2024
1 parent 42b34a2 commit 0129b7c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
6 changes: 0 additions & 6 deletions lib/galaxy/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""

Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/managers/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
7 changes: 6 additions & 1 deletion lib/galaxy/managers/sharable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/webapps/base/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 2 additions & 5 deletions lib/galaxy/webapps/galaxy/services/visualizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
)
Expand Down

0 comments on commit 0129b7c

Please sign in to comment.