Skip to content

Commit 1d852bc

Browse files
committed
Improve "Organization preferences"
- Add model-based conversation, using `UserOrganizationPreferences`. - Add `organization_preferences_patch`. - Deprecate `organization_preference_get` and `organization_preference_update` in favor of `organization_preferences_get` and `organization_preferences_update` (singular vs. plural).
1 parent 6e1c5c8 commit 1d852bc

File tree

3 files changed

+107
-8
lines changed

3 files changed

+107
-8
lines changed

grafana_client/elements/organization.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import warnings
2+
3+
from ..model import UserOrganizationPreferences
14
from .base import Base
25

36

@@ -180,25 +183,68 @@ def organization_preference_get(self):
180183
"""
181184
:return:
182185
"""
183-
update_preference = "/org/preferences"
184-
r = self.client.GET(update_preference)
185-
return r
186+
warnings.warn("Deprecated, please use `organization_preferences_get`", DeprecationWarning)
187+
return self.organization_preferences_get()
186188

187189
def organization_preference_update(self, theme="", home_dashboard_id=0, timezone="utc"):
188190
"""
189191
190192
:param theme:
191193
:param home_dashboard_id:
192194
:param timezone:
195+
:return:
196+
"""
197+
warnings.warn("Deprecated, please use `organization_preferences_update`", DeprecationWarning)
198+
preferences = UserOrganizationPreferences(theme=theme, homeDashboardId=home_dashboard_id, timezone=timezone)
199+
return self.organization_preferences_update(preferences)
200+
201+
def organization_preferences_get(self):
202+
"""
203+
Retrieve preferences of current organization.
204+
193205
:return:
194206
"""
195207
update_preference = "/org/preferences"
208+
r = self.client.GET(update_preference)
209+
return r
210+
211+
def organization_preferences_update(self, preferences: UserOrganizationPreferences):
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 `organization_preferences_patch` method.
220+
221+
:param preferences:
222+
:return:
223+
"""
224+
update_preference = "/org/preferences"
225+
data = preferences.asdict(filter_none=True)
226+
196227
r = self.client.PUT(
197228
update_preference,
198-
json={
199-
"theme": theme,
200-
"homeDashboardId": home_dashboard_id,
201-
"timezone": timezone,
202-
},
229+
json=data,
230+
)
231+
return r
232+
233+
def organization_preferences_patch(self, preferences: UserOrganizationPreferences):
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,
203249
)
204250
return r

grafana_client/model.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,32 @@ def asdict_compact(self):
6767
data = self.asdict()
6868
del data["response"]
6969
return data
70+
71+
72+
@dataclasses.dataclass
73+
class UserOrganizationPreferences:
74+
"""
75+
Request/response model for user- and organization-preferences.
76+
77+
https://grafana.com/docs/grafana/latest/developers/http_api/preferences/
78+
"""
79+
80+
homeDashboardId: Optional[int] = None
81+
homeDashboardUID: Optional[str] = None
82+
locale: Optional[str] = None
83+
theme: Optional[str] = None
84+
timezone: Optional[str] = None
85+
weekStart: Optional[str] = None
86+
87+
def asdict(self, filter_none=False):
88+
if filter_none:
89+
return dataclasses.asdict(self, dict_factory=self.dict_factory_filter_none)
90+
else:
91+
return dataclasses.asdict(self)
92+
93+
@staticmethod
94+
def dict_factory_filter_none(seq=None, **kwargs):
95+
seq = [item for item in seq if item[1] is not None]
96+
data = dict(seq)
97+
data.update(kwargs)
98+
return data

test/elements/test_organization.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import requests_mock
44

55
from grafana_client import GrafanaApi
6+
from grafana_client.model import UserOrganizationPreferences
67

78

89
class OrganizationTestCase(unittest.TestCase):
@@ -30,6 +31,29 @@ def test_organization_preference_update(self, m):
3031
)
3132
self.assertEqual(preference["message"], "Preferences updated")
3233

34+
@requests_mock.Mocker()
35+
def test_organization_preferences_get(self, m):
36+
m.get("http://localhost/api/org/preferences", json={"theme": "", "homeDashboardId": 0, "timezone": ""})
37+
38+
result = self.grafana.organizations.organization_preferences_get()
39+
self.assertEqual(result["homeDashboardId"], 0)
40+
41+
@requests_mock.Mocker()
42+
def test_organization_preferences_update(self, m):
43+
m.put("http://localhost/api/org/preferences", json={"message": "Preferences updated"})
44+
preference = self.grafana.organizations.organization_preferences_update(
45+
UserOrganizationPreferences(theme="", homeDashboardId=999, timezone="utc")
46+
)
47+
self.assertEqual(preference["message"], "Preferences updated")
48+
49+
@requests_mock.Mocker()
50+
def test_organization_preferences_patch(self, m):
51+
m.patch("http://localhost/api/org/preferences", json={"message": "Preferences updated"})
52+
preference = self.grafana.organizations.organization_preferences_patch(
53+
UserOrganizationPreferences(homeDashboardUID="zgjG8dKVz")
54+
)
55+
self.assertEqual(preference["message"], "Preferences updated")
56+
3357
@requests_mock.Mocker()
3458
def test_organization_user_update(self, m):
3559
m.patch("http://localhost/api/orgs/1/users/2", json={"message": "Organization user updated"})

0 commit comments

Comments
 (0)