Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resetting project anatomy during project update #87

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 14 additions & 5 deletions server/kitsu/anatomy.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import contextlib
from typing import TYPE_CHECKING, Any

from nxtools import logging

from ayon_server.entities import ProjectEntity
from ayon_server.exceptions import AyonException
from ayon_server.lib.postgres import Postgres
from ayon_server.settings.anatomy import Anatomy
from ayon_server.settings.anatomy.statuses import Status
from ayon_server.settings.anatomy.task_types import TaskType

from .addon_helpers import create_short_name, remove_accents
from .extract_ayon_project_anatomy import extract_ayon_project_anatomy

if TYPE_CHECKING:
from .. import KitsuAddon


async def parse_task_types(
addon: "KitsuAddon", kitsu_project_id: str
) -> list[TaskType]:
Expand Down Expand Up @@ -199,6 +202,7 @@ async def get_primary_anatomy_preset() -> Anatomy:
async def get_kitsu_project_anatomy(
addon: "KitsuAddon",
kitsu_project_id: str,
ayon_project: ProjectEntity | None = None,
) -> Anatomy:
kitsu_project_response = await addon.kitsu.get(
f"data/projects/{kitsu_project_id}"
Expand All @@ -212,13 +216,18 @@ async def get_kitsu_project_anatomy(
statuses = await parse_statuses(addon, kitsu_project_id)
task_types = await parse_task_types(addon, kitsu_project_id)

anatomy_preset = await get_primary_anatomy_preset()
anatomy_dict = anatomy_preset.dict()
if ayon_project:
anatomy = extract_ayon_project_anatomy(ayon_project)
else:
anatomy = await get_primary_anatomy_preset()

anatomy_dict = anatomy.dict()
for key in anatomy_dict["attributes"]:
if key in attributes:
anatomy_dict["attributes"][key]=attributes[key]
anatomy_dict["attributes"][key] = attributes[key]
logging.debug("updated project", ayon_project.name, "anatomy attribute", key, "to", attributes[key])


#anatomy_dict["attributes"] = attributes
anatomy_dict["statuses"] = statuses
anatomy_dict["task_types"] = task_types

Expand Down
58 changes: 58 additions & 0 deletions server/kitsu/extract_ayon_project_anatomy.py
martastain marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from typing import Any
from ayon_server.entities import ProjectEntity
from ayon_server.entities.models.submodels import LinkTypeModel
from ayon_server.settings.anatomy import Anatomy


def dict2list(src) -> list[dict[str, Any]]:
return [{"name": k, "original_name": k, **v} for k, v in src.items()]


def process_aux_table(src: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""Process auxiliary table."""
result = []
for data in src:
result.append({**data, "original_name": data["name"]})
return result


def process_link_types(src: list[LinkTypeModel]) -> list[dict[str, Any]]:
"""Convert project linktypes submodel to anatomy-style linktypes."""
result = []
for ltdata in src:
row = {
"link_type": ltdata.link_type,
"input_type": ltdata.input_type,
"output_type": ltdata.output_type,
}
for key in ["color", "style"]:
if value := ltdata.data.get(key):
row[key] = value
result.append(row)
return result


def extract_ayon_project_anatomy(project: ProjectEntity) -> Anatomy:
"""Extract Anatomy object from ayon ProjectEntity."""

templates = project.config.get("templates", {}).get("common", {})
for template_group, template_group_def in project.config.get(
"templates", {}
).items():
if template_group == "common":
continue
templates[template_group] = dict2list(template_group_def)

result = {
"templates": templates,
"roots": dict2list(project.config.get("roots", {})),
"folder_types": process_aux_table(project.folder_types),
"task_types": process_aux_table(project.task_types),
"link_types": process_link_types(project.link_types),
"statuses": process_aux_table(project.statuses),
"tags": process_aux_table(project.tags),
"attributes": project.attrib,
}

return Anatomy(**result)

2 changes: 1 addition & 1 deletion server/kitsu/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ async def sync_project(
return

await addon.ensure_kitsu(mock)
anatomy = await get_kitsu_project_anatomy(addon, entity_id)
anatomy = await get_kitsu_project_anatomy(addon, entity_id, project)
anatomy_data = anatomy_to_project_data(anatomy)

await update_project(project.name, **anatomy_data)
Expand Down
Loading