-
Notifications
You must be signed in to change notification settings - Fork 128
[QUAD] Ftrack Task Status Update: Rework of the task status change + cleanup #6327
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,23 @@ | |
import collections | ||
import platform | ||
|
||
from openpype.lib import register_event_callback | ||
from openpype.modules import ( | ||
click_wrap, | ||
OpenPypeModule, | ||
ITrayModule, | ||
IPluginPaths, | ||
ISettingsChangeListener | ||
) | ||
from openpype.settings import SaveWarningExc | ||
from openpype.settings import SaveWarningExc, get_project_settings | ||
from openpype.lib import Logger | ||
|
||
from openpype.pipeline import ( | ||
get_current_project_name, | ||
get_current_asset_name, | ||
get_current_task_name | ||
) | ||
|
||
FTRACK_MODULE_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
_URL_NOT_SET = object() | ||
|
||
|
@@ -62,6 +69,133 @@ def initialize(self, settings): | |
self.timers_manager_connector = None | ||
self._timers_manager_module = None | ||
|
||
# Hooks when a file has been opened or saved | ||
register_event_callback("open", self.after_file_open) | ||
register_event_callback("after.save", self.after_file_save) | ||
|
||
def find_ftrack_task_entity( | ||
self, session, project_name, asset_name, task_name | ||
): | ||
project_entity = session.query( | ||
"Project where full_name is \"{}\"".format(project_name) | ||
).first() | ||
if not project_entity: | ||
self.log.warning( | ||
"Couldn't find project \"{}\" in Ftrack.".format(project_name) | ||
) | ||
return | ||
|
||
potential_task_entities = session.query(( | ||
"TypedContext where parent.name is \"{}\" and project_id is \"{}\"" | ||
).format(asset_name, project_entity["id"])).all() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line over-indented for hanging indent |
||
filtered_entities = [] | ||
for _entity in potential_task_entities: | ||
if ( | ||
_entity.entity_type.lower() == "task" | ||
and _entity["name"] == task_name | ||
): | ||
filtered_entities.append(_entity) | ||
|
||
if not filtered_entities: | ||
self.log.warning(( | ||
"Couldn't find task \"{}\" under parent \"{}\" in Ftrack." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line over-indented for hanging indent |
||
).format(task_name, asset_name)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line over-indented for hanging indent |
||
return | ||
|
||
if len(filtered_entities) > 1: | ||
self.log.warning(( | ||
"Found more than one task \"{}\"" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line over-indented for hanging indent |
||
" under parent \"{}\" in Ftrack." | ||
).format(task_name, asset_name)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line over-indented for hanging indent |
||
return | ||
|
||
return filtered_entities[0] | ||
|
||
def after_file_open(self, event): | ||
self._auto_update_task_status("status_change_on_file_open") | ||
|
||
def after_file_save(self, event): | ||
self._auto_update_task_status("status_change_on_file_save") | ||
|
||
def _auto_update_task_status(self, setting_mapping_section): | ||
# Create ftrack session | ||
try: | ||
session = self.create_ftrack_session() | ||
except Exception: # noqa | ||
self.log.warning("Couldn't create ftrack session.", exc_info=True) | ||
return | ||
# ---------------------- | ||
|
||
project_name = get_current_project_name() | ||
project_settings = get_project_settings(project_name) | ||
|
||
# Do we want/need/can update the status? | ||
change_task_status = project_settings["ftrack"]["application_handlers"]["change_task_status"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (101 > 79 characters) |
||
if not change_task_status["enabled"]: | ||
return | ||
|
||
mapping = change_task_status[setting_mapping_section] | ||
if not mapping: | ||
# No rules registered, skip. | ||
return | ||
# -------------------------------------- | ||
|
||
asset_name = get_current_asset_name() | ||
task_name = get_current_task_name() | ||
|
||
# Find the entity | ||
entity = self.find_ftrack_task_entity(session, project_name, asset_name, task_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (91 > 79 characters) |
||
if not entity: | ||
# No valid entity found, quit. | ||
return | ||
# --------------- | ||
|
||
ent_path = "/".join([ent["name"] for ent in entity["link"]]) | ||
actual_status = entity["status"]["name"] | ||
|
||
# Find the next status | ||
if "__ignore__" in mapping: | ||
ignored_statuses = [status for status in mapping["__ignore__"]] | ||
if actual_status in ignored_statuses: | ||
# We can exit, the status is flagged to be ignored. | ||
return | ||
|
||
# Removing to avoid looping on it | ||
mapping.pop("__ignore__") | ||
|
||
next_status = None | ||
|
||
for to_status, from_statuses in mapping.items(): | ||
from_statuses = [status for status in from_statuses] | ||
if "__any__" in from_statuses: | ||
next_status = to_status | ||
# Not breaking, in case a better mapping is set after. | ||
continue | ||
|
||
if actual_status in from_statuses: | ||
next_status = to_status | ||
# We found a valid mapping (other that __any__) we stop looking. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (80 > 79 characters) |
||
break | ||
|
||
if not next_status: | ||
# No valid next status found, skip. | ||
return | ||
# -------------------- | ||
|
||
# Change the status on ftrack | ||
try: | ||
query = "Status where name is \"{}\"".format(next_status) | ||
next_status_obj = session.query(query).one() | ||
|
||
entity["status"] = next_status_obj | ||
session.commit() | ||
self.log.debug("Changing current task status to \"{}\" <{}>".format(next_status, ent_path)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (103 > 79 characters) |
||
except Exception: # noqa | ||
session.rollback() | ||
msg = "Status \"{}\" in presets wasn't found on Ftrack entity type \"{}\"".format(next_status, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (106 > 79 characters) |
||
entity.entity_type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (113 > 79 characters) |
||
self.log.error(msg) | ||
|
||
def get_ftrack_url(self): | ||
"""Resolved ftrack url. | ||
|
||
|
This file was deleted.
There was a problem hiding this comment.
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
line too long (119 > 79 characters)