@@ -221,7 +221,11 @@ def list(self, obj_class, **kwargs):
221
221
cls = obj_class
222
222
if obj_class ._returnClass :
223
223
cls = obj_class ._returnClass
224
-
224
+
225
+ # Add _created manually, because we are not creating objects
226
+ # through normal path
227
+ cls_kwargs ['_created' ] = True
228
+
225
229
# Remove parameters from kwargs before passing it to constructor
226
230
cls_kwargs = kwargs .copy ()
227
231
for key in ['page' , 'per_page' ]:
@@ -486,6 +490,8 @@ class GitlabObject(object):
486
490
_url = None
487
491
_returnClass = None
488
492
_constructorTypes = None
493
+ # Tells if _getListOrObject should return list or object when id is None
494
+ getListWhenNoId = True
489
495
canGet = True
490
496
canList = True
491
497
canCreate = True
@@ -509,16 +515,18 @@ def list(cls, gl, **kwargs):
509
515
return gl .list (cls , ** kwargs )
510
516
511
517
def _getListOrObject (self , cls , id , ** kwargs ):
512
- if id is None :
518
+ if id is None and cls . getListWhenNoId :
513
519
if not cls .canList :
514
- raise GitlabGetError
520
+ raise GitlabListError
515
521
return cls .list (self .gitlab , ** kwargs )
516
-
522
+ elif id is None and not cls .getListWhenNoId :
523
+ if not cls .canGet :
524
+ raise GitlabGetError
525
+ return cls (self .gitlab , id , ** kwargs )
517
526
elif isinstance (id , dict ):
518
527
if not cls .canCreate :
519
528
raise GitlabCreateError
520
529
return cls (self .gitlab , id , ** kwargs )
521
-
522
530
else :
523
531
if not cls .canGet :
524
532
raise GitlabGetError
@@ -547,6 +555,7 @@ def _create(self):
547
555
548
556
json = self .gitlab .create (self )
549
557
self ._setFromDict (json )
558
+ self ._created = True
550
559
551
560
def _update (self ):
552
561
if not self .canUpdate :
@@ -556,7 +565,7 @@ def _update(self):
556
565
self ._setFromDict (json )
557
566
558
567
def save (self ):
559
- if hasattr ( self , 'id' ) :
568
+ if self . _created :
560
569
self ._update ()
561
570
else :
562
571
self ._create ()
@@ -565,23 +574,32 @@ def delete(self):
565
574
if not self .canDelete :
566
575
raise NotImplementedError
567
576
568
- if not hasattr ( self , 'id' ) :
577
+ if not self . _created :
569
578
raise GitlabDeleteError
570
579
571
580
return self .gitlab .delete (self )
572
581
573
582
def __init__ (self , gl , data = None , ** kwargs ):
583
+ self ._created = False
574
584
self .gitlab = gl
575
585
576
586
if data is None or type (data ) in [int , str , unicode ]:
577
587
data = self .gitlab .get (self .__class__ , data , ** kwargs )
588
+ # Object is created because we got it from api
589
+ self ._created = True
578
590
579
591
self ._setFromDict (data )
580
592
581
593
if kwargs :
582
594
for k , v in kwargs .items ():
583
595
self .__dict__ [k ] = v
584
596
597
+ # Special handling for api-objects that don't have id-number in api
598
+ # responses. Currently only Labels and Files
599
+ if not hasattr (self , "id" ):
600
+ self .id = None
601
+
602
+
585
603
def __str__ (self ):
586
604
return '%s => %s' % (type (self ), str (self .__dict__ ))
587
605
0 commit comments