|
1 | | -import json |
2 | | -import collections |
3 | 1 | from typing import Optional |
4 | 2 |
|
5 | 3 | import ayon_api |
6 | | -from ayon_api.graphql import FIELD_VALUE, GraphQlQuery, fields_to_dict |
7 | 4 |
|
8 | 5 | from ayon_core.lib import NestedCacheItem, get_ayon_username |
9 | 6 |
|
10 | 7 | NOT_SET = object() |
11 | 8 |
|
12 | 9 |
|
13 | | -# --- Implementation that should be in ayon-python-api --- |
14 | | -# The implementation is not available in all versions of ayon-python-api. |
15 | | -def users_graphql_query(fields): |
16 | | - query = GraphQlQuery("Users") |
17 | | - names_var = query.add_variable("userNames", "[String!]") |
18 | | - project_name_var = query.add_variable("projectName", "String!") |
19 | | - |
20 | | - users_field = query.add_field_with_edges("users") |
21 | | - users_field.set_filter("names", names_var) |
22 | | - users_field.set_filter("projectName", project_name_var) |
23 | | - |
24 | | - nested_fields = fields_to_dict(set(fields)) |
25 | | - |
26 | | - query_queue = collections.deque() |
27 | | - for key, value in nested_fields.items(): |
28 | | - query_queue.append((key, value, users_field)) |
29 | | - |
30 | | - while query_queue: |
31 | | - item = query_queue.popleft() |
32 | | - key, value, parent = item |
33 | | - field = parent.add_field(key) |
34 | | - if value is FIELD_VALUE: |
35 | | - continue |
36 | | - |
37 | | - for k, v in value.items(): |
38 | | - query_queue.append((k, v, field)) |
39 | | - return query |
40 | | - |
41 | | - |
42 | | -def get_users(project_name=None, usernames=None, fields=None): |
43 | | - """Get Users. |
44 | | -
|
45 | | - Only administrators and managers can fetch all users. For other users |
46 | | - it is required to pass in 'project_name' filter. |
47 | | -
|
48 | | - Args: |
49 | | - project_name (Optional[str]): Project name. |
50 | | - usernames (Optional[Iterable[str]]): Filter by usernames. |
51 | | - fields (Optional[Iterable[str]]): Fields to be queried |
52 | | - for users. |
53 | | -
|
54 | | - Returns: |
55 | | - Generator[dict[str, Any]]: Queried users. |
56 | | -
|
57 | | - """ |
58 | | - filters = {} |
59 | | - if usernames is not None: |
60 | | - usernames = set(usernames) |
61 | | - if not usernames: |
62 | | - return |
63 | | - filters["userNames"] = list(usernames) |
64 | | - |
65 | | - if project_name is not None: |
66 | | - filters["projectName"] = project_name |
67 | | - |
68 | | - con = ayon_api.get_server_api_connection() |
69 | | - if not fields: |
70 | | - fields = con.get_default_fields_for_type("user") |
71 | | - |
72 | | - query = users_graphql_query(set(fields)) |
73 | | - for attr, filter_value in filters.items(): |
74 | | - query.set_variable_value(attr, filter_value) |
75 | | - |
76 | | - for parsed_data in query.continuous_query(con): |
77 | | - for user in parsed_data["users"]: |
78 | | - user["accessGroups"] = json.loads(user["accessGroups"]) |
79 | | - yield user |
80 | | -# --- END of ayon-python-api implementation --- |
81 | | - |
82 | | - |
83 | 10 | class UserItem: |
84 | 11 | def __init__( |
85 | 12 | self, |
@@ -172,5 +99,5 @@ def _invalidate_cache(self, project_name): |
172 | 99 |
|
173 | 100 | self._users_cache[project_name].update_data([ |
174 | 101 | UserItem.from_entity_data(user) |
175 | | - for user in get_users(project_name) |
| 102 | + for user in ayon_api.get_users(project_name=project_name) |
176 | 103 | ]) |
0 commit comments