Finalized implementation of attribute String.transform.

This commit is contained in:
Gaetan Delannay 2010-11-23 17:25:00 +01:00
parent 0e83a6f490
commit aea19a819e
2 changed files with 33 additions and 1 deletions

View file

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
import re, time, copy, sys, types, os, os.path import re, time, copy, sys, types, os, os.path
from appy.shared.utils import Traceback from appy.shared.utils import Traceback
@ -1170,6 +1171,35 @@ class String(Type):
# Check that the value is among possible values # Check that the value is among possible values
if error: obj.translate('bad_select_value') if error: obj.translate('bad_select_value')
accents = {'é':'e','è':'e','ê':'e','ë':'e','à':'a','â':'a','ä':'a',
'ù':'u','û':'u','ü':'u','î':'i','ï':'i','ô':'o','ö':'o',
'ç':'c', 'Ç':'C',
'Ù':'U','Û':'U','Ü':'U','Î':'I','Ï':'I','Ô':'O','Ö':'O',
'É':'E','È':'E','Ê':'E','Ë':'E','À':'A','Â':'A','Ä':'A'}
def applyTransform(self, value):
'''Applies a transform as required by self.transform on single
value p_value.'''
if self.transform in ('uppercase', 'lowercase'):
# For those transforms, I will remove any accent, because
# (1) 'é'.upper() or 'Ê'.lower() has no effect;
# (2) most of the time, if the user wants to apply such effect, it
# is for ease of data manipulation, so I guess without accent.
for c, n in self.accents.iteritems():
if c in value: value = value.replace(c, n)
# Apply the transform
if self.transform == 'lowercase': return value.lower()
elif self.transform == 'uppercase': return value.upper()
elif self.transform == 'capitalize': return value.capitalize()
return value
def getStorableValue(self, value):
if not self.isEmptyValue(value) and (self.transform != 'none'):
if isinstance(value, basestring):
return self.applyTransform(value)
else:
return [self.applyTransform(v) for v in value]
return value
def store(self, obj, value): def store(self, obj, value):
if self.isMultiValued() and isinstance(value, basestring): if self.isMultiValued() and isinstance(value, basestring):
value = [value] value = [value]

View file

@ -43,12 +43,14 @@
<tal:line condition="python: isOneLine and not isSelect"> <tal:line condition="python: isOneLine and not isSelect">
<input tal:attributes="id name; name name; size widget/width; <input tal:attributes="id name; name name; size widget/width;
value python: test(inRequest, requestValue, value); value python: test(inRequest, requestValue, value);
style python: 'text-transform:%s' % widget['transform'];
type python: (widget['format'] == 3) and 'password' or 'text'"/> type python: (widget['format'] == 3) and 'password' or 'text'"/>
</tal:line> </tal:line>
<tal:textarea condition="python: fmt == 1"> <tal:textarea condition="python: fmt == 1">
<textarea tal:attributes="id name; name name; <textarea tal:attributes="id name; name name;
cols widget/width; cols widget/width;
rows widget/height;" rows widget/height;
style python: 'text-transform:%s' % widget['transform'];"
tal:content="python: test(inRequest, requestValue, value)"> tal:content="python: test(inRequest, requestValue, value)">
</textarea> </textarea>
<input type="hidden" value="text/plain" originalvalue="text/plain" <input type="hidden" value="text/plain" originalvalue="text/plain"