From 16d50cd5d52617d9117409ccc9819d8429088e84 Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Wed, 27 Jan 2016 21:54:05 +0100 Subject: [PATCH] Add support for application settings --- gitlab/__init__.py | 3 ++- gitlab/objects.py | 41 +++++++++++++++++++++++++------ gitlab/tests/test_gitlabobject.py | 39 +++++++++++++++++++++++++++++ tools/python_test.py | 7 ++++++ 4 files changed, 82 insertions(+), 8 deletions(-) diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 28ebfe349..24d1882fb 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -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) @@ -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, diff --git a/gitlab/objects.py b/gitlab/objects.py index 0330807e2..4d1961915 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -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. @@ -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) @@ -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) @@ -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' @@ -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"): diff --git a/gitlab/tests/test_gitlabobject.py b/gitlab/tests/test_gitlabobject.py index 27268540c..e001a8c80 100644 --- a/gitlab/tests/test_gitlabobject.py +++ b/gitlab/tests/test_gitlabobject.py @@ -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) diff --git a/tools/python_test.py b/tools/python_test.py index ff4aa2a15..5292ed9b2 100644 --- a/tools/python_test.py +++ b/tools/python_test.py @@ -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'})