Skip to content

integrate_ftrack_note

Requires

context > hostName context > appName context > appLabel context > comment context > ftrackSession instance > ftrackIntegratedAssetVersionsData

IntegrateFtrackNote

Bases: FtrackPublishInstancePlugin

Create comments in ftrack.

Source code in client/ayon_ftrack/plugins/publish/integrate_ftrack_note.py
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class IntegrateFtrackNote(plugin.FtrackPublishInstancePlugin):
    """Create comments in ftrack."""

    # Must be after IntegrateAsset plugin in ayon_core
    order = pyblish.api.IntegratorOrder + 0.4999
    label = "Integrate ftrack note"
    families = ["ftrack"]

    # Can be set in presets:
    # - Allows only `comment` keys
    note_template = ""
    # - note label must exist in ftrack
    note_labels = []

    def process(self, instance):
        # Check if there are any integrated AssetVersion entities
        asset_versions_key = "ftrackIntegratedAssetVersionsData"
        asset_versions_data_by_id = instance.data.get(asset_versions_key)
        if not asset_versions_data_by_id:
            self.log.info("There are any integrated AssetVersions")
            return

        context = instance.context
        host_name = context.data["hostName"]
        app_name = context.data["appName"]
        app_label = context.data["appLabel"]
        comment = instance.data["comment"]
        if not comment:
            self.log.debug("Comment is not set.")
        else:
            self.log.debug(f"Comment is set to '{comment}'")

        session = context.data["ftrackSession"]

        # if intent label is set then format comment
        # - it is possible that intent_label is equal to "" (empty string)
        user = session.query(
            f"User where username is \"{session.api_user}\""
        ).first()
        if not user:
            self.log.warning(
                f"Was not able to query current User {session.api_user}"
            )

        labels = []
        if self.note_labels:
            all_labels = session.query("select id, name from NoteLabel").all()
            labels_by_low_name = {
                lab["name"].lower(): lab
                for lab in all_labels
            }
            for _label in self.note_labels:
                label = labels_by_low_name.get(_label.lower())
                if not label:
                    self.log.warning(f"Note Label `{_label}` was not found.")
                    continue

                labels.append(label)

        base_format_data = {
            "host_name": host_name,
            "app_name": app_name,
            "app_label": app_label,
            "source": instance.data.get("source", "")
        }
        if comment:
            base_format_data["comment"] = comment

        for asset_version_data in asset_versions_data_by_id.values():
            asset_version = asset_version_data["asset_version"]
            component_items = asset_version_data["component_items"]

            published_paths = set()
            for component_item in component_items:
                published_paths.add(component_item["component_path"])

            template = self.note_template
            format_data = copy.deepcopy(base_format_data)
            format_data["published_paths"] = "<br/>".join(
                sorted(published_paths)
            )
            note_text = StringTemplate.format_template(template, format_data)
            if not note_text.solved:
                self.log.debug(
                    "Note template require more keys then can be provided."
                    f"\nTemplate: {template}"
                    f"\nMissing values for keys:{note_text.missing_keys}"
                    f"\nData: {format_data}"
                )
                continue

            if not note_text:
                av_id = asset_version["id"]
                self.log.debug(
                    f"Note for AssetVersion {av_id} would be empty. Skipping."
                    f"\nTemplate: {template}\nData: {format_data}"
                )
                continue
            asset_version.create_note(note_text, author=user, labels=labels)

            try:
                session.commit()
                self.log.debug(
                    f"Note added to AssetVersion \"{asset_version}\""
                )
            except Exception as exc:
                session.rollback()
                session._configure_locations()
                raise exc