summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/gl_objects/users.py18
-rw-r--r--docs/gl_objects/users.rst36
-rw-r--r--gitlab/exceptions.py4
-rw-r--r--gitlab/v4/objects.py31
-rw-r--r--tools/python_test_v4.py14
5 files changed, 103 insertions, 0 deletions
diff --git a/docs/gl_objects/users.py b/docs/gl_objects/users.py
index c3618b9..da516e6 100644
--- a/docs/gl_objects/users.py
+++ b/docs/gl_objects/users.py
@@ -97,3 +97,21 @@ email.delete()
gl.auth()
current_user = gl.user
# end currentuser get
+
+# ca list
+attrs = user.customeattributes.list()
+# end ca list
+
+# ca get
+attr = user.customeattributes.get(attr_key)
+# end ca get
+
+# ca set
+attr = user.customeattributes.set(attr_key, attr_value)
+# end ca set
+
+# ca delete
+attr.delete()
+# or
+user.customeattributes.delete(attr_key)
+# end ca delete
diff --git a/docs/gl_objects/users.rst b/docs/gl_objects/users.rst
index d5b2976..4e22491 100644
--- a/docs/gl_objects/users.rst
+++ b/docs/gl_objects/users.rst
@@ -70,6 +70,42 @@ Block/Unblock a user:
:start-after: # block
:end-before: # end block
+User custom attributes
+======================
+
+References
+----------
+
+* v4 API:
+
+ + :class:`gitlab.v4.objects.UserCustomAttribute`
+ + :class:`gitlab.v4.objects.UserCustomAttributeManager`
+ + :attr:`gitlab.v4.objects.User.customattributes`
+
+List custom attributes for a user:
+
+.. literalinclude:: users.py
+ :start-after: # ca list
+ :end-before: # end ca list
+
+Get a custom attribute for a user:
+
+.. literalinclude:: users.py
+ :start-after: # ca get
+ :end-before: # end ca get
+
+Set (create or update) a custom attribute for a user:
+
+.. literalinclude:: users.py
+ :start-after: # ca set
+ :end-before: # end ca set
+
+Delete a custom attribute for a user:
+
+.. literalinclude:: users.py
+ :start-after: # ca list
+ :end-before: # end ca list
+
Current User
============
diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py
index a100395..d95bb08 100644
--- a/gitlab/exceptions.py
+++ b/gitlab/exceptions.py
@@ -77,6 +77,10 @@ class GitlabDeleteError(GitlabOperationError):
pass
+class GitlabSetError(GitlabOperationError):
+ pass
+
+
class GitlabProtectError(GitlabOperationError):
pass
diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py
index 5a3f17c..6d7512b 100644
--- a/gitlab/v4/objects.py
+++ b/gitlab/v4/objects.py
@@ -112,6 +112,36 @@ class SidekiqManager(RESTManager):
return self.gitlab.http_get('/sidekiq/compound_metrics', **kwargs)
+class UserCustomAttribute(ObjectDeleteMixin, RESTObject):
+ _id_attr = 'key'
+
+
+class UserCustomAttributeManager(RetrieveMixin, DeleteMixin, RESTManager):
+ _path = '/users/%(user_id)s/custom_attributes'
+ _obj_cls = UserCustomAttribute
+ _from_parent_attrs = {'user_id': 'id'}
+
+ def set(self, key, value, **kwargs):
+ """Create or update a user attribute.
+
+ Args:
+ key (str): The attribute to update
+ value (str): The value to set
+ **kwargs: Extra options to send to the server (e.g. sudo)
+
+ Raises:
+ GitlabAuthenticationError: If authentication is not correct
+ GitlabSetError: If an error occured
+
+ Returns:
+ UserCustomAttribute: The created/updated user attribute
+ """
+ path = '%s/%s' % (self.path, key.replace('/', '%2F'))
+ data = {'value': value}
+ server_data = self.gitlab.http_put(path, post_data=data, **kwargs)
+ return self._obj_cls(self, server_data)
+
+
class UserEmail(ObjectDeleteMixin, RESTObject):
_short_print_attr = 'email'
@@ -165,6 +195,7 @@ class UserProjectManager(CreateMixin, RESTManager):
class User(SaveMixin, ObjectDeleteMixin, RESTObject):
_short_print_attr = 'username'
_managers = (
+ ('customattributes', 'UserCustomAttributeManager'),
('emails', 'UserEmailManager'),
('gpgkeys', 'UserGPGKeyManager'),
('keys', 'UserKeyManager'),
diff --git a/tools/python_test_v4.py b/tools/python_test_v4.py
index 0b1793a..fa83228 100644
--- a/tools/python_test_v4.py
+++ b/tools/python_test_v4.py
@@ -131,6 +131,20 @@ assert(len(new_user.emails.list()) == 1)
email.delete()
assert(len(new_user.emails.list()) == 0)
+# custom attributes
+attrs = new_user.customattributes.list()
+assert(len(attrs) == 0)
+attr = new_user.customattributes.set('key', 'value1')
+assert(attr.key == 'key')
+assert(attr.value == 'value1')
+assert(len(new_user.customattributes.list()) == 1)
+attr = new_user.customattributes.set('key', 'value2')
+attr = new_user.customattributes.get('key')
+assert(attr.value == 'value2')
+assert(len(new_user.customattributes.list()) == 1)
+attr.delete()
+assert(len(new_user.customattributes.list()) == 0)
+
new_user.delete()
foobar_user.delete()
assert(len(gl.users.list()) == 3)