Skip to content

Commit 16d50cd

Browse files
author
Gauvain Pocentek
committed
Add support for application settings
1 parent e5438c6 commit 16d50cd

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed

gitlab/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def __init__(self, url, private_token=None,
122122
#: Whether SSL certificates should be validated
123123
self.ssl_verify = ssl_verify
124124

125+
self.settings = ApplicationSettingsManager(self)
125126
self.user_keys = UserKeyManager(self)
126127
self.users = UserManager(self)
127128
self.group_members = GroupMemberManager(self)
@@ -556,7 +557,7 @@ def update(self, obj, **kwargs):
556557
headers = self._create_headers(content_type="application/json")
557558

558559
# build data that can really be sent to server
559-
data = obj._data_for_gitlab(extra_parameters=kwargs)
560+
data = obj._data_for_gitlab(extra_parameters=kwargs, update=True)
560561

561562
try:
562563
r = requests.put(url, data=data,

gitlab/objects.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ class GitlabObject(object):
207207
#: Attributes that are optional when creating a new object.
208208
optionalCreateAttrs = []
209209
#: Attributes that are required when updating an object.
210-
requiredUpdateAttrs = None
210+
requiredUpdateAttrs = []
211211
#: Attributes that are optional when updating an object.
212-
optionalUpdateAttrs = None
212+
optionalUpdateAttrs = []
213213
#: Whether the object ID is required in the GET url.
214214
getRequiresId = True
215215
#: List of managers to create.
@@ -219,10 +219,15 @@ class GitlabObject(object):
219219
#: Attribute to use as ID when displaying the object.
220220
shortPrintAttr = None
221221

222-
def _data_for_gitlab(self, extra_parameters={}):
222+
def _data_for_gitlab(self, extra_parameters={}, update=False):
223223
data = {}
224-
for attribute in itertools.chain(self.requiredCreateAttrs,
225-
self.optionalCreateAttrs):
224+
if update and (self.requiredUpdateAttrs or self.optionalUpdateAttrs):
225+
attributes = itertools.chain(self.requiredUpdateAttrs,
226+
self.optionalUpdateAttrs)
227+
else:
228+
attributes = itertools.chain(self.requiredCreateAttrs,
229+
self.optionalCreateAttrs)
230+
for attribute in attributes:
226231
if hasattr(self, attribute):
227232
data[attribute] = getattr(self, attribute)
228233

@@ -506,7 +511,7 @@ class User(GitlabObject):
506511
'confirm']
507512
managers = [('keys', UserKeyManager, [('user_id', 'id')])]
508513

509-
def _data_for_gitlab(self, extra_parameters={}):
514+
def _data_for_gitlab(self, extra_parameters={}, update=False):
510515
if hasattr(self, 'confirm'):
511516
self.confirm = str(self.confirm).lower()
512517
return super(User, self)._data_for_gitlab(extra_parameters)
@@ -549,6 +554,28 @@ def Key(self, id=None, **kwargs):
549554
return CurrentUserKey._get_list_or_object(self.gitlab, id, **kwargs)
550555

551556

557+
class ApplicationSettings(GitlabObject):
558+
_url = '/application/settings'
559+
_id_in_update_url = False
560+
optionalUpdateAttrs = ['after_sign_out_path', 'default_branch_protection',
561+
'default_project_visibility',
562+
'default_projects_limit',
563+
'default_snippet_visibility', 'gravatar_enabled',
564+
'home_page_url', 'restricted_signup_domains',
565+
'restricted_visibility_levels',
566+
'session_expire_delay', 'sign_in_text',
567+
'signin_enabled', 'signup_enabled',
568+
'twitter_sharing_enabled',
569+
'user_oauth_applications']
570+
canList = False
571+
canCreate = False
572+
canDelete = False
573+
574+
575+
class ApplicationSettingsManager(BaseManager):
576+
obj_cls = ApplicationSettings
577+
578+
552579
class GroupMember(GitlabObject):
553580
_url = '/groups/%(group_id)s/members'
554581
canGet = 'from_list'
@@ -784,7 +811,7 @@ class ProjectIssue(GitlabObject):
784811
managers = [('notes', ProjectIssueNoteManager,
785812
[('project_id', 'project_id'), ('issue_id', 'id')])]
786813

787-
def _data_for_gitlab(self, extra_parameters={}):
814+
def _data_for_gitlab(self, extra_parameters={}, update=False):
788815
# Gitlab-api returns labels in a json list and takes them in a
789816
# comma separated list.
790817
if hasattr(self, "labels"):

gitlab/tests/test_gitlabobject.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,45 @@ def test_json(self):
159159
self.assertEqual(data["username"], "testname")
160160
self.assertEqual(data["gitlab"]["url"], "http://localhost/api/v3")
161161

162+
def test_data_for_gitlab(self):
163+
class FakeObj1(GitlabObject):
164+
_url = '/fake1'
165+
requiredCreateAttrs = ['create_req']
166+
optionalCreateAttrs = ['create_opt']
167+
requiredUpdateAttrs = ['update_req']
168+
optionalUpdateAttrs = ['update_opt']
169+
170+
class FakeObj2(GitlabObject):
171+
_url = '/fake2'
172+
requiredCreateAttrs = ['create_req']
173+
optionalCreateAttrs = ['create_opt']
174+
175+
obj1 = FakeObj1(self.gl, {'update_req': 1, 'update_opt': 1,
176+
'create_req': 1, 'create_opt': 1})
177+
obj2 = FakeObj2(self.gl, {'create_req': 1, 'create_opt': 1})
178+
179+
obj1_data = json.loads(obj1._data_for_gitlab())
180+
self.assertIn('create_req', obj1_data)
181+
self.assertIn('create_opt', obj1_data)
182+
self.assertNotIn('update_req', obj1_data)
183+
self.assertNotIn('update_opt', obj1_data)
184+
self.assertNotIn('gitlab', obj1_data)
185+
186+
obj1_data = json.loads(obj1._data_for_gitlab(update=True))
187+
self.assertNotIn('create_req', obj1_data)
188+
self.assertNotIn('create_opt', obj1_data)
189+
self.assertIn('update_req', obj1_data)
190+
self.assertIn('update_opt', obj1_data)
191+
192+
obj1_data = json.loads(obj1._data_for_gitlab(
193+
extra_parameters={'foo': 'bar'}))
194+
self.assertIn('foo', obj1_data)
195+
self.assertEqual(obj1_data['foo'], 'bar')
196+
197+
obj2_data = json.loads(obj2._data_for_gitlab(update=True))
198+
self.assertIn('create_req', obj2_data)
199+
self.assertIn('create_opt', obj2_data)
200+
162201
def test_list_not_implemented(self):
163202
self.assertRaises(NotImplementedError, CurrentUser.list, self.gl)
164203

tools/python_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
gl.auth()
2424
assert(isinstance(gl.user, gitlab.objects.CurrentUser))
2525

26+
# settings
27+
settings = gl.settings.get()
28+
settings.default_projects_limit = 42
29+
settings.save()
30+
settings = gl.settings.get()
31+
assert(settings.default_projects_limit == 42)
32+
2633
# user manipulations
2734
new_user = gl.users.create({'email': 'foo@bar.com', 'username': 'foo',
2835
'name': 'foo', 'password': 'foo_password'})

0 commit comments

Comments
 (0)