provides a fallback_on_none_attribute field attribute, allowing to specify default value for attributes of *existing* entities
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 10 Feb 2010 13:58:11 +0100
changeset 4546 f8ac61376b2b
parent 4545 9b7b3303c515
child 4547 5f957e351b0a
provides a fallback_on_none_attribute field attribute, allowing to specify default value for attributes of *existing* entities
web/facet.py
web/formfields.py
--- a/web/facet.py	Wed Feb 10 13:57:29 2010 +0100
+++ b/web/facet.py	Wed Feb 10 13:58:11 2010 +0100
@@ -519,8 +519,7 @@
         return FacetRangeWidget
 
     def get_widget(self):
-        """return the widget instance to use to display this facet
-        """
+        """return the widget instance to use to display this facet"""
         values = set(value for _, value in self.vocabulary() if value is not None)
         return self.wdgclass(self, min(values), max(values))
 
--- a/web/formfields.py	Wed Feb 10 13:57:29 2010 +0100
+++ b/web/formfields.py	Wed Feb 10 13:58:11 2010 +0100
@@ -112,6 +112,7 @@
     fieldset = None
     order = None
     value = _MARKER
+    fallback_on_none_attribute = False
 
     def __init__(self, name=None, label=_MARKER, widget=None, **kwargs):
         for key, val in kwargs.items():
@@ -218,9 +219,13 @@
             entity = form.edited_entity
             if form._cw.vreg.schema.rschema(self.name).final:
                 if entity.has_eid() or self.name in entity:
-                    return getattr(entity, self.name)
+                    value = getattr(entity, self.name)
+                    if value is not None or not self.fallback_on_none_attribute:
+                        return value
             elif entity.has_eid() or entity.relation_cached(self.name, self.role):
-                return [r[0] for r in entity.related(self.name, self.role)]
+                value = [r[0] for r in entity.related(self.name, self.role)]
+                if value or not self.fallback_on_none_attribute:
+                    return value
         return self.initial_typed_value(form, load_bytes)
 
     def initial_typed_value(self, form, load_bytes):