From 2e1c6a6999afffddd0ac7b038b078db1a52402b5 Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Fri, 29 Jan 2010 11:28:39 +0100 Subject: [PATCH] Management of floats with a given precision; XmlMarshaller can dump unicode or str result. --- gen/__init__.py | 7 ++++++- gen/plone25/mixins/__init__.py | 6 ++++++ gen/plone25/skin/macros.pt | 13 ++++++++++++- gen/plone25/wrappers/__init__.py | 3 ++- shared/xml_parser.py | 15 +++++++++++---- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/gen/__init__.py b/gen/__init__.py index 97fcfc0..3871aa3 100755 --- a/gen/__init__.py +++ b/gen/__init__.py @@ -158,12 +158,17 @@ class Float(Type): page='main', group=None, move=0, indexed=False, searchable=False, specificReadPermission=False, specificWritePermission=False, width=None, height=None, - master=None, masterValue=None, focus=False, historized=False): + master=None, masterValue=None, focus=False, historized=False, + precision=None): Type.__init__(self, validator, multiplicity, index, default, optional, editDefault, show, page, group, move, indexed, False, specificReadPermission, specificWritePermission, width, height, master, masterValue, focus, historized) self.pythonType = float + # The precision is the number of decimal digits. This number is used + # for rendering the float, but the internal float representation is not + # rounded. + self.precision = precision class String(Type): # Some predefined regular expressions that may be used as validators diff --git a/gen/plone25/mixins/__init__.py b/gen/plone25/mixins/__init__.py index 0a4da9a..50a3d1f 100644 --- a/gen/plone25/mixins/__init__.py +++ b/gen/plone25/mixins/__init__.py @@ -262,6 +262,12 @@ class AbstractMixin: elif vType == 'Boolean': if v: return self.translate('yes', domain='plone') else: return self.translate('no', domain='plone') + elif vType == 'Float': + if appyType['precision'] == None: + v = str(v) + else: + format = '%%.%df' % appyType['precision'] + v = format % v return v def getAppyType(self, fieldName, forward=True, asDict=True): diff --git a/gen/plone25/skin/macros.pt b/gen/plone25/skin/macros.pt index f407dc3..bfc4874 100644 --- a/gen/plone25/skin/macros.pt +++ b/gen/plone25/skin/macros.pt @@ -121,6 +121,14 @@ + + + + + - + + + + diff --git a/gen/plone25/wrappers/__init__.py b/gen/plone25/wrappers/__init__.py index 54d54d5..35faa0a 100644 --- a/gen/plone25/wrappers/__init__.py +++ b/gen/plone25/wrappers/__init__.py @@ -350,7 +350,8 @@ class AbstractWrapper: if toDisk and not at: at = getOsTempFolder() + '/' + self.o.UID() + '.xml' # Create the XML version of the object - xml = XmlMarshaller(cdata=True).marshall(self.o, objectType='appy') + xml = XmlMarshaller(cdata=True, dumpUnicode=True).marshall( + self.o, objectType='appy') # Produce the desired result if toDisk: f = file(at, 'w') diff --git a/shared/xml_parser.py b/shared/xml_parser.py index e2372fe..76b3307 100755 --- a/shared/xml_parser.py +++ b/shared/xml_parser.py @@ -336,16 +336,22 @@ class XmlMarshaller: fieldsToExclude = [] atFiles = ('image', 'file') # Types of archetypes fields that contain files. - def __init__(self, cdata=False): + def __init__(self, cdata=False, dumpUnicode=False): '''If p_cdata is True, all string values will be dumped as XML CDATA.''' self.cdata = cdata + self.dumpUnicode = dumpUnicode def dumpString(self, res, s): '''Dumps a string into the result.''' - if self.cdata: res.write('') + if hasattr(self, 'cdata') and self.cdata: res.write(']]>') def dumpFile(self, res, v): '''Dumps a file into the result.''' + if not v: return # p_value contains the (possibly binary) content of a file. We will # encode it in Base64, in one or several parts. res.write('')