@@ -231,9 +231,14 @@ def list(self, obj_class, **kwargs):
231231 cls = obj_class
232232 if obj_class ._returnClass :
233233 cls = obj_class ._returnClass
234-
235- # Remove parameters from kwargs before passing it to constructor
234+
236235 cls_kwargs = kwargs .copy ()
236+
237+ # Add _created manually, because we are not creating objects
238+ # through normal path
239+ cls_kwargs ['_created' ] = True
240+
241+ # Remove parameters from kwargs before passing it to constructor
237242 for key in ['page' , 'per_page' ]:
238243 if key in cls_kwargs :
239244 del cls_kwargs [key ]
@@ -524,6 +529,8 @@ class GitlabObject(object):
524529 _url = None
525530 _returnClass = None
526531 _constructorTypes = None
532+ # Tells if _getListOrObject should return list or object when id is None
533+ getListWhenNoId = True
527534 canGet = True
528535 canList = True
529536 canCreate = True
@@ -549,16 +556,18 @@ def list(cls, gl, **kwargs):
549556 return gl .list (cls , ** kwargs )
550557
551558 def _getListOrObject (self , cls , id , ** kwargs ):
552- if id is None :
559+ if id is None and cls . getListWhenNoId :
553560 if not cls .canList :
554- raise GitlabGetError
561+ raise GitlabListError
555562 return cls .list (self .gitlab , ** kwargs )
556-
563+ elif id is None and not cls .getListWhenNoId :
564+ if not cls .canGet :
565+ raise GitlabGetError
566+ return cls (self .gitlab , id , ** kwargs )
557567 elif isinstance (id , dict ):
558568 if not cls .canCreate :
559569 raise GitlabCreateError
560570 return cls (self .gitlab , id , ** kwargs )
561-
562571 else :
563572 if not cls .canGet :
564573 raise GitlabGetError
@@ -587,6 +596,7 @@ def _create(self):
587596
588597 json = self .gitlab .create (self )
589598 self ._setFromDict (json )
599+ self ._created = True
590600
591601 def _update (self ):
592602 if not self .canUpdate :
@@ -596,7 +606,7 @@ def _update(self):
596606 self ._setFromDict (json )
597607
598608 def save (self ):
599- if hasattr ( self , 'id' ) :
609+ if self . _created :
600610 self ._update ()
601611 else :
602612 self ._create ()
@@ -605,24 +615,33 @@ def delete(self):
605615 if not self .canDelete :
606616 raise NotImplementedError
607617
608- if not hasattr ( self , 'id' ) :
618+ if not self . _created :
609619 raise GitlabDeleteError
610620
611621 return self .gitlab .delete (self )
612622
613623 def __init__ (self , gl , data = None , ** kwargs ):
624+ self ._created = False
614625 self .gitlab = gl
615626
616627 if data is None or isinstance (data , six .integer_types ) or \
617628 isinstance (data , six .string_types ):
618629 data = self .gitlab .get (self .__class__ , data , ** kwargs )
630+ # Object is created because we got it from api
631+ self ._created = True
619632
620633 self ._setFromDict (data )
621634
622635 if kwargs :
623636 for k , v in kwargs .items ():
624637 self .__dict__ [k ] = v
625638
639+ # Special handling for api-objects that don't have id-number in api
640+ # responses. Currently only Labels and Files
641+ if not hasattr (self , "id" ):
642+ self .id = None
643+
644+
626645 def __str__ (self ):
627646 return '%s => %s' % (type (self ), str (self .__dict__ ))
628647
0 commit comments