diff options
author | Mika Mäenpää <mika.j.maenpaa@tut.fi> | 2014-10-15 16:48:56 +0300 |
---|---|---|
committer | Mika Mäenpää <mika.j.maenpaa@tut.fi> | 2014-10-15 16:55:29 +0300 |
commit | c3ab869711276522fe2997ba6e6332704a059d22 (patch) | |
tree | 20f2d77738a3181ac4af37b6d4aae10d03a6dad6 /gitlab.py | |
parent | 221f41806d0dad67adada158a9352aa9e2f2036f (diff) | |
download | gitlab-c3ab869711276522fe2997ba6e6332704a059d22.tar.gz |
Support api-objects which don't have id in api response.
Diffstat (limited to 'gitlab.py')
-rw-r--r-- | gitlab.py | 32 |
1 files changed, 25 insertions, 7 deletions
@@ -221,7 +221,11 @@ class Gitlab(object): cls = obj_class if obj_class._returnClass: cls = obj_class._returnClass - + + # Add _created manually, because we are not creating objects + # through normal path + cls_kwargs['_created'] = True + # Remove parameters from kwargs before passing it to constructor cls_kwargs = kwargs.copy() for key in ['page', 'per_page']: @@ -486,6 +490,8 @@ class GitlabObject(object): _url = None _returnClass = None _constructorTypes = None + # Tells if _getListOrObject should return list or object when id is None + getListWhenNoId = True canGet = True canList = True canCreate = True @@ -509,16 +515,18 @@ class GitlabObject(object): return gl.list(cls, **kwargs) def _getListOrObject(self, cls, id, **kwargs): - if id is None: + if id is None and cls.getListWhenNoId: if not cls.canList: - raise GitlabGetError + raise GitlabListError return cls.list(self.gitlab, **kwargs) - + elif id is None and not cls.getListWhenNoId: + if not cls.canGet: + raise GitlabGetError + return cls(self.gitlab, id, **kwargs) elif isinstance(id, dict): if not cls.canCreate: raise GitlabCreateError return cls(self.gitlab, id, **kwargs) - else: if not cls.canGet: raise GitlabGetError @@ -547,6 +555,7 @@ class GitlabObject(object): json = self.gitlab.create(self) self._setFromDict(json) + self._created = True def _update(self): if not self.canUpdate: @@ -556,7 +565,7 @@ class GitlabObject(object): self._setFromDict(json) def save(self): - if hasattr(self, 'id'): + if self._created: self._update() else: self._create() @@ -565,16 +574,19 @@ class GitlabObject(object): if not self.canDelete: raise NotImplementedError - if not hasattr(self, 'id'): + if not self._created: raise GitlabDeleteError return self.gitlab.delete(self) def __init__(self, gl, data=None, **kwargs): + self._created = False self.gitlab = gl if data is None or type(data) in [int, str, unicode]: data = self.gitlab.get(self.__class__, data, **kwargs) + # Object is created because we got it from api + self._created = True self._setFromDict(data) @@ -582,6 +594,12 @@ class GitlabObject(object): for k, v in kwargs.items(): self.__dict__[k] = v + # Special handling for api-objects that don't have id-number in api + # responses. Currently only Labels and Files + if not hasattr(self, "id"): + self.id = None + + def __str__(self): return '%s => %s' % (type(self), str(self.__dict__)) |