[Checkins] SVN: grok/trunk/src/grok/ Implement AutoFields,
so that one can use grok's automatic field
Martijn Faassen
faassen at infrae.com
Fri Dec 1 16:59:41 EST 2006
Log message for revision 71369:
Implement AutoFields, so that one can use grok's automatic field
generation and still manipulate the resulting fields.
Changed:
U grok/trunk/src/grok/__init__.py
U grok/trunk/src/grok/formlib.py
U grok/trunk/src/grok/interfaces.py
A grok/trunk/src/grok/tests/form/customautoform.py
-=-
Modified: grok/trunk/src/grok/__init__.py
===================================================================
--- grok/trunk/src/grok/__init__.py 2006-12-01 21:36:41 UTC (rev 71368)
+++ grok/trunk/src/grok/__init__.py 2006-12-01 21:59:40 UTC (rev 71369)
@@ -37,7 +37,7 @@
from grok._grok import do_grok as grok # Avoid name clash within _grok
from grok._grok import SubscribeDecorator as subscribe
from grok.error import GrokError, GrokImportError
-from grok.formlib import action
+from grok.formlib import action, AutoFields
# Our __init__ provides the grok API directly so using 'import grok' is enough.
from grok.interfaces import IGrokAPI
Modified: grok/trunk/src/grok/formlib.py
===================================================================
--- grok/trunk/src/grok/formlib.py 2006-12-01 21:36:41 UTC (rev 71368)
+++ grok/trunk/src/grok/formlib.py 2006-12-01 21:59:40 UTC (rev 71369)
@@ -85,6 +85,11 @@
fields = getattr(factory, 'form_fields', None)
if fields is not None:
return fields
+ return get_auto_fields(context)
+
+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)
@@ -97,6 +102,8 @@
fields = fields.omit('__name__')
return fields
+AutoFields = get_auto_fields
+
def load_template(name):
filename = os.path.join(os.path.dirname(__file__), 'templates', name)
f = open(filename, 'r')
Modified: grok/trunk/src/grok/interfaces.py
===================================================================
--- grok/trunk/src/grok/interfaces.py 2006-12-01 21:36:41 UTC (rev 71368)
+++ grok/trunk/src/grok/interfaces.py 2006-12-01 21:59:40 UTC (rev 71369)
@@ -135,6 +135,10 @@
"""Return a list of formlib fields based on interfaces and/or schema
fields."""
+ def AutoFields(context):
+ """Return a list of fields for context autogenerated by grok.
+ """
+
def action(label, actions=None, **options):
"""grok-specific action decorator.
"""
Added: grok/trunk/src/grok/tests/form/customautoform.py
===================================================================
--- grok/trunk/src/grok/tests/form/customautoform.py 2006-12-01 21:36:41 UTC (rev 71368)
+++ grok/trunk/src/grok/tests/form/customautoform.py 2006-12-01 21:59:40 UTC (rev 71369)
@@ -0,0 +1,49 @@
+"""
+A form view can have a custom form_fields but reusing those fields that
+were deduced automatically, using grok.AutoFields:
+
+ >>> grok.grok(__name__)
+
+We only expect a single field to be present in the form, as we omitted 'size':
+
+ >>> from zope import component
+ >>> from zope.publisher.browser import TestRequest
+ >>> request = TestRequest()
+ >>> view = component.getMultiAdapter((Mammoth(), request), name='edit')
+ >>> len(view.form_fields)
+ 1
+ >>> [w.__name__ for w in view.form.form_fields]
+ ['name']
+ >>> view = component.getMultiAdapter((Mammoth2(), request), name='edit2')
+ >>> len(view.form_fields)
+ 1
+ >>> [w.__name__ for w in view.form.form_fields]
+ ['size']
+
+"""
+
+import grok
+from zope import interface, schema
+
+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)
+
+class Edit(grok.EditForm):
+ grok.context(Mammoth)
+
+ form_fields = grok.AutoFields(Mammoth).omit('size')
+
+class Mammoth2(grok.Model):
+ class fields:
+ name = schema.TextLine(title=u"Name")
+ size = schema.TextLine(title=u"Size", default=u"Quite normal")
+
+class Edit2(grok.EditForm):
+ grok.context(Mammoth2)
+
+ form_fields = grok.AutoFields(Mammoth).omit('name')
+
More information about the Checkins
mailing list