Skip to content

Commit 19a4333

Browse files
committed
Extend CatalogUser methods with user groups manipulation
* add user group * add user groups * replace user groups * remove_user_groups
1 parent 1d6fb9d commit 19a4333

File tree

6 files changed

+497
-14
lines changed

6 files changed

+497
-14
lines changed

gooddata-sdk/gooddata_sdk/catalog/user/entity_model/user.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,32 @@ def init(
5252
return cls(id=user_id, attributes=attributes, relationships=relationships)
5353

5454
@property
55-
def get_user_groups(self) -> List[str]:
56-
return self.relationships.get_user_groups if self.relationships is not None else []
55+
def user_groups(self) -> List[CatalogUserGroup]:
56+
if self.relationships and self.relationships.user_groups:
57+
return self.relationships.user_groups.data
58+
return []
59+
60+
def add_user_group(self, user_group: CatalogUserGroup) -> None:
61+
if not self.relationships:
62+
self.relationships = CatalogUserRelationships(user_groups=CatalogUserGroupsData(data=[user_group]))
63+
else:
64+
self.relationships.add_user_groups([user_group])
65+
66+
def add_user_groups(self, user_groups: list[CatalogUserGroup]) -> None:
67+
if not self.relationships:
68+
self.relationships = CatalogUserRelationships(user_groups=CatalogUserGroupsData(data=user_groups))
69+
else:
70+
self.relationships.add_user_groups(user_groups)
71+
72+
def remove_user_groups(self) -> None:
73+
if self.relationships:
74+
self.relationships.user_groups = None
75+
76+
def replace_user_groups(self, user_groups: list[CatalogUserGroup]) -> None:
77+
if not self.relationships:
78+
self.relationships = CatalogUserRelationships(user_groups=CatalogUserGroupsData(data=user_groups))
79+
else:
80+
self.relationships.replace_user_groups(user_groups)
5781

5882

5983
@attr.s(auto_attribs=True, kw_only=True)
@@ -65,9 +89,17 @@ class CatalogUserAttributes(Base):
6589
class CatalogUserRelationships(Base):
6690
user_groups: Optional[CatalogUserGroupsData] = None
6791

68-
@property
69-
def get_user_groups(self) -> List[str]:
70-
return self.user_groups.get_user_groups if self.user_groups is not None else []
92+
def add_user_groups(self, user_groups: list[CatalogUserGroup]) -> None:
93+
if not self.user_groups:
94+
self.user_groups = CatalogUserGroupsData(data=user_groups)
95+
else:
96+
self.user_groups.data.extend(user_groups)
97+
98+
def replace_user_groups(self, user_groups: list[CatalogUserGroup]) -> None:
99+
if not self.user_groups:
100+
self.user_groups = CatalogUserGroupsData(data=user_groups)
101+
else:
102+
self.user_groups.data = user_groups
71103

72104
@classmethod
73105
def create_user_relationships(cls, user_group_ids: Optional[List[str]]) -> CatalogUserRelationships:
@@ -81,8 +113,4 @@ def create_user_relationships(cls, user_group_ids: Optional[List[str]]) -> Catal
81113

82114
@attr.s(auto_attribs=True, kw_only=True)
83115
class CatalogUserGroupsData(Base):
84-
data: Optional[List[CatalogUserGroup]] = None
85-
86-
@property
87-
def get_user_groups(self) -> List[str]:
88-
return [user_group.id for user_group in self.data] if self.data is not None else []
116+
data: List[CatalogUserGroup] = attr.field(factory=list)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# (C) 2023 GoodData Corporation
2+
version: 1
3+
interactions:
4+
- request:
5+
method: GET
6+
uri: http://localhost:3000/api/v1/entities/users/demo2?include=userGroups
7+
body: null
8+
headers:
9+
Accept:
10+
- application/vnd.gooddata.api+json
11+
X-GDC-VALIDATE-RELATIONS:
12+
- 'true'
13+
X-Requested-With:
14+
- XMLHttpRequest
15+
response:
16+
status:
17+
code: 200
18+
message: OK
19+
headers:
20+
Access-Control-Allow-Credentials:
21+
- 'true'
22+
Access-Control-Expose-Headers:
23+
- Content-Disposition, Content-Length, Content-Range, Set-Cookie
24+
Cache-Control:
25+
- no-cache, no-store, max-age=0, must-revalidate
26+
Connection:
27+
- keep-alive
28+
Content-Length:
29+
- '447'
30+
Content-Security-Policy:
31+
- 'default-src ''self'' *.wistia.com *.wistia.net; script-src ''self'' ''unsafe-inline''
32+
''unsafe-eval'' *.wistia.com *.wistia.net src.litix.io matomo.anywhere.gooddata.com
33+
code.jquery.com unpkg.com cdn.jsdelivr.net cdnjs.cloudflare.com; img-src
34+
''self'' data: blob: *.wistia.com *.wistia.net embedwistia-a.akamaihd.net
35+
privacy-policy.truste.com www.gooddata.com; style-src ''self'' ''unsafe-inline''
36+
fonts.googleapis.com cdn.jsdelivr.net fast.fonts.net; font-src ''self''
37+
data: fonts.gstatic.com *.alicdn.com *.wistia.com cdn.jsdelivr.net info.gooddata.com;
38+
frame-src ''self''; object-src ''none''; worker-src ''self'' blob:; child-src
39+
blob:; connect-src ''self'' *.tiles.mapbox.com *.mapbox.com *.litix.io
40+
*.wistia.com embedwistia-a.akamaihd.net matomo.anywhere.gooddata.com;
41+
media-src ''self'' blob: data: *.wistia.com *.wistia.net embedwistia-a.akamaihd.net'
42+
Content-Type:
43+
- application/vnd.gooddata.api+json
44+
Date: &id001
45+
- PLACEHOLDER
46+
Expires:
47+
- '0'
48+
GoodData-Deployment:
49+
- aio
50+
Permission-Policy:
51+
- geolocation 'none'; midi 'none'; sync-xhr 'none'; microphone 'none'; camera
52+
'none'; magnetometer 'none'; gyroscope 'none'; fullscreen 'none'; payment
53+
'none';
54+
Pragma:
55+
- no-cache
56+
Referrer-Policy:
57+
- no-referrer
58+
Server:
59+
- nginx
60+
Set-Cookie:
61+
- SPRING_REDIRECT_URI=; Path=/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00
62+
GMT; HttpOnly; SameSite=Lax
63+
Vary:
64+
- Origin
65+
- Access-Control-Request-Method
66+
- Access-Control-Request-Headers
67+
X-Content-Type-Options:
68+
- nosniff
69+
X-GDC-TRACE-ID: *id001
70+
X-XSS-Protection:
71+
- 1 ; mode=block
72+
body:
73+
string:
74+
data:
75+
id: demo2
76+
type: user
77+
attributes:
78+
authenticationId: CiQ1MjI0OWQwYi1lYmI4LTQ3OWUtOGFiZi1jNzEyNmQzNWUwZTcSBWxvY2Fs
79+
relationships:
80+
userGroups:
81+
data:
82+
- id: demoGroup
83+
type: userGroup
84+
included:
85+
- id: demoGroup
86+
type: userGroup
87+
attributes: {}
88+
links:
89+
self: http://localhost:3000/api/v1/entities/userGroups/demoGroup
90+
links:
91+
self: http://localhost:3000/api/v1/entities/users/demo2?include=userGroups
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# (C) 2023 GoodData Corporation
2+
version: 1
3+
interactions:
4+
- request:
5+
method: GET
6+
uri: http://localhost:3000/api/v1/entities/users/demo2?include=userGroups
7+
body: null
8+
headers:
9+
Accept:
10+
- application/vnd.gooddata.api+json
11+
X-GDC-VALIDATE-RELATIONS:
12+
- 'true'
13+
X-Requested-With:
14+
- XMLHttpRequest
15+
response:
16+
status:
17+
code: 200
18+
message: OK
19+
headers:
20+
Access-Control-Allow-Credentials:
21+
- 'true'
22+
Access-Control-Expose-Headers:
23+
- Content-Disposition, Content-Length, Content-Range, Set-Cookie
24+
Cache-Control:
25+
- no-cache, no-store, max-age=0, must-revalidate
26+
Connection:
27+
- keep-alive
28+
Content-Length:
29+
- '447'
30+
Content-Security-Policy:
31+
- 'default-src ''self'' *.wistia.com *.wistia.net; script-src ''self'' ''unsafe-inline''
32+
''unsafe-eval'' *.wistia.com *.wistia.net src.litix.io matomo.anywhere.gooddata.com
33+
code.jquery.com unpkg.com cdn.jsdelivr.net cdnjs.cloudflare.com; img-src
34+
''self'' data: blob: *.wistia.com *.wistia.net embedwistia-a.akamaihd.net
35+
privacy-policy.truste.com www.gooddata.com; style-src ''self'' ''unsafe-inline''
36+
fonts.googleapis.com cdn.jsdelivr.net fast.fonts.net; font-src ''self''
37+
data: fonts.gstatic.com *.alicdn.com *.wistia.com cdn.jsdelivr.net info.gooddata.com;
38+
frame-src ''self''; object-src ''none''; worker-src ''self'' blob:; child-src
39+
blob:; connect-src ''self'' *.tiles.mapbox.com *.mapbox.com *.litix.io
40+
*.wistia.com embedwistia-a.akamaihd.net matomo.anywhere.gooddata.com;
41+
media-src ''self'' blob: data: *.wistia.com *.wistia.net embedwistia-a.akamaihd.net'
42+
Content-Type:
43+
- application/vnd.gooddata.api+json
44+
Date: &id001
45+
- PLACEHOLDER
46+
Expires:
47+
- '0'
48+
GoodData-Deployment:
49+
- aio
50+
Permission-Policy:
51+
- geolocation 'none'; midi 'none'; sync-xhr 'none'; microphone 'none'; camera
52+
'none'; magnetometer 'none'; gyroscope 'none'; fullscreen 'none'; payment
53+
'none';
54+
Pragma:
55+
- no-cache
56+
Referrer-Policy:
57+
- no-referrer
58+
Server:
59+
- nginx
60+
Set-Cookie:
61+
- SPRING_REDIRECT_URI=; Path=/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00
62+
GMT; HttpOnly; SameSite=Lax
63+
Vary:
64+
- Origin
65+
- Access-Control-Request-Method
66+
- Access-Control-Request-Headers
67+
X-Content-Type-Options:
68+
- nosniff
69+
X-GDC-TRACE-ID: *id001
70+
X-XSS-Protection:
71+
- 1 ; mode=block
72+
body:
73+
string:
74+
data:
75+
id: demo2
76+
type: user
77+
attributes:
78+
authenticationId: CiQ1MjI0OWQwYi1lYmI4LTQ3OWUtOGFiZi1jNzEyNmQzNWUwZTcSBWxvY2Fs
79+
relationships:
80+
userGroups:
81+
data:
82+
- id: demoGroup
83+
type: userGroup
84+
included:
85+
- id: demoGroup
86+
type: userGroup
87+
attributes: {}
88+
links:
89+
self: http://localhost:3000/api/v1/entities/userGroups/demoGroup
90+
links:
91+
self: http://localhost:3000/api/v1/entities/users/demo2?include=userGroups
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# (C) 2023 GoodData Corporation
2+
version: 1
3+
interactions:
4+
- request:
5+
method: GET
6+
uri: http://localhost:3000/api/v1/entities/users/demo2?include=userGroups
7+
body: null
8+
headers:
9+
Accept:
10+
- application/vnd.gooddata.api+json
11+
X-GDC-VALIDATE-RELATIONS:
12+
- 'true'
13+
X-Requested-With:
14+
- XMLHttpRequest
15+
response:
16+
status:
17+
code: 200
18+
message: OK
19+
headers:
20+
Access-Control-Allow-Credentials:
21+
- 'true'
22+
Access-Control-Expose-Headers:
23+
- Content-Disposition, Content-Length, Content-Range, Set-Cookie
24+
Cache-Control:
25+
- no-cache, no-store, max-age=0, must-revalidate
26+
Connection:
27+
- keep-alive
28+
Content-Length:
29+
- '447'
30+
Content-Security-Policy:
31+
- 'default-src ''self'' *.wistia.com *.wistia.net; script-src ''self'' ''unsafe-inline''
32+
''unsafe-eval'' *.wistia.com *.wistia.net src.litix.io matomo.anywhere.gooddata.com
33+
code.jquery.com unpkg.com cdn.jsdelivr.net cdnjs.cloudflare.com; img-src
34+
''self'' data: blob: *.wistia.com *.wistia.net embedwistia-a.akamaihd.net
35+
privacy-policy.truste.com www.gooddata.com; style-src ''self'' ''unsafe-inline''
36+
fonts.googleapis.com cdn.jsdelivr.net fast.fonts.net; font-src ''self''
37+
data: fonts.gstatic.com *.alicdn.com *.wistia.com cdn.jsdelivr.net info.gooddata.com;
38+
frame-src ''self''; object-src ''none''; worker-src ''self'' blob:; child-src
39+
blob:; connect-src ''self'' *.tiles.mapbox.com *.mapbox.com *.litix.io
40+
*.wistia.com embedwistia-a.akamaihd.net matomo.anywhere.gooddata.com;
41+
media-src ''self'' blob: data: *.wistia.com *.wistia.net embedwistia-a.akamaihd.net'
42+
Content-Type:
43+
- application/vnd.gooddata.api+json
44+
Date: &id001
45+
- PLACEHOLDER
46+
Expires:
47+
- '0'
48+
GoodData-Deployment:
49+
- aio
50+
Permission-Policy:
51+
- geolocation 'none'; midi 'none'; sync-xhr 'none'; microphone 'none'; camera
52+
'none'; magnetometer 'none'; gyroscope 'none'; fullscreen 'none'; payment
53+
'none';
54+
Pragma:
55+
- no-cache
56+
Referrer-Policy:
57+
- no-referrer
58+
Server:
59+
- nginx
60+
Set-Cookie:
61+
- SPRING_REDIRECT_URI=; Path=/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00
62+
GMT; HttpOnly; SameSite=Lax
63+
Vary:
64+
- Origin
65+
- Access-Control-Request-Method
66+
- Access-Control-Request-Headers
67+
X-Content-Type-Options:
68+
- nosniff
69+
X-GDC-TRACE-ID: *id001
70+
X-XSS-Protection:
71+
- 1 ; mode=block
72+
body:
73+
string:
74+
data:
75+
id: demo2
76+
type: user
77+
attributes:
78+
authenticationId: CiQ1MjI0OWQwYi1lYmI4LTQ3OWUtOGFiZi1jNzEyNmQzNWUwZTcSBWxvY2Fs
79+
relationships:
80+
userGroups:
81+
data:
82+
- id: demoGroup
83+
type: userGroup
84+
included:
85+
- id: demoGroup
86+
type: userGroup
87+
attributes: {}
88+
links:
89+
self: http://localhost:3000/api/v1/entities/userGroups/demoGroup
90+
links:
91+
self: http://localhost:3000/api/v1/entities/users/demo2?include=userGroups

0 commit comments

Comments
 (0)