[Checkins] SVN: grok/trunk/src/grok/ Fix a bug where a special thing can happen that's described in schemainherit.py

Martijn Faassen faassen at infrae.com
Thu Dec 14 16:40:14 EST 2006


Log message for revision 71553:
  Fix a bug where a special thing can happen that's described in schemainherit.py
  

Changed:
  U   grok/trunk/src/grok/formlib.py
  U   grok/trunk/src/grok/tests/form/schemainherit.py

-=-
Modified: grok/trunk/src/grok/formlib.py
===================================================================
--- grok/trunk/src/grok/formlib.py	2006-12-14 21:08:23 UTC (rev 71552)
+++ grok/trunk/src/grok/formlib.py	2006-12-14 21:40:14 UTC (rev 71553)
@@ -113,13 +113,37 @@
     # we're autogenerating them from any model-specific
     # fields along with any schemas defined by the context
     fields = form.Fields(*get_context_schema_fields(context))
-    fields += form.Fields(*interface.implementedBy(context))
+    fields += form.Fields(*most_specialized_interfaces(context))
     # we pull in this field by default, but we don't want it in our form
     fields = fields.omit('__name__')
     return fields
 
 AutoFields = get_auto_fields
 
+def most_specialized_interfaces(context):
+    """Get interfaces for an object without any duplicates.
+
+    Interfaces in a declaration for an object may already have been seen
+    because it is also inherited by another interface. Don't return the
+    interface twice, as that would result in duplicate names when creating
+    the form.
+    """
+    declaration = interface.implementedBy(context)
+    seen = []
+    for iface in declaration.flattened():
+        if interface_seen(seen, iface):
+            continue
+        seen.append(iface)
+    return seen
+
+def interface_seen(seen, iface):
+    """Return True if interface already is seen.
+    """
+    for seen_iface in seen:
+        if seen_iface.extends(iface):
+            return True
+    return False
+
 def load_template(name):
     filename = os.path.join(os.path.dirname(__file__), 'templates', name)
     f = open(filename, 'r')

Modified: grok/trunk/src/grok/tests/form/schemainherit.py
===================================================================
--- grok/trunk/src/grok/tests/form/schemainherit.py	2006-12-14 21:08:23 UTC (rev 71552)
+++ grok/trunk/src/grok/tests/form/schemainherit.py	2006-12-14 21:40:14 UTC (rev 71553)
@@ -24,6 +24,12 @@
   >>> [w.__name__ for w in view.form.form_fields]
   ['name', 'size', 'speciality']
 
+  >>> antimanfred = YetAnotherMammoth()
+  >>> view = component.getMultiAdapter((antimanfred, request), name='edit3')
+  >>> len(view.form.form_fields)
+  3
+  >>> [w.__name__ for w in view.form.form_fields]
+  ['name', 'size', 'speciality']
 """
 import grok
 from zope import interface, schema
@@ -46,3 +52,13 @@
 
     form_fields = grok.AutoFields(Mammoth)
 
+# situation where subclass implements something on top of base class
+class AnotherMammoth(grok.Model):
+    interface.implements(IMammoth)
+
+class YetAnotherMammoth(AnotherMammoth):
+    interface.implements(ISpecialMammoth)
+
+class Edit3(grok.EditForm):
+    grok.context(YetAnotherMammoth)
+    



More information about the Checkins mailing list