formulator, xml and python 2.3

Sébastien Robin <[email protected]> Thu, 22 Jan 2004 17:04:05 +0100
Newsgroups gmane.comp.web.zope.formulator.devel
Message-ID <[email protected]>
Hi all,

Sorry to disturb you again.

Something changed between python 2.2 and python 2.3.
in python 2.2 :   1==1 returns 1
in python 2.3 :   1==1 returns True

So in python 2.2 checkbox fields were returning 0 or 1, and with python 
2.3 it returns true or false, with give errors when it is commited into 
the url (because it expect an int).

I had to do this for BooleanValidator:

validate(self, field, key, REQUEST):
    result = not not REQUEST.get(key, 0)
    if result==True:
       return 1
    else:
       return 0

And it is even worse when we do a formToXML and after an XMLToForm.
This is what I had without any modification to BooleanValidator.

formToXML returns : ...<required type>True</required>...
then XMLToForm thinks that the field required is a string with the value 
"True".

By the same time, when we set a method for a field value, the xml output 
may set a type, like type="method", and the name of the method. If we 
don't do that, the xml output is like this :
<list_method>&lt;Method instance at 44a17a40&gt;</list_method>

I join with this mail the patch I have applied for xml.

Regards.

  Sébastien Robin.

-- 
Sebastien ROBIN

About www.nexedi.com 

Nexedi is a consulting and development services company helping small and 
medium organisations to choose open source / free software and fulfill 
their IT application needs. Nexedi is the founder of the ERP5 project, a 
Free / Open Source ERP software based on innovative technologies 
(www.erp5.org).

About www.storever.com

Storever provides a reliable source for OpenBrick, WiFi infrastructure, 
notebooks and servers preconfigured with the GNU/Linux operating system

_______________________________________________
formulator-dev mailing list
formulator-dev-IAPFreCvJWM6s/[email protected]
http://lists.infrae.com/mailman/listinfo/formulator-dev
Formulator-1.6.1-xml.patch (text/x-diff, 2.4 KB)
Seulement dans Formulator-1.6.1.final: Form.pyc
diff -ru Formulator-1.6.1/FormToXML.py Formulator-1.6.1.final/FormToXML.py
--- Formulator-1.6.1/FormToXML.py	2004-01-21 17:06:17.000000000 +0100
+++ Formulator-1.6.1.final/FormToXML.py	2004-01-22 16:29:27.000000000 +0100
@@ -41,9 +41,16 @@
             for key, value in items:
                 if value is None:
                     continue
-                if type(value) == type(1.1):
+                if value==True: # XXX Patch
+                    value = 1 # XXX Patch
+                if value==False: # XXX Patch
+                    value = 0 # XXX Patch
+                if callable(value): # XXX Patch
+                    write('          <%s type="method">%s</%s>\n' % # XXX Patch
+                        (key, escape(str(value.method_name)), key)) # XXX Patch
+                elif type(value) == type(1.1):
                     write('          <%s type="float">%s</%s>\n' % (key, escape(str(value)), key))
-                if type(value) == type(1):
+                elif type(value) == type(1):
                     write('          <%s type="int">%s</%s>\n' % (key, escape(str(value)), key))
                 elif type(value) == type([]):
                     write('          <%s type="list">%s</%s>\n' % (key, escape(str(value)), key))
diff -ru Formulator-1.6.1/XMLToForm.py Formulator-1.6.1.final/XMLToForm.py
--- Formulator-1.6.1/XMLToForm.py	2004-01-21 17:06:17.000000000 +0100
+++ Formulator-1.6.1.final/XMLToForm.py	2004-01-22 16:22:21.000000000 +0100
@@ -1,5 +1,6 @@
 import XMLObjects
 from Products.Formulator.TALESField import TALESMethod
+from Products.Formulator.MethodField import Method
 
 def XMLToForm(s, form, override_encoding=None):
     """Takes an xml string and changes formulator form accordingly.
@@ -80,6 +81,8 @@
                     field.values[name] = float(value.text)
                 elif value.attributes.get('type') == 'int':
                     field.values[name] = int(value.text)
+                elif value.attributes.get('type') == 'method': # XXX Patch
+                    field.values[name] = Method(value.text) # XXX Patch
                 elif value.attributes.get('type') == 'list':
                     # XXX bare eval here (this may be a security leak ?)
                     field.values[name] = eval(
Seulement dans Formulator-1.6.1.final: XMLToForm.py~
Seulement dans Formulator-1.6.1.final: XMLToForm.pyc