def _attributes_to_set(self, project_defaults): attributes_to_set = {} cust_attrs, hier_cust_attrs = get_pype_attr(self.session, True) for attr in hier_cust_attrs: key = attr["key"] if key.startswith("avalon_"): continue attributes_to_set[key] = { "label": attr["label"], "object": attr, "default": project_defaults.get(key) } for attr in cust_attrs: if attr["entity_type"].lower() != "show": continue key = attr["key"] if key.startswith("avalon_"): continue attributes_to_set[key] = { "label": attr["label"], "object": attr, "default": project_defaults.get(key) } # Sort by label attributes_to_set = dict(sorted( attributes_to_set.items(), key=lambda x: x[1]["label"] )) return attributes_to_set
def launch(self, session, entities, event): project = entities[0] user_message = "This may take some time" self.show_message(event, user_message, result=True) self.log.debug("Preparing entities for cleanup.") all_entities = session.query( self.all_project_entities_query.format(project["id"])).all() all_entities_ids = [ "\"{}\"".format(entity["id"]) for entity in all_entities if entity.entity_type.lower() != "task" ] self.log.debug("Collected {} entities to process.".format( len(all_entities_ids))) entity_ids_joined = ", ".join(all_entities_ids) attrs, hier_attrs = get_pype_attr(session) for attr in hier_attrs: configuration_key = attr["key"] self.log.debug( "Looking for cleanup of custom attribute \"{}\"".format( configuration_key)) configuration_id = attr["id"] call_expr = [{ "action": "query", "expression": self.cust_attr_query.format(entity_ids_joined, configuration_id) }] [values] = self.session.call(call_expr) data = {} for item in values["data"]: value = item["value"] if value is None: data[item["entity_id"]] = value if not data: self.log.debug( "Nothing to clean for \"{}\".".format(configuration_key)) continue self.log.debug("Cleaning up {} values for \"{}\".".format( len(data), configuration_key)) for entity_id, value in data.items(): entity_key = collections.OrderedDict({ "configuration_id": configuration_id, "entity_id": entity_id }) session.recorded_operations.push( ftrack_api.operation.DeleteEntityOperation( "CustomAttributeValue", entity_key)) session.commit() return True
def on_project_anatomy_save( self, old_value, new_value, changes, project_name ): """Implementation of ISettingsChangeListener interface.""" if not project_name: return attributes_changes = changes.get("attributes") if not attributes_changes: return import ftrack_api from pype.modules.ftrack.lib import avalon_sync session = self.create_ftrack_session() project_entity = session.query( "Project where full_name is \"{}\"".format(project_name) ).first() if not project_entity: self.log.warning(( "Ftrack project with names \"{}\" was not found." " Skipping settings attributes change callback." )) return project_id = project_entity["id"] cust_attr, hier_attr = avalon_sync.get_pype_attr(session) cust_attr_by_key = {attr["key"]: attr for attr in cust_attr} hier_attrs_by_key = {attr["key"]: attr for attr in hier_attr} for key, value in attributes_changes.items(): configuration = hier_attrs_by_key.get(key) if not configuration: configuration = cust_attr_by_key.get(key) if not configuration: continue # TODO add value validations # - value type and list items entity_key = collections.OrderedDict() entity_key["configuration_id"] = configuration["id"] entity_key["entity_id"] = project_id session.recorded_operations.push( ftrack_api.operation.UpdateEntityOperation( "ContextCustomAttributeValue", entity_key, "value", ftrack_api.symbol.NOT_SET, value ) ) session.commit()