Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

[QUAD] Kitsu Sync: Properly synchronize project state between OP mongo and Kitsu #6322

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
69 changes: 57 additions & 12 deletions openpype/modules/kitsu/utils/update_op_with_zou.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
# Accepted namin pattern for OP
naming_pattern = re.compile("^[a-zA-Z0-9_.]*$")

KitsuStateToBool = {
'Closed': False,
'Open': True
}


def create_op_asset(gazu_entity: dict) -> dict:
"""Create OP asset dict from gazu entity.
Expand Down Expand Up @@ -327,7 +332,7 @@ def write_project_to_op(project: dict, dbcon: AvalonMongoDB) -> UpdateOne:
"code": project_code,
"fps": float(project["fps"]),
"zou_id": project["id"],
"active": project["project_status_name"] != "Closed",
"active": KitsuStateToBool[project["project_status_name"]]
}
)

Expand Down Expand Up @@ -356,6 +361,23 @@ def write_project_to_op(project: dict, dbcon: AvalonMongoDB) -> UpdateOne:
)


def update_project_state_in_db(dbcon: AvalonMongoDB, project: dict, active: bool):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (82 > 79 characters)

bulk_writes = []
project['data']['active'] = active
dbcon.Session["AVALON_PROJECT"] = project["name"]
bulk_writes.append(
UpdateOne(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line over-indented for hanging indent

{"_id": project["_id"]},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line missing indentation or outdented

{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line missing indentation or outdented

"$set": {
"data": project['data']
}
}
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line missing indentation or outdented

)
dbcon.bulk_write(bulk_writes)


def sync_all_projects(
login: str,
password: str,
Expand All @@ -372,6 +394,8 @@ def sync_all_projects(
Raises:
gazu.exception.AuthFailedException: Wrong user login and/or password
"""
if not ignore_projects:
ignore_projects = []

# Authenticate
if not validate_credentials(login, password):
Expand All @@ -385,6 +409,7 @@ def sync_all_projects(
all_projects = gazu.project.all_projects()

project_to_sync = []
all_project_names = {p["name"]: p for p in all_projects}

if filter_projects:
all_kitsu_projects = {p["name"]: p for p in all_projects}
Expand All @@ -400,8 +425,15 @@ def sync_all_projects(
# all project
project_to_sync = all_projects

# Iterate over MongoDB projects and if it's not present in Kitsu, deactivate it on MongoDB
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (94 > 79 characters)

for project in dbcon.projects():
if project['name'] in all_project_names:
# Project exists on Kitsu, skip
continue
update_project_state_in_db(dbcon, project, active=False)

for project in project_to_sync:
if ignore_projects and project["name"] in ignore_projects:
if project["name"] in ignore_projects:
continue
sync_project_from_kitsu(dbcon, project)

Expand Down Expand Up @@ -431,10 +463,26 @@ def sync_project_from_kitsu(dbcon: AvalonMongoDB, project: dict):
project["project_status_name"] = status["name"]
break

# Do not sync closed kitsu project that is not found in openpype
if project["project_status_name"] == "Closed" and not get_project(
project["name"]
):
# Get the project from OpenPype DB
project_name = project["name"]
project_dict = get_project(project_name)
project_active_state_kitsu = KitsuStateToBool[project["project_status_name"]]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (81 > 79 characters)


# Early exit condition if the project is deactivated (closed) on Kitsu
if not project_active_state_kitsu:
if not project_dict:
# The project doesn't exist on OpenPype DB, skip
return

# Deactivate the project on the OpenPype DB (if not already), then return
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (81 > 79 characters)

op_active_state = project_dict.get('data', {}).get('active', False)
if op_active_state != project_active_state_kitsu:
log.info(f"Deactivate {project['name']} on OpenPype DB...")
update_project_state_in_db(
dbcon,
project_dict,
active=project_active_state_kitsu
)
return

log.info(f"Synchronizing {project['name']}...")
Expand All @@ -456,18 +504,15 @@ def sync_project_from_kitsu(dbcon: AvalonMongoDB, project: dict):
]

# Sync project. Create if doesn't exist
project_name = project["name"]
project_dict = get_project(project_name)
if not project_dict:
log.info("Project created: {}".format(project_name))
bulk_writes.append(write_project_to_op(project, dbcon))

if project["project_status_name"] == "Closed":
return
bulk_writes.append(write_project_to_op(project, dbcon))

# Try to find project document
if not project_dict:
# Try to find the newly created project document on OpenPype DB
project_dict = get_project(project_name)

dbcon.Session["AVALON_PROJECT"] = project_name

# Query all assets of the local project
Expand Down
Loading