implemented helper functions to get tags from project

This commit is contained in:
Jakub Trllo 2025-06-11 15:28:01 +02:00
parent ff2645e335
commit 93b5ea5c31
5 changed files with 99 additions and 1 deletions

View file

@ -2,6 +2,7 @@
from .cache import CacheItem, NestedCacheItem
from .projects import (
TagItem,
StatusItem,
StatusStates,
ProjectItem,
@ -25,6 +26,7 @@ __all__ = (
"CacheItem",
"NestedCacheItem",
"TagItem",
"StatusItem",
"StatusStates",
"ProjectItem",

View file

@ -217,6 +217,8 @@ class HierarchyModel(object):
lifetime = 60 # A minute
def __init__(self, controller):
self._tags_by_entity_type = NestedCacheItem(
levels=1, default_factory=dict, lifetime=self.lifetime)
self._folders_items = NestedCacheItem(
levels=1, default_factory=dict, lifetime=self.lifetime)
self._folders_by_id = NestedCacheItem(
@ -235,6 +237,7 @@ class HierarchyModel(object):
self._controller = controller
def reset(self):
self._tags_by_entity_type.reset()
self._folders_items.reset()
self._folders_by_id.reset()
@ -514,6 +517,31 @@ class HierarchyModel(object):
return output
def get_available_tags_by_entity_type(
self, project_name: str
) -> dict[str, list[str]]:
"""Get available tags for all entity types in a project."""
cache = self._tags_by_entity_type.get(project_name)
if not cache.is_valid:
tags = None
if project_name:
response = ayon_api.get(f"projects/{project_name}/tags")
if response.status_code == 200:
tags = response.data
# Fake empty tags
if tags is None:
tags = {
"folders": [],
"tasks": [],
"products": [],
"versions": [],
"representations": [],
"workfiles": []
}
cache.update_data(tags)
return cache.get_data()
@contextlib.contextmanager
def _folder_refresh_event_manager(self, project_name, sender):
self._folders_refreshing.add(project_name)

View file

@ -1,6 +1,9 @@
from __future__ import annotations
import contextlib
from abc import ABC, abstractmethod
from typing import Dict, Any
from dataclasses import dataclass
import ayon_api
@ -72,6 +75,14 @@ class StatusItem:
)
@dataclass
class TagItem:
"""Tag definition set on project anatomy."""
name: str
color: str
class FolderTypeItem:
"""Item representing folder type of project.
@ -288,6 +299,22 @@ class ProjectsModel(object):
project_cache.update_data(entity)
return project_cache.get_data()
def get_project_anatomy_tags(self, project_name: str) -> list[TagItem]:
"""Get project anatomy tags.
Args:
project_name (str): Project name.
Returns:
list[TagItem]: Tag definitions.
"""
project_entity = self.get_project_entity(project_name)
return [
TagItem(tag["name"], tag["color"])
for tag in project_entity["tags"]
]
def get_project_status_items(self, project_name, sender):
"""Get project status items.

View file

@ -1,3 +1,4 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import List
@ -6,6 +7,7 @@ from ayon_core.lib.attribute_definitions import (
serialize_attr_defs,
deserialize_attr_defs,
)
from ayon_core.tools.common_models import TagItem
class ProductTypeItem:
@ -517,8 +519,21 @@ class FrontendLoaderController(_BaseLoaderController):
Returns:
list[ProjectItem]: List of project items.
"""
"""
pass
@abstractmethod
def get_project_anatomy_tags(self, project_name: str) -> list[TagItem]:
"""Tag items defined on project anatomy.
Args:
project_name (str): Project name.
Returns:
list[TagItem]: Tag definition items.
"""
pass
@abstractmethod
@ -590,6 +605,21 @@ class FrontendLoaderController(_BaseLoaderController):
"""
pass
@abstractmethod
def get_available_tags_by_entity_type(
self, project_name: str
) -> dict[str, list[str]]:
"""Get available tags by entity type.
Args:
project_name (str): Project name.
Returns:
dict[str, list[str]]: Available tags by entity type.
"""
pass
@abstractmethod
def get_project_status_items(self, project_name, sender=None):
"""Items for all projects available on server.

View file

@ -13,6 +13,7 @@ from ayon_core.tools.common_models import (
ProjectsModel,
HierarchyModel,
ThumbnailsModel,
TagItem,
)
from .abstract import (
@ -223,6 +224,16 @@ class LoaderController(BackendLoaderController, FrontendLoaderController):
output[folder_id] = label
return output
def get_available_tags_by_entity_type(
self, project_name: str
) -> dict[str, list[str]]:
return self._hierarchy_model.get_available_tags_by_entity_type(
project_name
)
def get_project_anatomy_tags(self, project_name: str) -> list[TagItem]:
return self._projects_model.get_project_anatomy_tags(project_name)
def get_product_items(self, project_name, folder_ids, sender=None):
return self._products_model.get_product_items(
project_name, folder_ids, sender)