[gen] Boolean field can now be rendered as 2 radio buttons.
This commit is contained in:
parent
f8d5cb546d
commit
6770d23a50
|
@ -23,29 +23,52 @@ from appy.gen.layout import Table
|
||||||
class Boolean(Field):
|
class Boolean(Field):
|
||||||
'''Field for storing boolean values.'''
|
'''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>
|
pxView = pxCell = Px('''<x>:value</x>
|
||||||
<input type="hidden" if="masterCss"
|
<input type="hidden" if="masterCss"
|
||||||
class=":masterCss" value=":rawValue" name=":name" id=":name"/>''')
|
class=":masterCss" value=":rawValue" name=":name" id=":name"/>''')
|
||||||
|
|
||||||
pxEdit = Px('''
|
pxEdit = Px('''<x var="isTrue=field.isTrue(zobj, rawValue)">
|
||||||
<x var="isChecked=field.isChecked(zobj, rawValue)">
|
<x if="field.render == 'checkbox'">
|
||||||
<input type="checkbox" name=":name + '_visible'" id=":name"
|
<input type="checkbox" name=":name + '_visible'" id=":name"
|
||||||
class=":masterCss" checked=":isChecked"
|
class=":masterCss" checked=":isTrue"
|
||||||
onclick=":'toggleCheckbox(%s, %s); %s' % (q(name), \
|
onclick=":'toggleCheckbox(%s, %s); %s' % (q(name), \
|
||||||
q('%s_hidden' % name), \
|
q('%s_hidden' % name), \
|
||||||
field.getOnChange(zobj, layoutType))"/>
|
field.getOnChange(zobj, layoutType))"/>
|
||||||
<input type="hidden" name=":name" id=":'%s_hidden' % name"
|
<input type="hidden" name=":name" id=":'%s_hidden' % name"
|
||||||
value=":isChecked and 'True' or 'False'"/>
|
value=":isTrue and 'True' or 'False'"/>
|
||||||
</x>''')
|
</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">
|
pxSearch = Px('''<x var="typedWidget='%s*bool' % widgetName">
|
||||||
<x var="valueId='%s_yes' % name">
|
<x var="valueId='%s_yes' % name">
|
||||||
<input type="radio" value="True" name=":typedWidget" id=":valueId"/>
|
<input type="radio" value="True" name=":typedWidget" id=":valueId"/>
|
||||||
<label lfor=":valueId">:_('yes')</label>
|
<label lfor=":valueId">:_(field.getValueLabel(True))</label>
|
||||||
</x>
|
</x>
|
||||||
<x var="valueId='%s_no' % name">
|
<x var="valueId='%s_no' % name">
|
||||||
<input type="radio" value="False" name=":typedWidget" id=":valueId"/>
|
<input type="radio" value="False" name=":typedWidget" id=":valueId"/>
|
||||||
<label lfor=":valueId">:_('no')</label>
|
<label lfor=":valueId">:_(field.getValueLabel(False))</label>
|
||||||
</x>
|
</x>
|
||||||
<x var="valueId='%s_whatever' % name">
|
<x var="valueId='%s_whatever' % name">
|
||||||
<input type="radio" value="" name=":typedWidget" id=":valueId"
|
<input type="radio" value="" name=":typedWidget" id=":valueId"
|
||||||
|
@ -60,7 +83,10 @@ class Boolean(Field):
|
||||||
maxChars=None, colspan=1, master=None, masterValue=None,
|
maxChars=None, colspan=1, master=None, masterValue=None,
|
||||||
focus=False, historized=False, mapping=None, label=None,
|
focus=False, historized=False, mapping=None, label=None,
|
||||||
sdefault=False, scolspan=1, swidth=None, sheight=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,
|
Field.__init__(self, validator, multiplicity, default, show, page,
|
||||||
group, layouts, move, indexed, searchable,
|
group, layouts, move, indexed, searchable,
|
||||||
specificReadPermission, specificWritePermission, width,
|
specificReadPermission, specificWritePermission, width,
|
||||||
|
@ -69,14 +95,8 @@ class Boolean(Field):
|
||||||
sheight, persist)
|
sheight, persist)
|
||||||
self.pythonType = bool
|
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):
|
def getDefaultLayouts(self):
|
||||||
return {'view': 'lf', 'edit': Table('f;lrv;=', width=None),
|
return (self.render == 'radios') and self.rLayouts or self.bLayouts
|
||||||
'search': 'l-f'}
|
|
||||||
|
|
||||||
def getValue(self, obj):
|
def getValue(self, obj):
|
||||||
'''Never returns "None". Returns always "True" or "False", even if
|
'''Never returns "None". Returns always "True" or "False", even if
|
||||||
|
@ -85,22 +105,28 @@ class Boolean(Field):
|
||||||
if value == None: return False
|
if value == None: return False
|
||||||
return value
|
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):
|
def getFormattedValue(self, obj, value, showChanges=False):
|
||||||
if value: res = obj.translate('yes')
|
return obj.translate(self.getValueLabel(value))
|
||||||
else: res = obj.translate('no')
|
|
||||||
return res
|
|
||||||
|
|
||||||
def getStorableValue(self, value):
|
def getStorableValue(self, value):
|
||||||
if not self.isEmptyValue(value):
|
if not self.isEmptyValue(value):
|
||||||
exec 'res = %s' % value
|
exec 'res = %s' % value
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def isChecked(self, obj, dbValue):
|
def isTrue(self, obj, dbValue):
|
||||||
'''When rendering this field as a checkbox, must it be checked or
|
'''When rendering this field as a checkbox, must it be checked or
|
||||||
not?'''
|
not?'''
|
||||||
rq = obj.REQUEST
|
rq = obj.REQUEST
|
||||||
# Get the value we must compare (from request or from database)
|
# Get the value we must compare (from request or from database)
|
||||||
if rq.has_key(self.name):
|
if rq.has_key(self.name):
|
||||||
return rq.get(self.name) in ('True', 1, '1')
|
return rq.get(self.name) in ('True', 1, '1')
|
||||||
return dbValue
|
return dbValue
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
|
@ -273,7 +273,7 @@ class FieldDescriptor:
|
||||||
return msgId, default, niceDefault
|
return msgId, default, niceDefault
|
||||||
|
|
||||||
def walkString(self):
|
def walkString(self):
|
||||||
'''How to generate an Appy String?'''
|
'''Generates String-specific i18n labels.'''
|
||||||
if self.appyType.isSelect and \
|
if self.appyType.isSelect and \
|
||||||
(type(self.appyType.validator) in (list, tuple)):
|
(type(self.appyType.validator) in (list, tuple)):
|
||||||
# Generate i18n messages for every possible value if the list
|
# Generate i18n messages for every possible value if the list
|
||||||
|
@ -283,8 +283,15 @@ class FieldDescriptor:
|
||||||
self.fieldName, value)
|
self.fieldName, value)
|
||||||
self.i18n(label, 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):
|
def walkAction(self):
|
||||||
'''Generates the i18n-related label.'''
|
'''Generates Action-specific i18n labels.'''
|
||||||
if self.appyType.confirm:
|
if self.appyType.confirm:
|
||||||
label = '%s_%s_confirm' % (self.classDescr.name, self.fieldName)
|
label = '%s_%s_confirm' % (self.classDescr.name, self.fieldName)
|
||||||
self.i18n(label, po.CONFIRM, nice=False)
|
self.i18n(label, po.CONFIRM, nice=False)
|
||||||
|
@ -351,6 +358,8 @@ class FieldDescriptor:
|
||||||
group.generateLabels(self.generator.labels, self.classDescr, set())
|
group.generateLabels(self.generator.labels, self.classDescr, set())
|
||||||
# Manage things which are specific to String types
|
# Manage things which are specific to String types
|
||||||
if self.appyType.type == 'String': self.walkString()
|
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
|
# Manage things which are specific to Actions
|
||||||
elif self.appyType.type == 'Action': self.walkAction()
|
elif self.appyType.type == 'Action': self.walkAction()
|
||||||
# Manage things which are specific to Ref types
|
# Manage things which are specific to Ref types
|
||||||
|
|
|
@ -207,6 +207,7 @@ class ZopeInstaller:
|
||||||
translations = [t.o.id for t in appyTool.translations]
|
translations = [t.o.id for t in appyTool.translations]
|
||||||
# We browse the languages supported by this application and check
|
# We browse the languages supported by this application and check
|
||||||
# whether we need to create the corresponding Translation objects.
|
# whether we need to create the corresponding Translation objects.
|
||||||
|
done = []
|
||||||
for language in self.languages:
|
for language in self.languages:
|
||||||
if language in translations: continue
|
if language in translations: continue
|
||||||
# We will create, in the tool, the translation object for this
|
# We will create, in the tool, the translation object for this
|
||||||
|
@ -218,7 +219,8 @@ class ZopeInstaller:
|
||||||
title = langEn
|
title = langEn
|
||||||
appyTool.create('translations', noSecurity=True,
|
appyTool.create('translations', noSecurity=True,
|
||||||
id=language, title=title)
|
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
|
# Synchronizes, if required, every Translation object with the
|
||||||
# corresponding "po" file on disk.
|
# corresponding "po" file on disk.
|
||||||
|
@ -226,14 +228,16 @@ class ZopeInstaller:
|
||||||
appFolder = self.config.diskFolder
|
appFolder = self.config.diskFolder
|
||||||
appName = self.config.PROJECTNAME
|
appName = self.config.PROJECTNAME
|
||||||
i18nFolder = os.path.join(appFolder, 'tr')
|
i18nFolder = os.path.join(appFolder, 'tr')
|
||||||
|
done = []
|
||||||
for translation in appyTool.translations:
|
for translation in appyTool.translations:
|
||||||
# Get the "po" file
|
# Get the "po" file
|
||||||
poName = '%s-%s.po' % (appName, translation.id)
|
poName = '%s-%s.po' % (appName, translation.id)
|
||||||
poFile = PoParser(os.path.join(i18nFolder, poName)).parse()
|
poFile = PoParser(os.path.join(i18nFolder, poName)).parse()
|
||||||
for message in poFile.messages:
|
for message in poFile.messages:
|
||||||
setattr(translation, message.id, message.getMessage())
|
setattr(translation, message.id, message.getMessage())
|
||||||
appyTool.log('Translation "%s" updated from "%s".' % \
|
done.append(translation.id)
|
||||||
(translation.id, poName))
|
appyTool.log('Translation(s) %s updated (%s messages).' % \
|
||||||
|
(', '.join(done), len(poFile.messages)))
|
||||||
|
|
||||||
# Execute custom installation code if any.
|
# Execute custom installation code if any.
|
||||||
if hasattr(appyTool, 'onInstall'): appyTool.onInstall()
|
if hasattr(appyTool, 'onInstall'): appyTool.onInstall()
|
||||||
|
@ -316,7 +320,6 @@ class ZopeInstaller:
|
||||||
install_product(self.app, Products.__path__[1], 'ZCTextIndex', [], {})
|
install_product(self.app, Products.__path__[1], 'ZCTextIndex', [], {})
|
||||||
|
|
||||||
def install(self):
|
def install(self):
|
||||||
self.logger.info('is being installed...')
|
|
||||||
self.installDependencies()
|
self.installDependencies()
|
||||||
self.patchZope()
|
self.patchZope()
|
||||||
self.installRoles()
|
self.installRoles()
|
||||||
|
|
|
@ -160,13 +160,9 @@ class Table:
|
||||||
|
|
||||||
def addCssClasses(self, css_class):
|
def addCssClasses(self, css_class):
|
||||||
'''Adds a single or a group of p_css_class.'''
|
'''Adds a single or a group of p_css_class.'''
|
||||||
classes = self.css_class
|
if not self.css_class: self.css_class = css_class
|
||||||
if classes == None:
|
|
||||||
classes = ''
|
|
||||||
if not classes:
|
|
||||||
self.css_class = css_class
|
|
||||||
else:
|
else:
|
||||||
self.css_class += ' ' + css_classes
|
self.css_class += ' ' + css_class
|
||||||
# Ensures that every class appears once
|
# Ensures that every class appears once
|
||||||
self.css_class = ' '.join(set(self.css_class.split()))
|
self.css_class = ' '.join(set(self.css_class.split()))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue