Skip to content

Commit 5ef5f4c

Browse files
committed
Add support for "User preferences", based on PersonalPreferences model
1 parent be81bee commit 5ef5f4c

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

grafana_client/elements/user.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ..model import PersonalPreferences
12
from .base import Base
23

34

@@ -172,3 +173,54 @@ def unstar_actual_user_dashboard(self, dashboard_id):
172173
unstar_dashboard = "/user/stars/dashboard/%s" % dashboard_id
173174
r = self.client.DELETE(unstar_dashboard)
174175
return r
176+
177+
def get_preferences(self):
178+
"""
179+
Retrieve preferences of current user.
180+
181+
:return:
182+
"""
183+
update_preference = "/user/preferences"
184+
r = self.client.GET(update_preference)
185+
return r
186+
187+
def update_preferences(self, preferences: PersonalPreferences):
188+
"""
189+
Update preferences of current user as a whole.
190+
191+
From the `preferences` instance, only attributes with values `not None` will be submitted.
192+
However, Grafana will reset all undefined attributes to its internal defaults.
193+
194+
If you want to update specific preference attributes, without touching the others,
195+
please use the `patch_preferences` method.
196+
197+
:param preferences:
198+
:return:
199+
"""
200+
update_preference = "/user/preferences"
201+
data = preferences.asdict(filter_none=True)
202+
203+
r = self.client.PUT(
204+
update_preference,
205+
json=data,
206+
)
207+
return r
208+
209+
def patch_preferences(self, preferences: PersonalPreferences):
210+
"""
211+
Update specific preferences of current user.
212+
213+
From the `preferences` instance, only attributes with values `not None` will be submitted
214+
and updated.
215+
216+
:param preferences:
217+
:return:
218+
"""
219+
update_preference = "/user/preferences"
220+
data = preferences.asdict(filter_none=True)
221+
222+
r = self.client.PATCH(
223+
update_preference,
224+
json=data,
225+
)
226+
return r

test/elements/test_user.py

Lines changed: 22 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 PersonalPreferences
67

78

89
class UsersTestCase(unittest.TestCase):
@@ -169,3 +170,24 @@ def test_unstar_actual_user_dashboard(self, m):
169170
)
170171
result = self.grafana.user.unstar_actual_user_dashboard("987vb7t33")
171172
self.assertEqual(result, {})
173+
174+
@requests_mock.Mocker()
175+
def test_get_preferences(self, m):
176+
m.get("http://localhost/api/user/preferences", json={"theme": "", "homeDashboardId": 0, "timezone": ""})
177+
178+
result = self.grafana.user.get_preferences()
179+
self.assertEqual(result["homeDashboardId"], 0)
180+
181+
@requests_mock.Mocker()
182+
def test_update_preferences(self, m):
183+
m.put("http://localhost/api/user/preferences", json={"message": "Preferences updated"})
184+
preference = self.grafana.user.update_preferences(
185+
PersonalPreferences(theme="", homeDashboardId=999, timezone="utc")
186+
)
187+
self.assertEqual(preference["message"], "Preferences updated")
188+
189+
@requests_mock.Mocker()
190+
def test_patch_preferences(self, m):
191+
m.patch("http://localhost/api/user/preferences", json={"message": "Preferences updated"})
192+
preference = self.grafana.user.patch_preferences(PersonalPreferences(homeDashboardUID="zgjG8dKVz"))
193+
self.assertEqual(preference["message"], "Preferences updated")

0 commit comments

Comments
 (0)