[Checkins] SVN: grok/trunk/src/grok/ merged luciano-fields-class-removal branch

Luciano Ramalho luciano at ramalho.org
Wed Sep 19 23:04:52 EDT 2007


Log message for revision 79757:
  merged luciano-fields-class-removal branch
  

Changed:
  U   grok/trunk/src/grok/formlib.py
  U   grok/trunk/src/grok/ftests/form/actions.py
  U   grok/trunk/src/grok/ftests/form/addform.py
  D   grok/trunk/src/grok/ftests/form/editform_applydata_classfields.py
  A   grok/trunk/src/grok/ftests/form/editform_applydata_schemafields.py
  U   grok/trunk/src/grok/ftests/form/form.py
  U   grok/trunk/src/grok/ftests/form/update.py
  U   grok/trunk/src/grok/meta.py
  U   grok/trunk/src/grok/tests/form/form.py
  U   grok/trunk/src/grok/tests/form/schemaform.py

-=-
Modified: grok/trunk/src/grok/formlib.py
===================================================================
--- grok/trunk/src/grok/formlib.py	2007-09-20 02:14:34 UTC (rev 79756)
+++ grok/trunk/src/grok/formlib.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -27,38 +27,15 @@
     fields.sort(key=lambda field: field.order)
     return form.Fields(*(args + tuple(fields)), **kw)
 
-def get_context_schema_fields(context):
-    """Get the schema fields for a context object.
-    """
-    fields = []
-    fields_class = getattr(context, 'fields', None)
-    # bail out if there is no fields attribute at all
-    if fields_class is None:
-        return fields
-    # bail out if there's a fields attribute but it isn't an old-style class
-    if type(fields_class) != types.ClassType:
-        return fields
-    # get the fields from the class
-    for name in dir(fields_class):
-        field = getattr(fields_class, name)
-        if IField.providedBy(field):
-            if not getattr(field, '__name__', None):
-                field.__name__ = name
-            fields.append(field)
-    fields.sort(key=lambda field: field.order)
-    return fields
-
 def get_auto_fields(context):
     """Get the form fields for context.
     """
     # for an interface context, we generate them from that interface
     if IInterface.providedBy(context):
         return form.Fields(context)
-    # if we have a non-interface context,
-    # 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(*most_specialized_interfaces(context))
+    # if we have a non-interface context, we're autogenerating them
+    # from any schemas defined by the 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

Modified: grok/trunk/src/grok/ftests/form/actions.py
===================================================================
--- grok/trunk/src/grok/ftests/form/actions.py	2007-09-20 02:14:34 UTC (rev 79756)
+++ grok/trunk/src/grok/ftests/form/actions.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -45,11 +45,18 @@
 """
 import grok
 from zope import schema
+from zope.interface import Interface, implements
+from zope.schema.fieldproperty import FieldProperty
 
+class IMammoth(Interface):
+    name = schema.TextLine(title=u"Name")
+    size = schema.TextLine(title=u"Size", default=u"Quite normal")
+
 class Mammoth(grok.Model):
-    class fields:
-        name = schema.TextLine(title=u"Name")
-        size = schema.TextLine(title=u"Size")
+    implements(IMammoth)
+    
+    name = FieldProperty(IMammoth['name'])    
+    size = FieldProperty(IMammoth['size'])    
 
 class Edit(grok.EditForm):
     @grok.action("Apply")

Modified: grok/trunk/src/grok/ftests/form/addform.py
===================================================================
--- grok/trunk/src/grok/ftests/form/addform.py	2007-09-20 02:14:34 UTC (rev 79756)
+++ grok/trunk/src/grok/ftests/form/addform.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -27,14 +27,17 @@
 """
 import grok
 from zope import schema
+from zope.interface import Interface, implements
 
 class Zoo(grok.Container):
     pass
 
+class IMammoth(Interface):
+    name = schema.TextLine(title=u"Name")
+    size = schema.TextLine(title=u"Size", default=u"Quite normal")
+
 class Mammoth(grok.Model):
-    class fields:
-        name = schema.TextLine(title=u"Name")
-        size = schema.TextLine(title=u"Size")
+    implements(IMammoth)
 
     def __init__(self, name='', size=''):
         self.name = name

Deleted: grok/trunk/src/grok/ftests/form/editform_applydata_classfields.py
===================================================================
--- grok/trunk/src/grok/ftests/form/editform_applydata_classfields.py	2007-09-20 02:14:34 UTC (rev 79756)
+++ grok/trunk/src/grok/ftests/form/editform_applydata_classfields.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -1,72 +0,0 @@
-"""
-A grok.EditForm uses applyData in update mode to save the form data on
-the object.  Update mode means that only those fields are changed on
-the object that need to be changed.
-
-This is essentially the same narrative as 'editform_applydata'. Here
-we test the whole procedure on fields defined on the model class:
-
-  >>> getRootFolder()["manfred"] = mammoth = Mammoth()
-  >>> mammoth.name = 'Manfred the Mammoth'
-  >>> mammoth.size = 'Really big'
-
-  >>> from zope.testbrowser.testing import Browser
-  >>> browser = Browser()
-  >>> browser.handleErrors = False
-
-If we don't change any of the fields, there will no object modified
-event and applyData will report no changes:
-
-  >>> browser.open("http://localhost/manfred/@@edit")
-  >>> browser.getControl("Apply").click()
-  >>> 'No changes' in browser.contents
-  True
-
-If we change one field, only that attribute will be changed.  The
-object modified event also reflects that:
-
-  >>> browser.getControl(name="form.name").value = "Manfred the Big Mammoth"
-  >>> browser.getControl("Apply").click()
-  An IObjectModifiedEvent was sent for a mammoth with the following changes:
-  name
-  >>> 'Updated' in browser.contents
-  True
-
-Let's change the other field:
-
-  >>> browser.getControl(name="form.size").value = "Enormously big"
-  >>> browser.getControl("Apply").click()
-  An IObjectModifiedEvent was sent for a mammoth with the following changes:
-  size
-  >>> 'Updated' in browser.contents
-  True
-
-And finally let's change both fields:
-
-  >>> browser.getControl(name="form.name").value = "Manfred the Mammoth"
-  >>> browser.getControl(name="form.size").value = "Really big"
-  >>> browser.getControl("Apply").click()
-  An IObjectModifiedEvent was sent for a mammoth with the following changes:
-  name, size
-  >>> 'Updated' in browser.contents
-  True
-
-"""
-import grok
-from zope import schema
-
-
-class Mammoth(grok.Model):
-    class fields:
-        name = schema.TextLine(title=u"Name")
-        size = schema.TextLine(title=u"Size")
-
-class Edit(grok.EditForm):
-    pass
-
- at grok.subscribe(Mammoth, grok.IObjectModifiedEvent)
-def notify_change_event(mammoth, event):
-    print ("An IObjectModifiedEvent was sent for a mammoth with the "
-           "following changes:")
-    for descr in event.descriptions:
-        print ", ".join(descr.attributes)

Copied: grok/trunk/src/grok/ftests/form/editform_applydata_schemafields.py (from rev 79756, grok/branches/luciano-fields-class-removal/src/grok/ftests/form/editform_applydata_schemafields.py)
===================================================================
--- grok/trunk/src/grok/ftests/form/editform_applydata_schemafields.py	                        (rev 0)
+++ grok/trunk/src/grok/ftests/form/editform_applydata_schemafields.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -0,0 +1,75 @@
+"""
+A grok.EditForm uses applyData in update mode to save the form data on
+the object.  Update mode means that only those fields are changed on
+the object that need to be changed.
+
+This is essentially the same narrative as 'editform_applydata'. Here we
+test the whole procedure on fields on the interface implemented by the
+model class:
+
+  >>> getRootFolder()["manfred"] = mammoth = Mammoth()
+  >>> mammoth.name = 'Manfred the Mammoth'
+  >>> mammoth.size = 'Really big'
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+
+If we don't change any of the fields, there will no object modified
+event and applyData will report no changes:
+
+  >>> browser.open("http://localhost/manfred/@@edit")
+  >>> browser.getControl("Apply").click()
+  >>> 'No changes' in browser.contents
+  True
+
+If we change one field, only that attribute will be changed.  The
+object modified event also reflects that:
+
+  >>> browser.getControl(name="form.name").value = "Manfred the Big Mammoth"
+  >>> browser.getControl("Apply").click()
+  An IObjectModifiedEvent was sent for a mammoth with the following changes:
+  name
+  >>> 'Updated' in browser.contents
+  True
+
+Let's change the other field:
+
+  >>> browser.getControl(name="form.size").value = "Enormously big"
+  >>> browser.getControl("Apply").click()
+  An IObjectModifiedEvent was sent for a mammoth with the following changes:
+  size
+  >>> 'Updated' in browser.contents
+  True
+
+And finally let's change both fields:
+
+  >>> browser.getControl(name="form.name").value = "Manfred the Mammoth"
+  >>> browser.getControl(name="form.size").value = "Really big"
+  >>> browser.getControl("Apply").click()
+  An IObjectModifiedEvent was sent for a mammoth with the following changes:
+  name, size
+  >>> 'Updated' in browser.contents
+  True
+
+"""
+import grok
+from zope import schema
+from zope.interface import Interface, implements
+
+class IMammoth(Interface):
+    name = schema.TextLine(title=u"Name")
+    size = schema.TextLine(title=u"Size", default=u"Quite normal")
+
+class Mammoth(grok.Model):
+    implements(IMammoth)
+    
+class Edit(grok.EditForm):
+    pass
+
+ at grok.subscribe(Mammoth, grok.IObjectModifiedEvent)
+def notify_change_event(mammoth, event):
+    print ("An IObjectModifiedEvent was sent for a mammoth with the "
+           "following changes:")
+    for descr in event.descriptions:
+        print ", ".join(descr.attributes)

Modified: grok/trunk/src/grok/ftests/form/form.py
===================================================================
--- grok/trunk/src/grok/ftests/form/form.py	2007-09-20 02:14:34 UTC (rev 79756)
+++ grok/trunk/src/grok/ftests/form/form.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -28,11 +28,18 @@
 """
 import grok
 from zope import schema
+from zope.interface import Interface, implements
+from zope.schema.fieldproperty import FieldProperty
 
+class IMammoth(Interface):
+    name = schema.TextLine(title=u"Name")
+    size = schema.TextLine(title=u"Size", default=u"Quite normal")
+
 class Mammoth(grok.Model):
-    class fields:
-        name = schema.TextLine(title=u"Name")
-        size = schema.TextLine(title=u"Size")
+    implements(IMammoth)
+    
+    name = FieldProperty(IMammoth['name'])    
+    size = FieldProperty(IMammoth['size'])    
 
 class Edit(grok.EditForm):
     pass

Modified: grok/trunk/src/grok/ftests/form/update.py
===================================================================
--- grok/trunk/src/grok/ftests/form/update.py	2007-09-20 02:14:34 UTC (rev 79756)
+++ grok/trunk/src/grok/ftests/form/update.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -45,9 +45,15 @@
 import grok
 from zope import schema
 
+from zope.interface import Interface, implements
+
+class IMammoth(Interface):
+    name = schema.TextLine(title=u"Name")
+
 class Mammoth(grok.Model):
-    class fields:
-        name = schema.TextLine(title=u"Name", default=u'Manfred')
+    implements(IMammoth)
+    
+    name = u'Manfred'
 
 class Index(grok.View):
 

Modified: grok/trunk/src/grok/meta.py
===================================================================
--- grok/trunk/src/grok/meta.py	2007-09-20 02:14:34 UTC (rev 79756)
+++ grok/trunk/src/grok/meta.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -50,23 +50,6 @@
 from grok.util import check_adapts, get_default_permission, make_checker
 
 
-class ModelGrokker(martian.ClassGrokker):
-    component_class = grok.Model
-
-    def grok(self, name, factory, context, module_info, templates):
-        for field in formlib.get_context_schema_fields(factory):
-            setattr(factory, field.__name__, field.default)
-        return True
-
-
-class ContainerGrokker(ModelGrokker):
-    component_class = grok.Container
-
-
-class LocalUtilityGrokker(ModelGrokker):
-    component_class = grok.LocalUtility
-
-
 class AdapterGrokker(martian.ClassGrokker):
     component_class = grok.Adapter
 

Modified: grok/trunk/src/grok/tests/form/form.py
===================================================================
--- grok/trunk/src/grok/tests/form/form.py	2007-09-20 02:14:34 UTC (rev 79756)
+++ grok/trunk/src/grok/tests/form/form.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -1,32 +1,11 @@
 """
-A grok.Model may contain a nested class named 'fields'. All attributes of
-'fields' that provide IField will cause attributes of the same name to appear on
-the grok.Model:
-
-  >>> grok.grok(__name__)
-  >>> manfred = Mammoth()
-  >>> print manfred.name
-  None
-  >>> print manfred.size
-  Quite normal
-  >>> manfred.somethingelse
-  Traceback (most recent call last):
-    ...
-  AttributeError: 'Mammoth' object has no attribute 'somethingelse'
-
-If the 'fields' attribute is not an old-style class, it will not trigger any
-attribute generation:
-
-  >>> cave = Cave()
-  >>> cave.ignored
-  Traceback (most recent call last):
-    ...
-  AttributeError: 'Cave' object has no attribute 'ignored'
-
 A grok.EditForm is a special grok.View that renders an edit form.
 
   >>> from zope import component
   >>> from zope.publisher.browser import TestRequest
+  >>> grok.grok(__name__)
+  >>> manfred = Mammoth()
+
   >>> request = TestRequest()
   >>> view = component.getMultiAdapter((manfred, request), name='edit')
   >>> len(view.form_fields)
@@ -46,24 +25,26 @@
 """
 import grok
 from zope import schema
+from zope.interface import Interface, implements
 
+class IMammoth(Interface):
+    name = schema.TextLine(title=u"Name")
+    size = schema.TextLine(title=u"Size", default=u"Quite normal")
+
 class Mammoth(grok.Model):
-    class fields:
-        name = schema.TextLine(title=u"Name")
-        size = schema.TextLine(title=u"Size", default=u"Quite normal")
-        somethingelse = None
+    implements(IMammoth)
 
 class Edit(grok.EditForm):
     grok.context(Mammoth)
 
-class Cave(grok.Model):
-    fields = ['ignored']
+class IDifferentMammoth(Interface):
+    # mind the different order of fields
+    size = schema.TextLine(title=u"Size", default=u"Quite normal")
+    name = schema.TextLine(title=u"Name")
 
 class DifferentMammoth(grok.Model):
-    class fields:
-        # mind the different order of fields
-        size = schema.TextLine(title=u"Size", default=u"Quite normal")
-        name = schema.TextLine(title=u"Name")
+    implements(IDifferentMammoth)
 
 class EditDifferent(grok.EditForm):
     grok.context(DifferentMammoth)
+

Modified: grok/trunk/src/grok/tests/form/schemaform.py
===================================================================
--- grok/trunk/src/grok/tests/form/schemaform.py	2007-09-20 02:14:34 UTC (rev 79756)
+++ grok/trunk/src/grok/tests/form/schemaform.py	2007-09-20 03:04:51 UTC (rev 79757)
@@ -3,6 +3,10 @@
 
   >>> grok.grok(__name__)
   >>> manfred = Mammoth()
+  >>> print manfred.name
+  None
+  >>> print manfred.size
+  Quite normal
 
 A grok.EditForm is a special grok.View that renders an edit form.
 
@@ -23,15 +27,6 @@
   >>> [w.__name__ for w in view.form_fields]
   ['can_talk', 'name', 'size']
 
-Schema fields and model level fields are combined:
-
-  >>> view = component.getMultiAdapter(
-  ...    (AnotherMammoth(), request), name='edit3')
-  >>> len(view.form_fields)
-  3
-  >>> [w.__name__ for w in view.form_fields]
-  ['can_talk', 'name', 'size']
-
 If the context is an interface instead of a model directly, the fields
 will be retrieved from that interface, and that interface only:
 
@@ -45,39 +40,44 @@
 """
 import grok
 from zope import interface, schema
+from zope.schema.fieldproperty import FieldProperty
 
 class IMammoth(interface.Interface):
     name = schema.TextLine(title=u"Name")
     size = schema.TextLine(title=u"Size", default=u"Quite normal")
 
+
 class Mammoth(grok.Model):
     interface.implements(IMammoth)
 
+    name = FieldProperty(IMammoth['name'])    
+    size = FieldProperty(IMammoth['size'])
+
+
 class Edit(grok.EditForm):
     grok.context(Mammoth)
 
+
 class IMovieCharacter(interface.Interface):
     can_talk = schema.Bool(title=u'Can talk', default=False)
 
+
 class Manfred(Mammoth):
     interface.implements(IMovieCharacter)
 
+
 class Edit2(grok.EditForm):
     grok.context(Manfred)
     
-class AnotherMammoth(Mammoth):
-    class fields:
-        can_talk = schema.Bool(title=u'Can talk', default=False)
 
-class Edit3(grok.EditForm):
-    grok.context(AnotherMammoth)
-
 class IYetAnotherMammoth(interface.Interface):
     alpha = schema.TextLine(title=u'alpha')
     beta = schema.TextLine(title=u'beta')
 
+
 class YetAnotherMammoth(grok.Model):
     interface.implements(IYetAnotherMammoth)
 
+
 class Edit4(grok.EditForm):
     grok.context(IYetAnotherMammoth)



More information about the Checkins mailing list