[Mpuls-commits] r1892 - in wasko/branches/2.0: . mpulsweb/controllers mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Mar 4 16:12:46 CET 2010


Author: bh
Date: 2010-03-04 16:12:44 +0100 (Thu, 04 Mar 2010)
New Revision: 1892

Modified:
   wasko/branches/2.0/ChangeLog
   wasko/branches/2.0/mpulsweb/controllers/formularpage.py
   wasko/branches/2.0/mpulsweb/lib/renderer.py
Log:
Make plain boolean fields work in the form pages

* mpulsweb/lib/renderer.py (hidden_bool_tag)
(apply_hidden_booleans): New functions to create resp. use hidden
fields that help detect unchecked boolean fields
(ViewRenderer._renderPlainBool): Make plain booleans work using
hidden_bool_tag and always setting the value to "1" and setting
the checked attribute only if the current value of the formed
field is 1.

* mpulsweb/controllers/formularpage.py
(FormularpageController.save): Handle unchecked booleans using
apply_hidden_booleans


Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog	2010-03-04 14:57:33 UTC (rev 1891)
+++ wasko/branches/2.0/ChangeLog	2010-03-04 15:12:44 UTC (rev 1892)
@@ -1,3 +1,19 @@
+2010-03-04  Bernhard Herzog  <bh at intevation.de>
+
+	Make plain boolean fields work in the form pages
+
+	* mpulsweb/lib/renderer.py (hidden_bool_tag)
+	(apply_hidden_booleans): New functions to create resp. use hidden
+	fields that help detect unchecked boolean fields
+	(ViewRenderer._renderPlainBool): Make plain booleans work using
+	hidden_bool_tag and always setting the value to "1" and setting
+	the checked attribute only if the current value of the formed
+	field is 1.
+
+	* mpulsweb/controllers/formularpage.py
+	(FormularpageController.save): Handle unchecked booleans using
+	apply_hidden_booleans
+
 2010-03-04  Torsten Irländer <torsten.irlaender at intevation.de>
 
 	* jmdweb/public/formed/annotations.xhtml,

Modified: wasko/branches/2.0/mpulsweb/controllers/formularpage.py
===================================================================
--- wasko/branches/2.0/mpulsweb/controllers/formularpage.py	2010-03-04 14:57:33 UTC (rev 1891)
+++ wasko/branches/2.0/mpulsweb/controllers/formularpage.py	2010-03-04 15:12:44 UTC (rev 1892)
@@ -19,7 +19,7 @@
 
 from mpulsweb.lib.base import BaseController, render
 from mpulsweb.lib.renderer import ViewRenderer, RepeatGroupRenderer, \
-     ErrorRenderer
+     ErrorRenderer, apply_hidden_booleans
 
 
 log = logging.getLogger(__name__)
@@ -162,6 +162,7 @@
         page_id = request.params['page']
         try:
             form_result = convert2dic(request.params)
+            apply_hidden_booleans(form_result)
             instance_tree.setData(form_result)
             session['uncommited_fields'] = instance_tree.commit()
             session.save()

Modified: wasko/branches/2.0/mpulsweb/lib/renderer.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/renderer.py	2010-03-04 14:57:33 UTC (rev 1891)
+++ wasko/branches/2.0/mpulsweb/lib/renderer.py	2010-03-04 15:12:44 UTC (rev 1892)
@@ -120,6 +120,38 @@
     return "".join(out)
 
 
+def hidden_bool_tag(id):
+    """Return the hidden HTML input tag for the plain boolean field given by id.
+
+    This hidden input field will have a name derived from id by
+    prepending the id with 'bool-'.  The hidden field can be used to
+    determine which booleans are present to detect which have been
+    unckecked by the user.
+    """
+    return tag("input", type="hidden", name="bool-" + id)
+
+def apply_hidden_booleans(form_result):
+    """Set the boolean values in form results using the hidden fields.
+
+    In HTML-Forms, check boxes that are noch checked when the user
+    submits the form are not included in the form results.  The need to
+    be included the form results, though, so that their values are
+    updated properly.  This function uses the hidden fields created by
+    hidden_bool_tag to determine which booleans are present in the form
+    and adds all booleans which have a hidden field but are not included
+    in form_result, to an empty string.  The hidden fields are removed
+    from form_result.
+
+    The form_result parameter should be a dictionary mapping HTML form
+    field names to values.
+    """
+    for key in [key for key in form_result.keys() if key.startswith("bool-")]:
+        del form_result[key]
+        fieldid = key[5:]
+        if fieldid not in form_result:
+            form_result[fieldid] = ""
+
+
 class Text:
 
     def __init__(self, txt):
@@ -556,9 +588,10 @@
         #print "mvalue: %s value: %s" % (mvalue, value)
         out = []
         out.append(tag("input", type="checkbox", id=id, name=id,
-                       value=mvalue, disabled=self.ro_mode,
-                       checked=(mvalue == value), tabindex=self.next_tabindex(),
+                       value="1", disabled=self.ro_mode,
+                       checked=(value == 1), tabindex=self.next_tabindex(),
                        **SET_MODIFICATION_ATTRS))
+        out.append(hidden_bool_tag(id))
         out.append(self._renderLabel(node))
         out.append(self._renderAttributes(node))
         errors = self._renderErrors(node)



More information about the Mpuls-commits mailing list