Skip to content

server

ShotgridAddon

Bases: BaseServerAddon

Source code in server/__init__.py
 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
class ShotgridAddon(BaseServerAddon):
    settings_model: Type[ShotgridSettings] = ShotgridSettings

    frontend_scopes: dict[str, Any] = {"settings": {}}

    def initialize(self) -> None:

        # returning user for SG id value
        self.add_endpoint(
            "/get_ayon_name_by_sg_id/{sg_user_id}",
            self.get_ayon_name_by_sg_id,
            method="GET",
        )
        self.add_endpoint(
            "/{project_name}/trigger_mediapath",
            self.trigger_mediapath_event,
            method="POST",
        )

    async def setup(self):
        need_restart = await self.create_shotgrid_attributes()
        if need_restart:
            logging.debug(
                "Created or updated attributes in database, "
                "requesting a server restart."
            )
            self.request_server_restart()

    async def create_shotgrid_attributes(self) -> bool:
        """Make sure AYON has the `shotgridId` and `shotgridPath` attributes.

        Returns:
            bool: 'True' if an attribute was created or updated.
        """

        if Postgres.pool is None:
            await Postgres.connect()

        all_attributes = await Postgres.fetch(
            "SELECT name from public.attributes"
        )

        num_of_attributes = len(all_attributes)

        shotgrid_attributes = await Postgres.fetch(
            "SELECT name, scope from public.attributes "
            f"WHERE (name = '{SG_ID_ATTRIB}'"
            f" OR name = '{SG_TYPE_ATTRIB}'"
            f" OR name = '{SG_PUSH_ATTRIB}') "
        )

        expected_scopes = {
            SG_ID_ATTRIB: ["project", "folder", "task", "version"],
            SG_TYPE_ATTRIB:  ["project", "folder", "task", "version"],
            SG_PUSH_ATTRIB: ["project"]
        }
        not_matching_scopes = False
        for attr in shotgrid_attributes:
            if expected_scopes[attr["name"]] != attr["scope"]:
                not_matching_scopes = True
                break
        if (not shotgrid_attributes or
                len(shotgrid_attributes) < 3 or
                not_matching_scopes):
            postgres_query = "\n".join((
                "INSERT INTO public.attributes",
                "    (name, position, scope, data)",
                "VALUES",
                "    ($1, $2, $3, $4)",
                "ON CONFLICT (name)",
                "DO UPDATE SET",
                "    scope = $3,",
                "    data = $4",
            ))

            logging.debug("Creating Shotgrid Attributes...")

            await Postgres.execute(
                postgres_query,
                SG_ID_ATTRIB,  # name
                num_of_attributes + 1,  # Add Attributes at the end of the list
                expected_scopes[SG_ID_ATTRIB],  # scope
                {
                    "type": "string",
                    "title": "Shotgrid ID",
                    "description": "The Shotgrid ID of this entity.",
                    "inherit": False
                }
            )

            await Postgres.execute(
                postgres_query,
                SG_TYPE_ATTRIB,  # name
                num_of_attributes + 2,  # Add Attributes at the end of the list
                expected_scopes[SG_TYPE_ATTRIB],  # scope
                {
                    "type": "string",
                    "title": "Shotgrid Type",
                    "description": "The Shotgrid Type of this entity.",
                    "inherit": False
                }
            )

            await Postgres.execute(
                postgres_query,
                SG_PUSH_ATTRIB,  # name
                num_of_attributes + 3,  # Add Attributes at the end of the list
                expected_scopes[SG_PUSH_ATTRIB],  # scope
                {
                    "type": "boolean",
                    "title": "Shotgrid Push",
                    "description": (
                        "Push changes done to this project to ShotGrid. "
                        "Requires the transmitter service."
                    ),
                    "inherit": False,
                    "value": False,
                }
            )

            return True

        else:
            logging.debug("Shotgrid Attributes already exist.")
            return False

    async def get_ayon_name_by_sg_id(
        self,
        sg_user_id: str = Path(
            ...,
            description="Id of Shotgrid user ",
            example="123",
        )
    ) -> Optional[str]:
        """Queries user for specific 'sg_user_id' field in 'data'.

        Field added during user synchronization to be explicit, not depending that
        SG login will be same as AYON (which is not as @ is not allowed in AYON)
        """
        query = f"""
            SELECT
                *
            FROM public.users
            WHERE data ? 'sg_user_id' AND data->>'sg_user_id' = '{sg_user_id}';
        """

        res = await Postgres.fetch(query)
        if res:
            return res[0]["name"]

    async def trigger_mediapath_event(
        self,
        user: CurrentUser,
        project_name: ProjectName,
        data: dict[str, Any] = Body(...),
    ) -> Response:
        """Temporary endpoint to trigger event with explicit sender_type"""
        response = await dispatch_event(
            "flow.version.mediapath",
            project=project_name,
            sender_type="publish",
            description="Update media paths on synchronized Version",
            payload=data,
        )

        return Response(status_code=200, content=str(response))

create_shotgrid_attributes() async

Make sure AYON has the shotgridId and shotgridPath attributes.

Returns:

Name Type Description
bool bool

'True' if an attribute was created or updated.

Source code in server/__init__.py
 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
async def create_shotgrid_attributes(self) -> bool:
    """Make sure AYON has the `shotgridId` and `shotgridPath` attributes.

    Returns:
        bool: 'True' if an attribute was created or updated.
    """

    if Postgres.pool is None:
        await Postgres.connect()

    all_attributes = await Postgres.fetch(
        "SELECT name from public.attributes"
    )

    num_of_attributes = len(all_attributes)

    shotgrid_attributes = await Postgres.fetch(
        "SELECT name, scope from public.attributes "
        f"WHERE (name = '{SG_ID_ATTRIB}'"
        f" OR name = '{SG_TYPE_ATTRIB}'"
        f" OR name = '{SG_PUSH_ATTRIB}') "
    )

    expected_scopes = {
        SG_ID_ATTRIB: ["project", "folder", "task", "version"],
        SG_TYPE_ATTRIB:  ["project", "folder", "task", "version"],
        SG_PUSH_ATTRIB: ["project"]
    }
    not_matching_scopes = False
    for attr in shotgrid_attributes:
        if expected_scopes[attr["name"]] != attr["scope"]:
            not_matching_scopes = True
            break
    if (not shotgrid_attributes or
            len(shotgrid_attributes) < 3 or
            not_matching_scopes):
        postgres_query = "\n".join((
            "INSERT INTO public.attributes",
            "    (name, position, scope, data)",
            "VALUES",
            "    ($1, $2, $3, $4)",
            "ON CONFLICT (name)",
            "DO UPDATE SET",
            "    scope = $3,",
            "    data = $4",
        ))

        logging.debug("Creating Shotgrid Attributes...")

        await Postgres.execute(
            postgres_query,
            SG_ID_ATTRIB,  # name
            num_of_attributes + 1,  # Add Attributes at the end of the list
            expected_scopes[SG_ID_ATTRIB],  # scope
            {
                "type": "string",
                "title": "Shotgrid ID",
                "description": "The Shotgrid ID of this entity.",
                "inherit": False
            }
        )

        await Postgres.execute(
            postgres_query,
            SG_TYPE_ATTRIB,  # name
            num_of_attributes + 2,  # Add Attributes at the end of the list
            expected_scopes[SG_TYPE_ATTRIB],  # scope
            {
                "type": "string",
                "title": "Shotgrid Type",
                "description": "The Shotgrid Type of this entity.",
                "inherit": False
            }
        )

        await Postgres.execute(
            postgres_query,
            SG_PUSH_ATTRIB,  # name
            num_of_attributes + 3,  # Add Attributes at the end of the list
            expected_scopes[SG_PUSH_ATTRIB],  # scope
            {
                "type": "boolean",
                "title": "Shotgrid Push",
                "description": (
                    "Push changes done to this project to ShotGrid. "
                    "Requires the transmitter service."
                ),
                "inherit": False,
                "value": False,
            }
        )

        return True

    else:
        logging.debug("Shotgrid Attributes already exist.")
        return False

get_ayon_name_by_sg_id(sg_user_id=Path(..., description='Id of Shotgrid user ', example='123')) async

Queries user for specific 'sg_user_id' field in 'data'.

Field added during user synchronization to be explicit, not depending that SG login will be same as AYON (which is not as @ is not allowed in AYON)

Source code in server/__init__.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
async def get_ayon_name_by_sg_id(
    self,
    sg_user_id: str = Path(
        ...,
        description="Id of Shotgrid user ",
        example="123",
    )
) -> Optional[str]:
    """Queries user for specific 'sg_user_id' field in 'data'.

    Field added during user synchronization to be explicit, not depending that
    SG login will be same as AYON (which is not as @ is not allowed in AYON)
    """
    query = f"""
        SELECT
            *
        FROM public.users
        WHERE data ? 'sg_user_id' AND data->>'sg_user_id' = '{sg_user_id}';
    """

    res = await Postgres.fetch(query)
    if res:
        return res[0]["name"]

trigger_mediapath_event(user, project_name, data=Body(...)) async

Temporary endpoint to trigger event with explicit sender_type

Source code in server/__init__.py
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
async def trigger_mediapath_event(
    self,
    user: CurrentUser,
    project_name: ProjectName,
    data: dict[str, Any] = Body(...),
) -> Response:
    """Temporary endpoint to trigger event with explicit sender_type"""
    response = await dispatch_event(
        "flow.version.mediapath",
        project=project_name,
        sender_type="publish",
        description="Update media paths on synchronized Version",
        payload=data,
    )

    return Response(status_code=200, content=str(response))