summaryrefslogtreecommitdiff
path: root/gitlab
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain.pocentek@objectif-libre.com>2016-01-27 21:54:05 +0100
committerGauvain Pocentek <gauvain.pocentek@objectif-libre.com>2016-01-27 21:54:05 +0100
commit16d50cd5d52617d9117409ccc9819d8429088e84 (patch)
tree23c1a9c651aad411e443a97f2a3e4c4521ac69a9 /gitlab
parente5438c6440a2477c796427bc598b2b31b10dc762 (diff)
downloadgitlab-16d50cd5d52617d9117409ccc9819d8429088e84.tar.gz
Add support for application settings
Diffstat (limited to 'gitlab')
-rw-r--r--gitlab/__init__.py3
-rw-r--r--gitlab/objects.py41
-rw-r--r--gitlab/tests/test_gitlabobject.py39
3 files changed, 75 insertions, 8 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index 28ebfe3..24d1882 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -122,6 +122,7 @@ class Gitlab(object):
#: 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 @@ class Gitlab(object):
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 0330807..4d19619 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 @@ class CurrentUser(GitlabObject):
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 2726854..e001a8c 100644
--- a/gitlab/tests/test_gitlabobject.py
+++ b/gitlab/tests/test_gitlabobject.py
@@ -159,6 +159,45 @@ class TestGitlabObject(unittest.TestCase):
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)