correctly check when a field needs to be completed (and introduce the __iscomplete__ method)
This commit is contained in:
parent
d7e2a02733
commit
d026b0c2d5
1 changed files with 26 additions and 1 deletions
|
|
@ -19,6 +19,7 @@
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
from threading import RLock
|
from threading import RLock
|
||||||
|
from logging import debug
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['BaseBackend', 'ObjectNotSupported']
|
__all__ = ['BaseBackend', 'ObjectNotSupported']
|
||||||
|
|
@ -175,8 +176,32 @@ class BaseBackend(object):
|
||||||
return self.TEST(self)
|
return self.TEST(self)
|
||||||
|
|
||||||
def fillobj(self, obj, fields):
|
def fillobj(self, obj, fields):
|
||||||
|
missing_fields = []
|
||||||
|
for field in fields:
|
||||||
|
if not hasattr(obj, field):
|
||||||
|
continue
|
||||||
|
value = getattr(obj, field)
|
||||||
|
|
||||||
|
missing = False
|
||||||
|
if isinstance(value, dict):
|
||||||
|
for v in value.itervalues():
|
||||||
|
if hasattr(v, '__iscomplete__') and not v.__iscomplete__():
|
||||||
|
missing = True
|
||||||
|
break
|
||||||
|
elif isinstance(value, (list,tuple)):
|
||||||
|
for v in value:
|
||||||
|
if hasattr(v, '__iscomplete__') and not v.__iscomplete__():
|
||||||
|
missing = True
|
||||||
|
break
|
||||||
|
elif not value or hasattr(value, '__iscomplete__') and not value.__iscomplete__():
|
||||||
|
missing = True
|
||||||
|
|
||||||
|
if missing:
|
||||||
|
missing_fields.append(field)
|
||||||
|
|
||||||
for key, value in self.OBJECTS.iteritems():
|
for key, value in self.OBJECTS.iteritems():
|
||||||
if isinstance(obj, key):
|
if isinstance(obj, key):
|
||||||
return value(self, obj, fields)
|
debug('Complete %r with fields: %s' % (obj, missing_fields))
|
||||||
|
return value(self, obj, missing_fields) or obj
|
||||||
|
|
||||||
raise ObjectNotSupported('The object of type %s is not supported by the backend %s' % (type(obj), self))
|
raise ObjectNotSupported('The object of type %s is not supported by the backend %s' % (type(obj), self))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue