Skip to content

server

ShotgridAddon

Bases: BaseServerAddon

Source code in server/__init__.py
 15
 16
 17
 18
 19
 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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",
        )

    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 from public.attributes "
            f"WHERE (name = '{SG_ID_ATTRIB}'"
            f" OR name = '{SG_TYPE_ATTRIB}'"
            f" OR name = '{SG_PUSH_ATTRIB}') "
        )

        if not shotgrid_attributes or len(shotgrid_attributes) < 3:
            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
                ["project", "folder", "task"],  # 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
                ["project", "folder", "task"],  # 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
                ["project"],  # 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"]

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
 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
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 from public.attributes "
        f"WHERE (name = '{SG_ID_ATTRIB}'"
        f" OR name = '{SG_TYPE_ATTRIB}'"
        f" OR name = '{SG_PUSH_ATTRIB}') "
    )

    if not shotgrid_attributes or len(shotgrid_attributes) < 3:
        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
            ["project", "folder", "task"],  # 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
            ["project", "folder", "task"],  # 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
            ["project"],  # 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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"]