Skip to content

Commit a1b097c

Browse files
author
Gauvain Pocentek
committed
Add a SetMixin
Use it for UserCustomAttribute, will be useful for {Project,Group}CustomAttribute (#367)
1 parent 397d677 commit a1b097c

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

gitlab/mixins.py

+23
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,29 @@ def update(self, id=None, new_data={}, **kwargs):
222222
return self.gitlab.http_put(path, post_data=data, **kwargs)
223223

224224

225+
class SetMixin(object):
226+
@exc.on_http_error(exc.GitlabSetError)
227+
def set(self, key, value, **kwargs):
228+
"""Create or update the object.
229+
230+
Args:
231+
key (str): The key of the object to create/update
232+
value (str): The value to set for the object
233+
**kwargs: Extra options to send to the server (e.g. sudo)
234+
235+
Raises:
236+
GitlabAuthenticationError: If authentication is not correct
237+
GitlabSetError: If an error occured
238+
239+
Returns:
240+
UserCustomAttribute: The created/updated user attribute
241+
"""
242+
path = '%s/%s' % (self.path, key.replace('/', '%2F'))
243+
data = {'value': value}
244+
server_data = self.gitlab.http_put(path, post_data=data, **kwargs)
245+
return self._obj_cls(self, server_data)
246+
247+
225248
class DeleteMixin(object):
226249
@exc.on_http_error(exc.GitlabDeleteError)
227250
def delete(self, id, **kwargs):

gitlab/tests/test_mixins.py

+25
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ class O(TimeTrackingMixin):
6666
self.assertTrue(hasattr(obj, 'add_spent_time'))
6767
self.assertTrue(hasattr(obj, 'reset_spent_time'))
6868

69+
def test_set_mixin(self):
70+
class O(SetMixin):
71+
pass
72+
73+
obj = O()
74+
self.assertTrue(hasattr(obj, 'set'))
75+
6976

7077
class TestMetaMixins(unittest.TestCase):
7178
def test_retrieve_mixin(self):
@@ -409,3 +416,21 @@ def resp_cont(url, request):
409416
obj.save()
410417
self.assertEqual(obj._attrs['foo'], 'baz')
411418
self.assertDictEqual(obj._updated_attrs, {})
419+
420+
def test_set_mixin(self):
421+
class M(SetMixin, FakeManager):
422+
pass
423+
424+
@urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests/foo',
425+
method="put")
426+
def resp_cont(url, request):
427+
headers = {'Content-Type': 'application/json'}
428+
content = '{"key": "foo", "value": "bar"}'
429+
return response(200, content, headers, None, 5, request)
430+
431+
with HTTMock(resp_cont):
432+
mgr = M(self.gl)
433+
obj = mgr.set('foo', 'bar')
434+
self.assertIsInstance(obj, FakeObject)
435+
self.assertEqual(obj.key, 'foo')
436+
self.assertEqual(obj.value, 'bar')

gitlab/v4/objects.py

+2-21
Original file line numberDiff line numberDiff line change
@@ -125,31 +125,12 @@ class UserCustomAttribute(ObjectDeleteMixin, RESTObject):
125125
_id_attr = 'key'
126126

127127

128-
class UserCustomAttributeManager(RetrieveMixin, DeleteMixin, RESTManager):
128+
class UserCustomAttributeManager(RetrieveMixin, SetMixin, DeleteMixin,
129+
RESTManager):
129130
_path = '/users/%(user_id)s/custom_attributes'
130131
_obj_cls = UserCustomAttribute
131132
_from_parent_attrs = {'user_id': 'id'}
132133

133-
def set(self, key, value, **kwargs):
134-
"""Create or update a user attribute.
135-
136-
Args:
137-
key (str): The attribute to update
138-
value (str): The value to set
139-
**kwargs: Extra options to send to the server (e.g. sudo)
140-
141-
Raises:
142-
GitlabAuthenticationError: If authentication is not correct
143-
GitlabSetError: If an error occured
144-
145-
Returns:
146-
UserCustomAttribute: The created/updated user attribute
147-
"""
148-
path = '%s/%s' % (self.path, key.replace('/', '%2F'))
149-
data = {'value': value}
150-
server_data = self.gitlab.http_put(path, post_data=data, **kwargs)
151-
return self._obj_cls(self, server_data)
152-
153134

154135
class UserEmail(ObjectDeleteMixin, RESTObject):
155136
_short_print_attr = 'email'

0 commit comments

Comments
 (0)