[Checkins] SVN: z3c.form/branches/adamg-objectwidget/ - Feature: Added a new flag ``ignoreContext`` to the form field, so that one

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Nov 20 19:05:46 EST 2008


Log message for revision 93204:
  - Feature: Added a new flag ``ignoreContext`` to the form field, so that one
    can individually select which fields should and which ones should not ignore
    the context.
  

Changed:
  U   z3c.form/branches/adamg-objectwidget/CHANGES.txt
  U   z3c.form/branches/adamg-objectwidget/src/z3c/form/field.py
  U   z3c.form/branches/adamg-objectwidget/src/z3c/form/field.txt
  U   z3c.form/branches/adamg-objectwidget/src/z3c/form/interfaces.py

-=-
Modified: z3c.form/branches/adamg-objectwidget/CHANGES.txt
===================================================================
--- z3c.form/branches/adamg-objectwidget/CHANGES.txt	2008-11-21 00:04:53 UTC (rev 93203)
+++ z3c.form/branches/adamg-objectwidget/CHANGES.txt	2008-11-21 00:05:46 UTC (rev 93204)
@@ -5,6 +5,10 @@
 Version 2.0.0 (2008-??-??)
 --------------------------
 
+- Feature: Added a new flag ``ignoreContext`` to the form field, so that one
+  can individually select which fields should and which ones should not ignore
+  the context.
+
 - Feature: Allow raw request values of sequence widgets to be non-sequence
   values, which makes integration with Javascript libraries easier.
 

Modified: z3c.form/branches/adamg-objectwidget/src/z3c/form/field.py
===================================================================
--- z3c.form/branches/adamg-objectwidget/src/z3c/form/field.py	2008-11-21 00:04:53 UTC (rev 93203)
+++ z3c.form/branches/adamg-objectwidget/src/z3c/form/field.py	2008-11-21 00:05:46 UTC (rev 93204)
@@ -66,7 +66,8 @@
 
     widgetFactory = WidgetFactoryProperty()
 
-    def __init__(self, field, name=None, prefix='', mode=None, interface=None):
+    def __init__(self, field, name=None, prefix='', mode=None, interface=None,
+                 ignoreContext=None):
         self.field = field
         if name is None:
             name = field.__name__
@@ -77,6 +78,7 @@
         if interface is None:
             interface = field.interface
         self.interface = interface
+        self.ignoreContext = ignoreContext
 
     def __repr__(self):
         return '<%s %r>' % (self.__class__.__name__, self.__name__)
@@ -221,13 +223,17 @@
         prefix += util.expandPrefix(self.prefix)
         # Walk through each field, making a widget out of it.
         for field in self.form.fields.values():
+            # Step 0. Determine whether the context should be ignored.
+            ignoreContext = self.ignoreContext
+            if field.ignoreContext is not None:
+                ignoreContext = field.ignoreContext
             # Step 1: Determine the mode of the widget.
             mode = self.mode
             if field.mode is not None:
                 mode = field.mode
             elif field.field.readonly and not self.ignoreReadonly:
                     mode = interfaces.DISPLAY_MODE
-            elif not self.ignoreContext:
+            elif not ignoreContext:
                 # If we do not have enough permissions to write to the
                 # attribute, then switch to display mode.
                 dm = zope.component.getMultiAdapter(
@@ -249,12 +255,12 @@
             widget.context = self.content
             # Step 5: Set the form
             widget.form = self.form
-            # Optimization: set both interfaces here, rather in step 4 and 5: alsoProvides is quite slow
-            zope.interface.alsoProvides(widget,
-                                        interfaces.IContextAware,
-                                        interfaces.IFormAware)
+            # Optimization: set both interfaces here, rather in step 4 and 5:
+            # alsoProvides is quite slow
+            zope.interface.alsoProvides(
+                widget, interfaces.IContextAware, interfaces.IFormAware)
             # Step 6: Set some variables
-            widget.ignoreContext = self.ignoreContext
+            widget.ignoreContext = ignoreContext
             widget.ignoreRequest = self.ignoreRequest
             # Step 7: Set the mode of the widget
             widget.mode = mode

Modified: z3c.form/branches/adamg-objectwidget/src/z3c/form/field.txt
===================================================================
--- z3c.form/branches/adamg-objectwidget/src/z3c/form/field.txt	2008-11-21 00:04:53 UTC (rev 93203)
+++ z3c.form/branches/adamg-objectwidget/src/z3c/form/field.txt	2008-11-21 00:05:46 UTC (rev 93204)
@@ -288,7 +288,23 @@
     >>> manager['country'].mode
     'display'
 
+* ``ignoreContext``
 
+  While the ``ignoreContext`` flag is usually set on the form, it is sometimes
+  desirable to set the flag for a particular field.
+
+    >>> manager = field.Fields(IPerson)
+    >>> manager['country'].ignoreContext
+
+    >>> manager = field.Fields(IPerson, ignoreContext=True)
+    >>> manager['country'].ignoreContext
+    True
+
+    >>> manager = field.Fields(IPerson, ignoreContext=False)
+    >>> manager['country'].ignoreContext
+    False
+
+
 Fields Widget Manager
 ---------------------
 
@@ -730,7 +746,31 @@
   >>> manager['lastName'].error is None
   True
 
+Customization of Ignoring the Context
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+Note that you can also manually control ignoring the context per field.
+
+  >>> class CustomPersonForm(object):
+  ...     prefix = 'form.'
+  ...     fields = field.Fields(IPerson).select('id')
+  ...     fields += field.Fields(IPerson, ignoreContext=True).select(
+  ...                   'firstName', 'lastName')
+  >>> customPersonForm = CustomPersonForm()
+
+Let's now create a manager and update it:
+
+  >>> customManager = field.FieldWidgets(customPersonForm, request, context)
+  >>> customManager.update()
+
+  >>> customManager['id'].ignoreContext
+  False
+  >>> customManager['firstName'].ignoreContext
+  True
+  >>> customManager['lastName'].ignoreContext
+  True
+
+
 Fields -- Custom Widget Factories
 ---------------------------------
 

Modified: z3c.form/branches/adamg-objectwidget/src/z3c/form/interfaces.py
===================================================================
--- z3c.form/branches/adamg-objectwidget/src/z3c/form/interfaces.py	2008-11-21 00:04:53 UTC (rev 93203)
+++ z3c.form/branches/adamg-objectwidget/src/z3c/form/interfaces.py	2008-11-21 00:05:46 UTC (rev 93204)
@@ -189,11 +189,11 @@
         description=_('The interface from which the field is coming.'),
         required=True)
 
-    dataProvider = zope.schema.Field(
-        title=_('Data Provider'),
-        description=_('The component providing the data of the field for '
-                      'the widget.'),
-        required=True)
+    ignoreContext = zope.schema.Bool(
+        title=_('Ignore Context'),
+        description=_('A flag, when set, forces the widget not to look at '
+                      'the context for a value.'),
+        required=False)
 
     widgetFactory = zope.schema.Field(
         title=_('Widget Factory'),



More information about the Checkins mailing list