Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita1b097c

Browse files
author
Gauvain Pocentek
committed
Add a SetMixin
Use it for UserCustomAttribute, will be useful for{Project,Group}CustomAttribute (#367)
1 parent397d677 commita1b097c

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

‎gitlab/mixins.py

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

224224

225+
classSetMixin(object):
226+
@exc.on_http_error(exc.GitlabSetError)
227+
defset(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+
returnself._obj_cls(self,server_data)
246+
247+
225248
classDeleteMixin(object):
226249
@exc.on_http_error(exc.GitlabDeleteError)
227250
defdelete(self,id,**kwargs):

‎gitlab/tests/test_mixins.py

Lines changed: 25 additions & 0 deletions
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+
deftest_set_mixin(self):
70+
classO(SetMixin):
71+
pass
72+
73+
obj=O()
74+
self.assertTrue(hasattr(obj,'set'))
75+
6976

7077
classTestMetaMixins(unittest.TestCase):
7178
deftest_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+
deftest_set_mixin(self):
421+
classM(SetMixin,FakeManager):
422+
pass
423+
424+
@urlmatch(scheme="http",netloc="localhost",path='/api/v4/tests/foo',
425+
method="put")
426+
defresp_cont(url,request):
427+
headers= {'Content-Type':'application/json'}
428+
content='{"key": "foo", "value": "bar"}'
429+
returnresponse(200,content,headers,None,5,request)
430+
431+
withHTTMock(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

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

127127

128-
classUserCustomAttributeManager(RetrieveMixin,DeleteMixin,RESTManager):
128+
classUserCustomAttributeManager(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-
defset(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-
returnself._obj_cls(self,server_data)
152-
153134

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp