Management of floats with a given precision; XmlMarshaller can dump unicode or str result.

This commit is contained in:
Gaetan Delannay 2010-01-29 11:28:39 +01:00
parent 3bb907ca5d
commit 2e1c6a6999
5 changed files with 37 additions and 7 deletions

View file

@ -158,12 +158,17 @@ class Float(Type):
page='main', group=None, move=0, indexed=False, page='main', group=None, move=0, indexed=False,
searchable=False, specificReadPermission=False, searchable=False, specificReadPermission=False,
specificWritePermission=False, width=None, height=None, 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, Type.__init__(self, validator, multiplicity, index, default, optional,
editDefault, show, page, group, move, indexed, False, editDefault, show, page, group, move, indexed, False,
specificReadPermission, specificWritePermission, width, specificReadPermission, specificWritePermission, width,
height, master, masterValue, focus, historized) height, master, masterValue, focus, historized)
self.pythonType = float 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): class String(Type):
# Some predefined regular expressions that may be used as validators # Some predefined regular expressions that may be used as validators

View file

@ -262,6 +262,12 @@ class AbstractMixin:
elif vType == 'Boolean': elif vType == 'Boolean':
if v: return self.translate('yes', domain='plone') if v: return self.translate('yes', domain='plone')
else: return self.translate('no', 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 return v
def getAppyType(self, fieldName, forward=True, asDict=True): def getAppyType(self, fieldName, forward=True, asDict=True):

View file

@ -121,6 +121,14 @@
<span tal:replace="v"></span> <span tal:replace="v"></span>
</metal:showDate> </metal:showDate>
<metal:showFloat define-macro="showFloatField"
tal:define="v python: contextObj.getAppyValue(field.getName(), appyType)">
<span tal:condition="showLabel" tal:content="label"
tal:attributes="class python: 'appyLabel ' + contextObj.getCssClasses(appyType, asSlave=False);
id python: v"></span>
<span tal:replace="v"></span>
</metal:showFloat>
<metal:showString define-macro="showStringField" <metal:showString define-macro="showStringField"
tal:define="v python: contextObj.getAppyValue(field.getName(), appyType); tal:define="v python: contextObj.getAppyValue(field.getName(), appyType);
fmt python: appyType['format']; fmt python: appyType['format'];
@ -179,7 +187,10 @@
<tal:string condition="python: appyType['type'] == 'String'"> <tal:string condition="python: appyType['type'] == 'String'">
<metal:showString use-macro="here/skyn/macros/macros/showStringField"/> <metal:showString use-macro="here/skyn/macros/macros/showStringField"/>
</tal:string> </tal:string>
<tal:simpleField condition="python: (appyType['type'] in ('Integer', 'Float', 'Boolean'))"> <tal:float condition="python: appyType['type'] == 'Float'">
<metal:showFloat use-macro="here/skyn/macros/macros/showFloatField"/>
</tal:float>
<tal:simpleField condition="python: (appyType['type'] in ('Integer', 'Boolean'))">
<span tal:condition="showLabel" tal:content="label" <span tal:condition="showLabel" tal:content="label"
tal:attributes="class python: 'appyLabel ' + contextObj.getCssClasses(appyType, asSlave=False); tal:attributes="class python: 'appyLabel ' + contextObj.getCssClasses(appyType, asSlave=False);
id python: field.getAccessor(contextObj)()"></span> id python: field.getAccessor(contextObj)()"></span>

View file

@ -350,7 +350,8 @@ class AbstractWrapper:
if toDisk and not at: if toDisk and not at:
at = getOsTempFolder() + '/' + self.o.UID() + '.xml' at = getOsTempFolder() + '/' + self.o.UID() + '.xml'
# Create the XML version of the object # 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 # Produce the desired result
if toDisk: if toDisk:
f = file(at, 'w') f = file(at, 'w')

View file

@ -336,16 +336,22 @@ class XmlMarshaller:
fieldsToExclude = [] fieldsToExclude = []
atFiles = ('image', 'file') # Types of archetypes fields that contain files. 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.''' '''If p_cdata is True, all string values will be dumped as XML CDATA.'''
self.cdata = cdata self.cdata = cdata
self.dumpUnicode = dumpUnicode
def dumpString(self, res, s): def dumpString(self, res, s):
'''Dumps a string into the result.''' '''Dumps a string into the result.'''
if self.cdata: res.write('<![CDATA[') if hasattr(self, 'cdata') and self.cdata: res.write('<![CDATA[')
# Try to solve encoding problems # Try to solve encoding problems
try: try:
s = s.decode('utf-8') if hasattr(self, 'dumpUnicode') and self.dumpUnicode:
# Produce a unicode
s = s.decode('utf-8')
else:
# Produce a str
s = s.decode('utf-8').encode('utf-8')
except UnicodeEncodeError: except UnicodeEncodeError:
pass pass
# Replace special chars by XML entities # Replace special chars by XML entities
@ -354,10 +360,11 @@ class XmlMarshaller:
res.write(self.xmlEntities[c]) res.write(self.xmlEntities[c])
else: else:
res.write(c) res.write(c)
if self.cdata: res.write(']]>') if hasattr(self, 'cdata') and self.cdata: res.write(']]>')
def dumpFile(self, res, v): def dumpFile(self, res, v):
'''Dumps a file into the result.''' '''Dumps a file into the result.'''
if not v: return
# p_value contains the (possibly binary) content of a file. We will # p_value contains the (possibly binary) content of a file. We will
# encode it in Base64, in one or several parts. # encode it in Base64, in one or several parts.
res.write('<part type="base64" number="1">') res.write('<part type="base64" number="1">')