Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions ayon_server/enum/resolvers/enum_anatomy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any
from typing import Any, Literal
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed

from ayon_server.enum.base_resolver import BaseEnumResolver
from ayon_server.enum.enum_item import EnumItem
from ayon_server.enum.enum_registry import EnumRegistry
from ayon_server.helpers.project_list import normalize_project_name
from ayon_server.lib.postgres import Postgres
from ayon_server.lib.redis import Redis
Expand Down Expand Up @@ -155,10 +156,33 @@ class StatusesEnumResolver(BaseEnumResolver):
name = "statuses"

async def get_accepted_params(self) -> dict[str, type]:
return {"project_name": str}
return {
"project_name": str,
"entity_type": Literal["folder", "task", "product", "version"],
}

@classmethod
def for_type(
cls, entity_type: Literal["folder", "task", "product", "version"]
):
"""Helper method to easily get resolver for an entity type.

Goal is to be able to use
'enum_resolver=StatusesEnumResolver.for_type("task")'.

"""
async def resolve(project_name: str | None = None) -> list[EnumItem]:
return await EnumRegistry.resolve(
"statuses",
project_name=project_name,
entity_type=entity_type,
)

return resolve

async def resolve(self, context: dict[str, Any]) -> list[EnumItem]:
project_name = context.get("project_name")
entity_type = context.get("entity_type")
if not project_name:
anatomy = await get_primary_anatomy_preset()
return [
Expand All @@ -170,6 +194,10 @@ async def resolve(self, context: dict[str, Any]) -> list[EnumItem]:
short_name=status.shortName,
)
for status in anatomy.statuses
if (
not entity_type
or entity_type in status.scope
)
]

project_name = await normalize_project_name(project_name)
Expand All @@ -180,6 +208,11 @@ async def resolve(self, context: dict[str, Any]) -> list[EnumItem]:
"SELECT name, data FROM statuses ORDER BY position"
)
async for row in stmt.cursor():
if entity_type:
scope = row["data"].get("scope")
if not scope or entity_type not in scope:
continue

result.append(
EnumItem(
value=row["name"],
Expand All @@ -195,11 +228,21 @@ async def create_item(
self,
item: EnumItem,
project_name: str | None = None,
entity_type: str | None = None,
**kwargs,
) -> None:
if not project_name:
raise ValueError("Missing project name in item data")

data = {
"icon": item.icon or "check_circle",
"color": item.color or "#808080",
"name": item.value,
"shortName": item.short_name or str(item.value)[0:3].upper(),
}
if entity_type:
data["scope"] = [entity_type]

project_name = await normalize_project_name(project_name)
async with Postgres.transaction():
await Postgres.set_project_schema(project_name)
Expand All @@ -209,12 +252,7 @@ async def create_item(
VALUES ($1, $2, (SELECT COALESCE(MAX(position), 0) + 1 FROM statuses))
""",
item.value,
{
"icon": item.icon or "check_circle",
"color": item.color or "#808080",
"name": item.value,
"shortName": item.short_name or str(item.value)[0:3].upper(),
},
data,
)
await Redis.delete("project-anatomy", project_name)
await Redis.delete("project-data", project_name)
Expand Down