Management of floats with a given precision; XmlMarshaller can dump unicode or str result.
This commit is contained in:
parent
3bb907ca5d
commit
2e1c6a6999
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -121,6 +121,14 @@
|
|||
<span tal:replace="v"></span>
|
||||
</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"
|
||||
tal:define="v python: contextObj.getAppyValue(field.getName(), appyType);
|
||||
fmt python: appyType['format'];
|
||||
|
@ -179,7 +187,10 @@
|
|||
<tal:string condition="python: appyType['type'] == 'String'">
|
||||
<metal:showString use-macro="here/skyn/macros/macros/showStringField"/>
|
||||
</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"
|
||||
tal:attributes="class python: 'appyLabel ' + contextObj.getCssClasses(appyType, asSlave=False);
|
||||
id python: field.getAccessor(contextObj)()"></span>
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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('<![CDATA[')
|
||||
if hasattr(self, 'cdata') and self.cdata: res.write('<![CDATA[')
|
||||
# Try to solve encoding problems
|
||||
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:
|
||||
pass
|
||||
# Replace special chars by XML entities
|
||||
|
@ -354,10 +360,11 @@ class XmlMarshaller:
|
|||
res.write(self.xmlEntities[c])
|
||||
else:
|
||||
res.write(c)
|
||||
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('<part type="base64" number="1">')
|
||||
|
|
Loading…
Reference in a new issue