add method CapBaseObject.set_empty_fields()

This method can be used to fill all empty fields with the same value.

A field is empty when is value is None, NotLoaded or NotAvailable.

Example:

     # It will be impossible to get all empty fields, except
     # of 'url' (with a call to fillobj())
     video.set_empty_fields(NotAvailable, ('url',))
This commit is contained in:
Romain Bignon 2012-03-12 13:42:00 +01:00
commit 4afac2a0d0

View file

@ -107,6 +107,15 @@ class CapBaseObject(object):
return False
return True
def set_empty_fields(self, value, excepts=()):
"""
Set the same value on all empty fields.
"""
for key, old_value in self.iter_fields():
if old_value in (None, NotLoaded, NotAvailable) and \
not key in excepts:
setattr(self, key, value)
def iter_fields(self):
"""
Iterate on the FIELDS keys and values.