Skip to content

Commit 59ab388

Browse files
committed
Improve "Organization preferences"
The methods for retrieving and manipulating preferences have been incorrectly placed within the `organizations` namespace. Now, they are correctly placed within the `organization` namespace.
1 parent 397101d commit 59ab388

File tree

3 files changed

+61
-61
lines changed

3 files changed

+61
-61
lines changed

grafana_client/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(
6666
self.folder = Folder(self.client)
6767
self.health = Health(self.client)
6868
self.organization = Organization(self.client)
69-
self.organizations = Organizations(self.client)
69+
self.organizations = Organizations(self.client, self)
7070
self.search = Search(self.client)
7171
self.user = User(self.client)
7272
self.users = Users(self.client)

grafana_client/elements/organization.py

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,63 @@ def delete_user_current_organization(self, user_id):
8888
r = self.client.DELETE(delete_user_current_organization_path)
8989
return r
9090

91+
def get_preferences(self):
92+
"""
93+
Retrieve preferences of current organization.
94+
95+
:return:
96+
"""
97+
update_preference = "/org/preferences"
98+
r = self.client.GET(update_preference)
99+
return r
100+
101+
def update_preferences(self, preferences: PersonalPreferences):
102+
"""
103+
Update preferences of current organization as a whole.
104+
105+
From the `preferences` instance, only attributes with values `not None` will be submitted.
106+
However, Grafana will reset all undefined attributes to its internal defaults.
107+
108+
If you want to update specific preference attributes, without touching the others,
109+
please use the `patch_preferences` method.
110+
111+
:param preferences:
112+
:return:
113+
"""
114+
update_preference = "/org/preferences"
115+
data = preferences.asdict(filter_none=True)
116+
117+
r = self.client.PUT(
118+
update_preference,
119+
json=data,
120+
)
121+
return r
122+
123+
def patch_preferences(self, preferences: PersonalPreferences):
124+
"""
125+
Update specific preferences of current organization.
126+
127+
From the `preferences` instance, only attributes with values `not None` will be submitted
128+
and updated.
129+
130+
:param preferences:
131+
:return:
132+
"""
133+
update_preference = "/org/preferences"
134+
data = preferences.asdict(filter_none=True)
135+
136+
r = self.client.PATCH(
137+
update_preference,
138+
json=data,
139+
)
140+
return r
141+
91142

92143
class Organizations(Base):
93-
def __init__(self, client):
144+
def __init__(self, client, api):
94145
super(Organizations, self).__init__(client)
95146
self.client = client
96-
self.path = "/users"
147+
self.api = api
97148

98149
def update_organization(self, organization_id, organization):
99150
"""
@@ -183,8 +234,8 @@ def organization_preference_get(self):
183234
"""
184235
:return:
185236
"""
186-
warnings.warn("This method is deprecated, please use `get_preferences`", DeprecationWarning)
187-
return self.get_preferences()
237+
warnings.warn("This method is deprecated, please use `organization.get_preferences`", DeprecationWarning)
238+
return self.api.organization.get_preferences()
188239

189240
def organization_preference_update(self, theme="", home_dashboard_id=0, timezone="utc"):
190241
"""
@@ -194,57 +245,6 @@ def organization_preference_update(self, theme="", home_dashboard_id=0, timezone
194245
:param timezone:
195246
:return:
196247
"""
197-
warnings.warn("This method is deprecated, please use `update_preferences`", DeprecationWarning)
248+
warnings.warn("This method is deprecated, please use `organization.update_preferences`", DeprecationWarning)
198249
preferences = PersonalPreferences(theme=theme, homeDashboardId=home_dashboard_id, timezone=timezone)
199-
return self.update_preferences(preferences)
200-
201-
def get_preferences(self):
202-
"""
203-
Retrieve preferences of current organization.
204-
205-
:return:
206-
"""
207-
update_preference = "/org/preferences"
208-
r = self.client.GET(update_preference)
209-
return r
210-
211-
def update_preferences(self, preferences: PersonalPreferences):
212-
"""
213-
Update preferences of current organization as a whole.
214-
215-
From the `preferences` instance, only attributes with values `not None` will be submitted.
216-
However, Grafana will reset all undefined attributes to its internal defaults.
217-
218-
If you want to update specific preference attributes, without touching the others,
219-
please use the `patch_preferences` method.
220-
221-
:param preferences:
222-
:return:
223-
"""
224-
update_preference = "/org/preferences"
225-
data = preferences.asdict(filter_none=True)
226-
227-
r = self.client.PUT(
228-
update_preference,
229-
json=data,
230-
)
231-
return r
232-
233-
def patch_preferences(self, preferences: PersonalPreferences):
234-
"""
235-
Update specific preferences of current organization.
236-
237-
From the `preferences` instance, only attributes with values `not None` will be submitted
238-
and updated.
239-
240-
:param preferences:
241-
:return:
242-
"""
243-
update_preference = "/org/preferences"
244-
data = preferences.asdict(filter_none=True)
245-
246-
r = self.client.PATCH(
247-
update_preference,
248-
json=data,
249-
)
250-
return r
250+
return self.api.organization.update_preferences(preferences)

test/elements/test_organization.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ def test_organization_preference_update(self, m):
3535
def test_get_preferences(self, m):
3636
m.get("http://localhost/api/org/preferences", json={"theme": "", "homeDashboardId": 0, "timezone": ""})
3737

38-
result = self.grafana.organizations.get_preferences()
38+
result = self.grafana.organization.get_preferences()
3939
self.assertEqual(result["homeDashboardId"], 0)
4040

4141
@requests_mock.Mocker()
4242
def test_update_preferences(self, m):
4343
m.put("http://localhost/api/org/preferences", json={"message": "Preferences updated"})
44-
preference = self.grafana.organizations.update_preferences(
44+
preference = self.grafana.organization.update_preferences(
4545
PersonalPreferences(theme="", homeDashboardId=999, timezone="utc")
4646
)
4747
self.assertEqual(preference["message"], "Preferences updated")
4848

4949
@requests_mock.Mocker()
5050
def test_patch_preferences(self, m):
5151
m.patch("http://localhost/api/org/preferences", json={"message": "Preferences updated"})
52-
preference = self.grafana.organizations.patch_preferences(PersonalPreferences(homeDashboardUID="zgjG8dKVz"))
52+
preference = self.grafana.organization.patch_preferences(PersonalPreferences(homeDashboardUID="zgjG8dKVz"))
5353
self.assertEqual(preference["message"], "Preferences updated")
5454

5555
@requests_mock.Mocker()

0 commit comments

Comments
 (0)