Skip to content

Commit

Permalink
Add support for application settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauvain Pocentek committed Jan 27, 2016
1 parent e5438c6 commit 16d50cd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
3 changes: 2 additions & 1 deletion gitlab/__init__.py
Expand Up @@ -122,6 +122,7 @@ def __init__(self, url, private_token=None,
#: Whether SSL certificates should be validated
self.ssl_verify = ssl_verify

self.settings = ApplicationSettingsManager(self)
self.user_keys = UserKeyManager(self)
self.users = UserManager(self)
self.group_members = GroupMemberManager(self)
Expand Down Expand Up @@ -556,7 +557,7 @@ def update(self, obj, **kwargs):
headers = self._create_headers(content_type="application/json")

# build data that can really be sent to server
data = obj._data_for_gitlab(extra_parameters=kwargs)
data = obj._data_for_gitlab(extra_parameters=kwargs, update=True)

try:
r = requests.put(url, data=data,
Expand Down
41 changes: 34 additions & 7 deletions gitlab/objects.py
Expand Up @@ -207,9 +207,9 @@ class GitlabObject(object):
#: Attributes that are optional when creating a new object.
optionalCreateAttrs = []
#: Attributes that are required when updating an object.
requiredUpdateAttrs = None
requiredUpdateAttrs = []
#: Attributes that are optional when updating an object.
optionalUpdateAttrs = None
optionalUpdateAttrs = []
#: Whether the object ID is required in the GET url.
getRequiresId = True
#: List of managers to create.
Expand All @@ -219,10 +219,15 @@ class GitlabObject(object):
#: Attribute to use as ID when displaying the object.
shortPrintAttr = None

def _data_for_gitlab(self, extra_parameters={}):
def _data_for_gitlab(self, extra_parameters={}, update=False):
data = {}
for attribute in itertools.chain(self.requiredCreateAttrs,
self.optionalCreateAttrs):
if update and (self.requiredUpdateAttrs or self.optionalUpdateAttrs):
attributes = itertools.chain(self.requiredUpdateAttrs,
self.optionalUpdateAttrs)
else:
attributes = itertools.chain(self.requiredCreateAttrs,
self.optionalCreateAttrs)
for attribute in attributes:
if hasattr(self, attribute):
data[attribute] = getattr(self, attribute)

Expand Down Expand Up @@ -506,7 +511,7 @@ class User(GitlabObject):
'confirm']
managers = [('keys', UserKeyManager, [('user_id', 'id')])]

def _data_for_gitlab(self, extra_parameters={}):
def _data_for_gitlab(self, extra_parameters={}, update=False):
if hasattr(self, 'confirm'):
self.confirm = str(self.confirm).lower()
return super(User, self)._data_for_gitlab(extra_parameters)
Expand Down Expand Up @@ -549,6 +554,28 @@ def Key(self, id=None, **kwargs):
return CurrentUserKey._get_list_or_object(self.gitlab, id, **kwargs)


class ApplicationSettings(GitlabObject):
_url = '/application/settings'
_id_in_update_url = False
optionalUpdateAttrs = ['after_sign_out_path', 'default_branch_protection',
'default_project_visibility',
'default_projects_limit',
'default_snippet_visibility', 'gravatar_enabled',
'home_page_url', 'restricted_signup_domains',
'restricted_visibility_levels',
'session_expire_delay', 'sign_in_text',
'signin_enabled', 'signup_enabled',
'twitter_sharing_enabled',
'user_oauth_applications']
canList = False
canCreate = False
canDelete = False


class ApplicationSettingsManager(BaseManager):
obj_cls = ApplicationSettings


class GroupMember(GitlabObject):
_url = '/groups/%(group_id)s/members'
canGet = 'from_list'
Expand Down Expand Up @@ -784,7 +811,7 @@ class ProjectIssue(GitlabObject):
managers = [('notes', ProjectIssueNoteManager,
[('project_id', 'project_id'), ('issue_id', 'id')])]

def _data_for_gitlab(self, extra_parameters={}):
def _data_for_gitlab(self, extra_parameters={}, update=False):
# Gitlab-api returns labels in a json list and takes them in a
# comma separated list.
if hasattr(self, "labels"):
Expand Down
39 changes: 39 additions & 0 deletions gitlab/tests/test_gitlabobject.py
Expand Up @@ -159,6 +159,45 @@ def test_json(self):
self.assertEqual(data["username"], "testname")
self.assertEqual(data["gitlab"]["url"], "http://localhost/api/v3")

def test_data_for_gitlab(self):
class FakeObj1(GitlabObject):
_url = '/fake1'
requiredCreateAttrs = ['create_req']
optionalCreateAttrs = ['create_opt']
requiredUpdateAttrs = ['update_req']
optionalUpdateAttrs = ['update_opt']

class FakeObj2(GitlabObject):
_url = '/fake2'
requiredCreateAttrs = ['create_req']
optionalCreateAttrs = ['create_opt']

obj1 = FakeObj1(self.gl, {'update_req': 1, 'update_opt': 1,
'create_req': 1, 'create_opt': 1})
obj2 = FakeObj2(self.gl, {'create_req': 1, 'create_opt': 1})

obj1_data = json.loads(obj1._data_for_gitlab())
self.assertIn('create_req', obj1_data)
self.assertIn('create_opt', obj1_data)
self.assertNotIn('update_req', obj1_data)
self.assertNotIn('update_opt', obj1_data)
self.assertNotIn('gitlab', obj1_data)

obj1_data = json.loads(obj1._data_for_gitlab(update=True))
self.assertNotIn('create_req', obj1_data)
self.assertNotIn('create_opt', obj1_data)
self.assertIn('update_req', obj1_data)
self.assertIn('update_opt', obj1_data)

obj1_data = json.loads(obj1._data_for_gitlab(
extra_parameters={'foo': 'bar'}))
self.assertIn('foo', obj1_data)
self.assertEqual(obj1_data['foo'], 'bar')

obj2_data = json.loads(obj2._data_for_gitlab(update=True))
self.assertIn('create_req', obj2_data)
self.assertIn('create_opt', obj2_data)

def test_list_not_implemented(self):
self.assertRaises(NotImplementedError, CurrentUser.list, self.gl)

Expand Down
7 changes: 7 additions & 0 deletions tools/python_test.py
Expand Up @@ -23,6 +23,13 @@
gl.auth()
assert(isinstance(gl.user, gitlab.objects.CurrentUser))

# settings
settings = gl.settings.get()
settings.default_projects_limit = 42
settings.save()
settings = gl.settings.get()
assert(settings.default_projects_limit == 42)

# user manipulations
new_user = gl.users.create({'email': 'foo@bar.com', 'username': 'foo',
'name': 'foo', 'password': 'foo_password'})
Expand Down

0 comments on commit 16d50cd

Please sign in to comment.