Unverified Commit e7c12347 authored by Björn Grüning's avatar Björn Grüning Committed by GitHub
Browse files

Merge pull request #12697 from davelopez/tags_validation

Add Tag validation in API requests
parents 64329459 7f2407b9
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
from enum import Enum
from typing import (
    List,
    Optional,
)

@@ -13,6 +12,7 @@ from galaxy.managers.context import ProvidesUserContext
from galaxy.model import ItemTagAssociation
from galaxy.model.tags import GalaxyTagHandlerSession
from galaxy.schema.fields import EncodedDatabaseIdField
from galaxy.schema.schema import TagCollection

taggable_item_names = {item: item for item in ItemTagAssociation.associated_item_names}
# This Enum is generated dynamically and mypy can not statically infer it's real type
@@ -31,7 +31,7 @@ class ItemTagsPayload(BaseModel):
        title="Item class",
        description="The name of the class of the item that will be tagged.",
    )
    item_tags: Optional[List[str]] = Field(
    item_tags: Optional[TagCollection] = Field(
        default=None,
        title="Item tags",
        description="The list of tags that will replace the current tags associated with the item.",
@@ -48,8 +48,8 @@ class TagsManager:
        """Apply a new set of tags to an item; previous tags are deleted."""
        tag_handler = GalaxyTagHandlerSession(trans.sa_session)
        new_tags: Optional[str] = None
        if payload.item_tags and len(payload.item_tags) > 0:
            new_tags = ",".join(payload.item_tags)
        if payload.item_tags and len(payload.item_tags.__root__) > 0:
            new_tags = ",".join(payload.item_tags.__root__)
        item = self._get_item(trans, payload)
        user = trans.user
        tag_handler.delete_item_tags(user, item)
+7 −1
Original line number Diff line number Diff line
"""This module contains general pydantic models and common schema field annotations for them."""

import re
from datetime import datetime
from enum import Enum
from typing import (
@@ -15,6 +16,7 @@ from pydantic import (
    AnyHttpUrl,
    AnyUrl,
    BaseModel,
    ConstrainedStr,
    Extra,
    Field,
    FilePath,
@@ -241,9 +243,13 @@ class HistoryContentSource(str, Enum):
    new_collection = "new_collection"


class TagItem(ConstrainedStr):
    regex = re.compile(r"^([^\s.:])+(.[^\s.:]+)*(:[^\s.:]+)?$")


class TagCollection(Model):
    """Represents the collection of tags associated with an item."""
    __root__: List[str] = Field(
    __root__: List[TagItem] = Field(
        default=...,
        title="Tags",
        description="The collection of tags associated with an item.",