Skip to content

Commit 6ed1b9b

Browse files
authored
Merge pull request #255 from ynput/enhancement/254-product-base-types-add-support
🏛️Product base types: add support for product base types in API
2 parents a62ff08 + 9d31f0e commit 6ed1b9b

File tree

10 files changed

+178
-27
lines changed

10 files changed

+178
-27
lines changed

ayon_api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
get_info,
5555
get_server_version,
5656
get_server_version_tuple,
57+
is_product_base_type_supported,
5758
get_users,
5859
get_user_by_name,
5960
get_user,
@@ -332,6 +333,7 @@
332333
"get_info",
333334
"get_server_version",
334335
"get_server_version_tuple",
336+
"is_product_base_type_supported",
335337
"get_users",
336338
"get_user_by_name",
337339
"get_user",

ayon_api/_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,13 @@ def get_server_version_tuple() -> ServerVersion:
721721
return con.get_server_version_tuple()
722722

723723

724+
def is_product_base_type_supported() -> bool:
725+
"""Product base types are available on server.
726+
"""
727+
con = get_server_api_connection()
728+
return con.is_product_base_type_supported()
729+
730+
724731
def get_users(
725732
project_name: Optional[str] = None,
726733
usernames: Optional[Iterable[str]] = None,
@@ -4900,6 +4907,7 @@ def get_products(
49004907
product_names: Optional[Iterable[str]] = None,
49014908
folder_ids: Optional[Iterable[str]] = None,
49024909
product_types: Optional[Iterable[str]] = None,
4910+
product_base_types: Optional[Iterable[str]] = None,
49034911
product_name_regex: Optional[str] = None,
49044912
product_path_regex: Optional[str] = None,
49054913
names_by_folder_ids: Optional[dict[str, Iterable[str]]] = None,
@@ -4952,6 +4960,7 @@ def get_products(
49524960
product_names=product_names,
49534961
folder_ids=folder_ids,
49544962
product_types=product_types,
4963+
product_base_types=product_base_types,
49554964
product_name_regex=product_name_regex,
49564965
product_path_regex=product_path_regex,
49574966
names_by_folder_ids=names_by_folder_ids,
@@ -5110,6 +5119,7 @@ def create_product(
51105119
tags: Optional[Iterable[str]] = None,
51115120
status: Optional[str] = None,
51125121
active: Optional[bool] = None,
5122+
product_base_type: Optional[str] = None,
51135123
product_id: Optional[str] = None,
51145124
) -> str:
51155125
"""Create new product.
@@ -5124,6 +5134,7 @@ def create_product(
51245134
tags (Optional[Iterable[str]]): Product tags.
51255135
status (Optional[str]): Product status.
51265136
active (Optional[bool]): Product active state.
5137+
product_base_type (Optional[str]): Product base type.
51275138
product_id (Optional[str]): Product id. If not passed new id is
51285139
generated.
51295140
@@ -5142,6 +5153,7 @@ def create_product(
51425153
tags=tags,
51435154
status=status,
51445155
active=active,
5156+
product_base_type=product_base_type,
51455157
product_id=product_id,
51465158
)
51475159

@@ -5152,6 +5164,7 @@ def update_product(
51525164
name: Optional[str] = None,
51535165
folder_id: Optional[str] = None,
51545166
product_type: Optional[str] = None,
5167+
product_base_type: Optional[str] = None,
51555168
attrib: Optional[dict[str, Any]] = None,
51565169
data: Optional[dict[str, Any]] = None,
51575170
tags: Optional[Iterable[str]] = None,
@@ -5171,6 +5184,7 @@ def update_product(
51715184
name (Optional[str]): New product name.
51725185
folder_id (Optional[str]): New product id.
51735186
product_type (Optional[str]): New product type.
5187+
product_base_type (Optional[str]): New product base type.
51745188
attrib (Optional[dict[str, Any]]): New product attributes.
51755189
data (Optional[dict[str, Any]]): New product data.
51765190
tags (Optional[Iterable[str]]): New product tags.
@@ -5185,6 +5199,7 @@ def update_product(
51855199
name=name,
51865200
folder_id=folder_id,
51875201
product_type=product_type,
5202+
product_base_type=product_base_type,
51885203
attrib=attrib,
51895204
data=data,
51905205
tags=tags,

ayon_api/_api_helpers/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class BaseServerAPI:
2525
def log(self) -> logging.Logger:
2626
raise NotImplementedError()
2727

28+
def is_product_base_type_supported(self) -> bool:
29+
raise NotImplementedError()
30+
2831
def get_server_version(self) -> str:
2932
raise NotImplementedError()
3033

ayon_api/_api_helpers/products.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import typing
66
from typing import Optional, Iterable, Generator, Any
77

8+
from ayon_api.exceptions import UnsupportedServerVersion
89
from ayon_api.utils import (
910
prepare_list_filters,
1011
create_entity_id,
@@ -32,9 +33,10 @@ def get_products(
3233
self,
3334
project_name: str,
3435
product_ids: Optional[Iterable[str]] = None,
35-
product_names: Optional[Iterable[str]]=None,
36-
folder_ids: Optional[Iterable[str]]=None,
37-
product_types: Optional[Iterable[str]]=None,
36+
product_names: Optional[Iterable[str]] = None,
37+
folder_ids: Optional[Iterable[str]] = None,
38+
product_types: Optional[Iterable[str]] = None,
39+
product_base_types: Optional[Iterable[str]] = None,
3840
product_name_regex: Optional[str] = None,
3941
product_path_regex: Optional[str] = None,
4042
names_by_folder_ids: Optional[dict[str, Iterable[str]]] = None,
@@ -59,6 +61,8 @@ def get_products(
5961
Use 'None' if folder is direct child of project.
6062
product_types (Optional[Iterable[str]]): Product types used for
6163
filtering.
64+
product_base_types (Optional[Iterable[str]]): Product base types
65+
used for filtering.
6266
product_name_regex (Optional[str]): Filter products by name regex.
6367
product_path_regex (Optional[str]): Filter products by path regex.
6468
Path starts with folder path and ends with product name.
@@ -83,6 +87,11 @@ def get_products(
8387
if not project_name:
8488
return
8589

90+
if product_base_types and not self.is_product_base_type_supported():
91+
raise UnsupportedServerVersion(
92+
"Product base type is not supported for your server version."
93+
)
94+
8695
# Prepare these filters before 'name_by_filter_ids' filter
8796
filter_product_names = None
8897
if product_names is not None:
@@ -150,6 +159,7 @@ def get_products(
150159
filters,
151160
("productIds", product_ids),
152161
("productTypes", product_types),
162+
("productBaseTypes", product_base_types),
153163
("productStatuses", statuses),
154164
("productTags", tags),
155165
):
@@ -378,6 +388,7 @@ def create_product(
378388
tags: Optional[Iterable[str]] =None,
379389
status: Optional[str] = None,
380390
active: Optional[bool] = None,
391+
product_base_type: Optional[str] = None,
381392
product_id: Optional[str] = None,
382393
) -> str:
383394
"""Create new product.
@@ -392,13 +403,22 @@ def create_product(
392403
tags (Optional[Iterable[str]]): Product tags.
393404
status (Optional[str]): Product status.
394405
active (Optional[bool]): Product active state.
406+
product_base_type (Optional[str]): Product base type.
395407
product_id (Optional[str]): Product id. If not passed new id is
396408
generated.
397409
398410
Returns:
399411
str: Product id.
400412
401413
"""
414+
if (
415+
product_base_type is not None
416+
and not self.is_product_base_type_supported()
417+
):
418+
raise UnsupportedServerVersion(
419+
"Product base type is not supported for your server version."
420+
)
421+
402422
if not product_id:
403423
product_id = create_entity_id()
404424
create_data = {
@@ -408,6 +428,7 @@ def create_product(
408428
"folderId": folder_id,
409429
}
410430
for key, value in (
431+
("productBaseType", product_base_type),
411432
("attrib", attrib),
412433
("data", data),
413434
("tags", tags),
@@ -431,6 +452,7 @@ def update_product(
431452
name: Optional[str] = None,
432453
folder_id: Optional[str] = None,
433454
product_type: Optional[str] = None,
455+
product_base_type: Optional[str] = None,
434456
attrib: Optional[dict[str, Any]] = None,
435457
data: Optional[dict[str, Any]] = None,
436458
tags: Optional[Iterable[str]] = None,
@@ -450,17 +472,27 @@ def update_product(
450472
name (Optional[str]): New product name.
451473
folder_id (Optional[str]): New product id.
452474
product_type (Optional[str]): New product type.
475+
product_base_type (Optional[str]): New product base type.
453476
attrib (Optional[dict[str, Any]]): New product attributes.
454477
data (Optional[dict[str, Any]]): New product data.
455478
tags (Optional[Iterable[str]]): New product tags.
456479
status (Optional[str]): New product status.
457480
active (Optional[bool]): New product active state.
458481
459482
"""
483+
if (
484+
product_base_type is not None
485+
and not self.is_product_base_type_supported()
486+
):
487+
raise UnsupportedServerVersion(
488+
"Product base type is not supported for your server version."
489+
)
490+
460491
update_data = {}
461492
for key, value in (
462493
("name", name),
463494
("productType", product_type),
495+
("productBaseType", product_base_type),
464496
("folderId", folder_id),
465497
("attrib", attrib),
466498
("data", data),

ayon_api/_api_helpers/projects.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
import typing
88
from typing import Optional, Generator, Iterable, Any
99

10-
from ayon_api.constants import PROJECT_NAME_REGEX
10+
from ayon_api.constants import (
11+
PROJECT_NAME_REGEX,
12+
DEFAULT_PRODUCT_BASE_TYPE_FIELDS,
13+
DEFAULT_PRODUCT_TYPE_FIELDS,
14+
)
1115
from ayon_api.utils import prepare_query_string, fill_own_attribs
1216
from ayon_api.graphql_queries import projects_graphql_query
1317

@@ -679,9 +683,8 @@ def _get_project_graphql_fields(
679683
elif field == "productTypes":
680684
must_use_graphql = True
681685
fields.discard(field)
682-
graphql_fields.add("productTypes.name")
683-
graphql_fields.add("productTypes.icon")
684-
graphql_fields.add("productTypes.color")
686+
for f_name in DEFAULT_PRODUCT_TYPE_FIELDS:
687+
fields.add(f"{field}.{f_name}")
685688

686689
elif field.startswith("productTypes"):
687690
must_use_graphql = True
@@ -690,7 +693,8 @@ def _get_project_graphql_fields(
690693
elif field == "productBaseTypes":
691694
must_use_graphql = True
692695
fields.discard(field)
693-
graphql_fields.add("productBaseTypes.name")
696+
for f_name in DEFAULT_PRODUCT_BASE_TYPE_FIELDS:
697+
fields.add(f"{field}.{f_name}")
694698

695699
elif field.startswith("productBaseTypes"):
696700
must_use_graphql = True

ayon_api/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@
8282
"color",
8383
}
8484

85+
# --- Product base type ---
86+
DEFAULT_PRODUCT_BASE_TYPE_FIELDS = {
87+
# Ignore 'icon' and 'color'
88+
# - current server implementation always returns 'null'
89+
"name",
90+
}
91+
8592
# --- Project ---
8693
DEFAULT_PROJECT_FIELDS = {
8794
"active",

0 commit comments

Comments
 (0)