@@ -231,9 +231,14 @@ def list(self, obj_class, **kwargs):
231
231
cls = obj_class
232
232
if obj_class ._returnClass :
233
233
cls = obj_class ._returnClass
234
-
235
- # Remove parameters from kwargs before passing it to constructor
234
+
236
235
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
237
242
for key in ['page' , 'per_page' ]:
238
243
if key in cls_kwargs :
239
244
del cls_kwargs [key ]
@@ -524,6 +529,8 @@ class GitlabObject(object):
524
529
_url = None
525
530
_returnClass = None
526
531
_constructorTypes = None
532
+ # Tells if _getListOrObject should return list or object when id is None
533
+ getListWhenNoId = True
527
534
canGet = True
528
535
canList = True
529
536
canCreate = True
@@ -549,16 +556,18 @@ def list(cls, gl, **kwargs):
549
556
return gl .list (cls , ** kwargs )
550
557
551
558
def _getListOrObject (self , cls , id , ** kwargs ):
552
- if id is None :
559
+ if id is None and cls . getListWhenNoId :
553
560
if not cls .canList :
554
- raise GitlabGetError
561
+ raise GitlabListError
555
562
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 )
557
567
elif isinstance (id , dict ):
558
568
if not cls .canCreate :
559
569
raise GitlabCreateError
560
570
return cls (self .gitlab , id , ** kwargs )
561
-
562
571
else :
563
572
if not cls .canGet :
564
573
raise GitlabGetError
@@ -587,6 +596,7 @@ def _create(self):
587
596
588
597
json = self .gitlab .create (self )
589
598
self ._setFromDict (json )
599
+ self ._created = True
590
600
591
601
def _update (self ):
592
602
if not self .canUpdate :
@@ -596,7 +606,7 @@ def _update(self):
596
606
self ._setFromDict (json )
597
607
598
608
def save (self ):
599
- if hasattr ( self , 'id' ) :
609
+ if self . _created :
600
610
self ._update ()
601
611
else :
602
612
self ._create ()
@@ -605,24 +615,33 @@ def delete(self):
605
615
if not self .canDelete :
606
616
raise NotImplementedError
607
617
608
- if not hasattr ( self , 'id' ) :
618
+ if not self . _created :
609
619
raise GitlabDeleteError
610
620
611
621
return self .gitlab .delete (self )
612
622
613
623
def __init__ (self , gl , data = None , ** kwargs ):
624
+ self ._created = False
614
625
self .gitlab = gl
615
626
616
627
if data is None or isinstance (data , six .integer_types ) or \
617
628
isinstance (data , six .string_types ):
618
629
data = self .gitlab .get (self .__class__ , data , ** kwargs )
630
+ # Object is created because we got it from api
631
+ self ._created = True
619
632
620
633
self ._setFromDict (data )
621
634
622
635
if kwargs :
623
636
for k , v in kwargs .items ():
624
637
self .__dict__ [k ] = v
625
638
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
+
626
645
def __str__ (self ):
627
646
return '%s => %s' % (type (self ), str (self .__dict__ ))
628
647
0 commit comments