[gen] Boolean field can now be rendered as 2 radio buttons.

This commit is contained in:
Gaetan Delannay 2014-08-08 12:36:19 +02:00
parent f8d5cb546d
commit 6770d23a50
4 changed files with 66 additions and 32 deletions

View file

@ -23,29 +23,52 @@ from appy.gen.layout import Table
class Boolean(Field):
'''Field for storing boolean values.'''
yesNo = {'true': 'yes', 'false': 'no', True: 'yes', False: 'no'}
trueFalse = {True: 'true', False: 'false'}
# Default layout (render = "checkbox") ("b" stands for "base").
bLayouts = {'view': 'lf', 'edit': Table('f;lrv;=', width=None),
'search': 'l-f'}
# Layout including a description.
dLayouts = {'view': 'lf', 'edit': Table('flrv;=d', width=None)}
# Centered layout, no description.
cLayouts = {'view': 'lf|', 'edit': 'flrv|'}
# Layout for radio buttons (render = "radios")
rLayouts = {'edit': 'f', 'view': 'f', 'search': 'l-f'}
pxView = pxCell = Px('''<x>:value</x>
<input type="hidden" if="masterCss"
class=":masterCss" value=":rawValue" name=":name" id=":name"/>''')
pxEdit = Px('''
<x var="isChecked=field.isChecked(zobj, rawValue)">
pxEdit = Px('''<x var="isTrue=field.isTrue(zobj, rawValue)">
<x if="field.render == 'checkbox'">
<input type="checkbox" name=":name + '_visible'" id=":name"
class=":masterCss" checked=":isChecked"
class=":masterCss" checked=":isTrue"
onclick=":'toggleCheckbox(%s, %s); %s' % (q(name), \
q('%s_hidden' % name), \
field.getOnChange(zobj, layoutType))"/>
<input type="hidden" name=":name" id=":'%s_hidden' % name"
value=":isChecked and 'True' or 'False'"/>
</x>''')
value=":isTrue and 'True' or 'False'"/>
</x>
<x if="field.render == 'radios'"
var2="falseId='%s_false' % name;
trueId='%s_true' % name">
<input type="radio" name=":name" id=":falseId" class=":masterCss"
value="False" checked=":not isTrue"/>
<label lfor=":falseId">:_(field.labelId + '_false')</label><br/>
<input type="radio" name=":name" id=":trueId" class=":masterCss"
value="True" checked=":isTrue"/>
<label lfor=":trueId">:_(field.labelId + '_true')</label>
</x></x>''')
pxSearch = Px('''<x var="typedWidget='%s*bool' % widgetName">
<x var="valueId='%s_yes' % name">
<input type="radio" value="True" name=":typedWidget" id=":valueId"/>
<label lfor=":valueId">:_('yes')</label>
<label lfor=":valueId">:_(field.getValueLabel(True))</label>
</x>
<x var="valueId='%s_no' % name">
<input type="radio" value="False" name=":typedWidget" id=":valueId"/>
<label lfor=":valueId">:_('no')</label>
<label lfor=":valueId">:_(field.getValueLabel(False))</label>
</x>
<x var="valueId='%s_whatever' % name">
<input type="radio" value="" name=":typedWidget" id=":valueId"
@ -60,7 +83,10 @@ class Boolean(Field):
maxChars=None, colspan=1, master=None, masterValue=None,
focus=False, historized=False, mapping=None, label=None,
sdefault=False, scolspan=1, swidth=None, sheight=None,
persist=True):
persist=True, render='checkbox'):
# By default, a boolean is edited via a checkbox. It can also be edited
# via 2 radio buttons (p_render="radios").
self.render = render
Field.__init__(self, validator, multiplicity, default, show, page,
group, layouts, move, indexed, searchable,
specificReadPermission, specificWritePermission, width,
@ -69,14 +95,8 @@ class Boolean(Field):
sheight, persist)
self.pythonType = bool
# Layout including a description
dLayouts = {'view': 'lf', 'edit': Table('flrv;=d', width=None)}
# Centered layout, no description
cLayouts = {'view': 'lf|', 'edit': 'flrv|'}
def getDefaultLayouts(self):
return {'view': 'lf', 'edit': Table('f;lrv;=', width=None),
'search': 'l-f'}
return (self.render == 'radios') and self.rLayouts or self.bLayouts
def getValue(self, obj):
'''Never returns "None". Returns always "True" or "False", even if
@ -85,17 +105,23 @@ class Boolean(Field):
if value == None: return False
return value
def getValueLabel(self, value):
'''Returns the label for p_value (True or False): if self.render is
"checkbox", the label is simply the translated version of "yes" or
"no"; if self.render is "radios", there are specific labels.'''
if self.render == 'radios':
return '%s_%s' % (self.labelId, self.trueFalse[value])
return self.yesNo[value]
def getFormattedValue(self, obj, value, showChanges=False):
if value: res = obj.translate('yes')
else: res = obj.translate('no')
return res
return obj.translate(self.getValueLabel(value))
def getStorableValue(self, value):
if not self.isEmptyValue(value):
exec 'res = %s' % value
return res
def isChecked(self, obj, dbValue):
def isTrue(self, obj, dbValue):
'''When rendering this field as a checkbox, must it be checked or
not?'''
rq = obj.REQUEST

View file

@ -273,7 +273,7 @@ class FieldDescriptor:
return msgId, default, niceDefault
def walkString(self):
'''How to generate an Appy String?'''
'''Generates String-specific i18n labels.'''
if self.appyType.isSelect and \
(type(self.appyType.validator) in (list, tuple)):
# Generate i18n messages for every possible value if the list
@ -283,8 +283,15 @@ class FieldDescriptor:
self.fieldName, value)
self.i18n(label, value)
def walkBoolean(self):
'''Generates Boolean-specific i18n labels.'''
if self.appyType.render == 'radios':
for v in ('true', 'false'):
label = '%s_%s_%s' % (self.classDescr.name, self.fieldName, v)
self.i18n(label, self.appyType.yesNo[v])
def walkAction(self):
'''Generates the i18n-related label.'''
'''Generates Action-specific i18n labels.'''
if self.appyType.confirm:
label = '%s_%s_confirm' % (self.classDescr.name, self.fieldName)
self.i18n(label, po.CONFIRM, nice=False)
@ -351,6 +358,8 @@ class FieldDescriptor:
group.generateLabels(self.generator.labels, self.classDescr, set())
# Manage things which are specific to String types
if self.appyType.type == 'String': self.walkString()
# Manage things which are specific to Boolean types
if self.appyType.type == 'Boolean': self.walkBoolean()
# Manage things which are specific to Actions
elif self.appyType.type == 'Action': self.walkAction()
# Manage things which are specific to Ref types

View file

@ -207,6 +207,7 @@ class ZopeInstaller:
translations = [t.o.id for t in appyTool.translations]
# We browse the languages supported by this application and check
# whether we need to create the corresponding Translation objects.
done = []
for language in self.languages:
if language in translations: continue
# We will create, in the tool, the translation object for this
@ -218,7 +219,8 @@ class ZopeInstaller:
title = langEn
appyTool.create('translations', noSecurity=True,
id=language, title=title)
appyTool.log('Translation object created for "%s".' % language)
done.append(language)
if done: appyTool.log('Translations created for %s.' % ', '.join(done))
# Synchronizes, if required, every Translation object with the
# corresponding "po" file on disk.
@ -226,14 +228,16 @@ class ZopeInstaller:
appFolder = self.config.diskFolder
appName = self.config.PROJECTNAME
i18nFolder = os.path.join(appFolder, 'tr')
done = []
for translation in appyTool.translations:
# Get the "po" file
poName = '%s-%s.po' % (appName, translation.id)
poFile = PoParser(os.path.join(i18nFolder, poName)).parse()
for message in poFile.messages:
setattr(translation, message.id, message.getMessage())
appyTool.log('Translation "%s" updated from "%s".' % \
(translation.id, poName))
done.append(translation.id)
appyTool.log('Translation(s) %s updated (%s messages).' % \
(', '.join(done), len(poFile.messages)))
# Execute custom installation code if any.
if hasattr(appyTool, 'onInstall'): appyTool.onInstall()
@ -316,7 +320,6 @@ class ZopeInstaller:
install_product(self.app, Products.__path__[1], 'ZCTextIndex', [], {})
def install(self):
self.logger.info('is being installed...')
self.installDependencies()
self.patchZope()
self.installRoles()

View file

@ -160,13 +160,9 @@ class Table:
def addCssClasses(self, css_class):
'''Adds a single or a group of p_css_class.'''
classes = self.css_class
if classes == None:
classes = ''
if not classes:
self.css_class = css_class
if not self.css_class: self.css_class = css_class
else:
self.css_class += ' ' + css_classes
self.css_class += ' ' + css_class
# Ensures that every class appears once
self.css_class = ' '.join(set(self.css_class.split()))