Skip to content

Commit 1fddcd2

Browse files
author
Gauvain Pocentek
committed
Rework object creation from json
1 parent 1d7e85d commit 1fddcd2

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

gitlab.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,21 @@ def delete(cls, gl, id, **kwargs):
193193

194194
return gl.delete(cls, id, **kwargs)
195195

196+
def getObject(self, k, v):
197+
if self.constructorTypes and k in self.constructorTypes:
198+
return globals()[self.constructorTypes[k]](v)
199+
else:
200+
return v
201+
196202
def __init__(self, data):
197203
for k, v in data.items():
198-
if self.constructorTypes and k in self.constructorTypes:
199-
self.__dict__[k] = self.constructorTypes[k](v)
204+
if isinstance (v, list):
205+
self.__dict__[k] = []
206+
for i in v:
207+
self.__dict__[k].append(self.getObject(k,i))
208+
200209
else:
201-
self.__dict__[k] = v
210+
self.__dict__[k] = self.getObject(k,v)
202211

203212
def __str__(self):
204213
return '%s => %s'%(type(self), str(self.__dict__))
@@ -207,9 +216,13 @@ def __str__(self):
207216
class User(GitlabObject):
208217
url = '/users'
209218

219+
class Group(GitlabObject):
220+
url = '/groups'
221+
constructorTypes = {'projects': 'Project'}
222+
210223
class Project(GitlabObject):
211224
url = '/projects'
212-
constructorTypes = {'owner': User}
225+
constructorTypes = {'owner': 'User', 'namespace': 'Group'}
213226
canUpdate = False
214227
canDelete = False
215228

@@ -246,10 +259,13 @@ class ProjectMilestone(GitlabObject):
246259

247260
class ProjectMergeRequest(GitlabObject):
248261
url = '/projects/%(project_id)d/merge_request'
262+
constructorTypes = {'author': 'User', 'assignee': 'User'}
249263
canDelete = False
250264

251265
class Issue(GitlabObject):
252266
url = '/issues'
267+
constructorTypes = {'author': 'User', 'assignee': 'User',
268+
'milestone': 'ProjectMilestone'}
253269
canGet = False
254270
canDelete = False
255271
canUpdate = False
@@ -260,15 +276,13 @@ class ProjectIssue(GitlabObject):
260276
returnClass = Issue
261277
canDelete = False
262278

263-
class Group(GitlabObject):
264-
url = '/groups'
265-
266279
class Session(object):
267280
def __init__(self):
268281
raise NotImplementedError
269282

270-
class Snippet(GitlabObject):
283+
class ProjectSnippet(GitlabObject):
271284
url = '/projects/%(project_id)d/snippets'
285+
constructorTypes = {'author': 'User'}
272286

273287
class User(GitlabObject):
274288
url = '/users'

0 commit comments

Comments
 (0)