Skip to content

Commit 8e2279e

Browse files
committed
Merge branch 'develop'
2 parents 82e28e3 + a5c1432 commit 8e2279e

7 files changed

Lines changed: 99 additions & 4 deletions

File tree

ayon_api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
get_representations,
120120
get_representations_parents,
121121
get_representation_parents,
122+
get_repre_ids_by_context_filters,
122123

123124
create_thumbnail,
124125
get_thumbnail,
@@ -245,6 +246,7 @@
245246
"get_representations",
246247
"get_representations_parents",
247248
"get_representation_parents",
249+
"get_repre_ids_by_context_filters",
248250

249251
"create_thumbnail",
250252
"get_thumbnail",

ayon_api/_api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,11 @@ def get_representations_parents(*args, **kwargs):
751751
return con.get_representations_parents(*args, **kwargs)
752752

753753

754+
def get_repre_ids_by_context_filters(*args, **kwargs):
755+
con = get_server_api_connection()
756+
return con.get_repre_ids_by_context_filters(*args, **kwargs)
757+
758+
754759
def create_project(
755760
project_name,
756761
project_code,

ayon_api/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
}
6868

6969
REPRESENTATION_FILES_FIELDS = {
70-
"files.baseName",
70+
"files.name",
7171
"files.hash",
7272
"files.id",
7373
"files.path",

ayon_api/server_api.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
TransferProgress,
5757
)
5858

59+
PatternType = type(re.compile(""))
5960
JSONDecodeError = getattr(json, "JSONDecodeError", ValueError)
6061
# This should be collected from server schema
6162
PROJECT_NAME_ALLOWED_SYMBOLS = "a-zA-Z0-9_"
@@ -1182,6 +1183,7 @@ def query_graphql(self, query, variables=None):
11821183
self._graphl_url,
11831184
json=data
11841185
)
1186+
response.raise_for_status()
11851187
return GraphQlResponse(response)
11861188

11871189
def get_graphql_schema(self):
@@ -3129,6 +3131,92 @@ def get_representations_parents(self, project_name, representation_ids):
31293131

31303132
return output
31313133

3134+
def get_repre_ids_by_context_filters(
3135+
self,
3136+
project_name,
3137+
context_filters,
3138+
representation_names=None,
3139+
version_ids=None
3140+
):
3141+
"""Find representation ids which match passed context filters.
3142+
3143+
Each representation has context integrated on representation entity in
3144+
database. The context may contain project, folder, task name or subset,
3145+
family and many more. This implementation gives option to quickly
3146+
filter representation based on representation data in database.
3147+
3148+
Context filters have defined structure. To define filter of nested
3149+
subfield use dot '.' as delimiter (For example 'task.name').
3150+
Filter values can be regex filters. String or 're.Pattern' can be used.
3151+
3152+
Args:
3153+
project_name (str): Project where to look for representations.
3154+
context_filters (dict[str, list[str]]): Filters of context fields.
3155+
representation_names (Iterable[str]): Representation names, can be
3156+
used as additional filter for representations by their names.
3157+
version_ids (Iterable[str]): Version ids, can be used as additional
3158+
filter for representations by their parent ids.
3159+
3160+
Returns:
3161+
list[str]: Representation ids that match passed filters.
3162+
3163+
Example:
3164+
The function returns just representation ids so if entities are
3165+
required for funtionality they must be queried afterwards by
3166+
their ids.
3167+
>>> project_name = "testProject"
3168+
>>> filters = {
3169+
... "task.name": ["[aA]nimation"],
3170+
... "subset": [".*[Mm]ain"]
3171+
... }
3172+
>>> repre_ids = get_repre_ids_by_context_filters(
3173+
... project_name, filters)
3174+
>>> repres = get_representations(project_name, repre_ids)
3175+
"""
3176+
3177+
if not isinstance(context_filters, dict):
3178+
raise TypeError(
3179+
"Expected 'dict' got {}".format(str(type(context_filters)))
3180+
)
3181+
3182+
filter_body = {}
3183+
if representation_names is not None:
3184+
if not representation_names:
3185+
return []
3186+
filter_body["names"] = list(set(representation_names))
3187+
3188+
if version_ids is not None:
3189+
if not version_ids:
3190+
return []
3191+
filter_body["versionIds"] = list(set(version_ids))
3192+
3193+
body_context_filters = []
3194+
for key, filters in context_filters.items():
3195+
if not isinstance(filters, (set, list, tuple)):
3196+
raise TypeError(
3197+
"Expected 'set', 'list', 'tuple' got {}".format(
3198+
str(type(filters))))
3199+
3200+
3201+
new_filters = set()
3202+
for filter_value in filters:
3203+
if isinstance(filter_value, PatternType):
3204+
filter_value = filter_value.pattern
3205+
new_filters.add(filter_value)
3206+
3207+
body_context_filters.append({
3208+
"key": key,
3209+
"values": list(new_filters)
3210+
})
3211+
3212+
response = self.post(
3213+
"projects/{}/repreContextFilter".format(project_name),
3214+
context=body_context_filters,
3215+
**filter_body
3216+
)
3217+
response.raise_for_status()
3218+
return response.data["ids"]
3219+
31323220
def get_workfiles_info(
31333221
self,
31343222
project_name,

ayon_api/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def validate_url(url):
340340
)
341341

342342
raise UrlError(
343-
"Couldn't connect to server on \"{}\"".format(),
343+
"Couldn't connect to server on \"{}\"".format(url),
344344
title="Couldn't connect to server",
345345
hints=hints + universal_hints
346346
)

ayon_api/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""Package declaring Python API for Ayon server."""
2-
__version__ = "0.1.13"
2+
__version__ = "0.1.14"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ayon_api"
3-
version = "0.1.13"
3+
version = "0.1.14"
44
description = "Python api for AYON server"
55
authors = ["ynput.io <info@ynput.io>"]
66

0 commit comments

Comments
 (0)