2013-07-08 16:39:16 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# This file is part of Appy, a framework for building applications in the Python
|
|
|
|
# language. Copyright (C) 2007 Gaetan Delannay
|
|
|
|
|
|
|
|
# Appy is free software; you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
|
|
|
|
# Appy is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Appy. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
from appy.fields import Field
|
|
|
|
from appy.px import Px
|
|
|
|
from appy.gen.layout import Table
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class Boolean(Field):
|
|
|
|
'''Field for storing boolean values.'''
|
|
|
|
|
2014-08-08 05:36:19 -05:00
|
|
|
yesNo = {'true': 'yes', 'false': 'no', True: 'yes', False: 'no'}
|
|
|
|
trueFalse = {True: 'true', False: 'false'}
|
|
|
|
|
|
|
|
# Default layout (render = "checkbox") ("b" stands for "base").
|
2014-11-30 04:38:29 -06:00
|
|
|
bLayouts = {'view': 'lf', 'edit': Table('f;lrv;-', width=None),
|
2014-08-08 05:36:19 -05:00
|
|
|
'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'}
|
2014-08-13 10:17:25 -05:00
|
|
|
rlLayouts = {'edit': 'l-f', 'view': 'lf', 'search': 'l-f'}
|
2014-08-08 05:36:19 -05:00
|
|
|
|
2014-03-04 08:03:37 -06:00
|
|
|
pxView = pxCell = Px('''<x>:value</x>
|
|
|
|
<input type="hidden" if="masterCss"
|
|
|
|
class=":masterCss" value=":rawValue" name=":name" id=":name"/>''')
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-08-08 05:36:19 -05:00
|
|
|
pxEdit = Px('''<x var="isTrue=field.isTrue(zobj, rawValue)">
|
|
|
|
<x if="field.render == 'checkbox'">
|
2013-07-08 16:39:16 -05:00
|
|
|
<input type="checkbox" name=":name + '_visible'" id=":name"
|
2014-08-08 05:36:19 -05:00
|
|
|
class=":masterCss" checked=":isTrue"
|
2014-03-03 11:54:21 -06:00
|
|
|
onclick=":'toggleCheckbox(%s, %s); %s' % (q(name), \
|
2014-03-04 08:03:37 -06:00
|
|
|
q('%s_hidden' % name), \
|
|
|
|
field.getOnChange(zobj, layoutType))"/>
|
2013-07-08 16:39:16 -05:00
|
|
|
<input type="hidden" name=":name" id=":'%s_hidden' % name"
|
2014-08-08 05:36:19 -05:00
|
|
|
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>''')
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-03-05 06:25:36 -06:00
|
|
|
pxSearch = Px('''<x var="typedWidget='%s*bool' % widgetName">
|
2013-07-08 16:39:16 -05:00
|
|
|
<x var="valueId='%s_yes' % name">
|
2013-07-10 02:56:35 -05:00
|
|
|
<input type="radio" value="True" name=":typedWidget" id=":valueId"/>
|
2014-08-08 05:36:19 -05:00
|
|
|
<label lfor=":valueId">:_(field.getValueLabel(True))</label>
|
2013-07-08 16:39:16 -05:00
|
|
|
</x>
|
|
|
|
<x var="valueId='%s_no' % name">
|
2013-07-10 02:56:35 -05:00
|
|
|
<input type="radio" value="False" name=":typedWidget" id=":valueId"/>
|
2014-08-08 05:36:19 -05:00
|
|
|
<label lfor=":valueId">:_(field.getValueLabel(False))</label>
|
2013-07-08 16:39:16 -05:00
|
|
|
</x>
|
|
|
|
<x var="valueId='%s_whatever' % name">
|
2013-07-10 02:56:35 -05:00
|
|
|
<input type="radio" value="" name=":typedWidget" id=":valueId"
|
|
|
|
checked="checked"/>
|
|
|
|
<label lfor=":valueId">:_('whatever')</label>
|
2014-03-05 06:25:36 -06:00
|
|
|
</x><br/></x>''')
|
2013-07-08 16:39:16 -05:00
|
|
|
|
|
|
|
def __init__(self, validator=None, multiplicity=(0,1), default=None,
|
|
|
|
show=True, page='main', group=None, layouts = None, move=0,
|
2014-12-26 10:29:14 -06:00
|
|
|
indexed=False, mustIndex=True, searchable=False,
|
|
|
|
specificReadPermission=False, specificWritePermission=False,
|
|
|
|
width=None, height=None, 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, render='checkbox', view=None,
|
|
|
|
xml=None):
|
2014-08-08 05:36:19 -05:00
|
|
|
# By default, a boolean is edited via a checkbox. It can also be edited
|
|
|
|
# via 2 radio buttons (p_render="radios").
|
|
|
|
self.render = render
|
2013-07-08 16:39:16 -05:00
|
|
|
Field.__init__(self, validator, multiplicity, default, show, page,
|
2014-12-26 10:29:14 -06:00
|
|
|
group, layouts, move, indexed, mustIndex, searchable,
|
2013-07-08 16:39:16 -05:00
|
|
|
specificReadPermission, specificWritePermission, width,
|
|
|
|
height, None, colspan, master, masterValue, focus,
|
2014-03-05 09:19:11 -06:00
|
|
|
historized, mapping, label, sdefault, scolspan, swidth,
|
2014-12-09 08:19:28 -06:00
|
|
|
sheight, persist, view, xml)
|
2013-07-08 16:39:16 -05:00
|
|
|
self.pythonType = bool
|
|
|
|
|
|
|
|
def getDefaultLayouts(self):
|
2014-11-30 04:38:29 -06:00
|
|
|
cp = Field.copyLayouts
|
|
|
|
if self.render == 'radios': return cp(Boolean.rLayouts)
|
|
|
|
return cp(Boolean.bLayouts)
|
2013-07-08 16:39:16 -05:00
|
|
|
|
|
|
|
def getValue(self, obj):
|
|
|
|
'''Never returns "None". Returns always "True" or "False", even if
|
|
|
|
"None" is stored in the DB.'''
|
|
|
|
value = Field.getValue(self, obj)
|
|
|
|
if value == None: return False
|
|
|
|
return value
|
|
|
|
|
2014-08-08 05:36:19 -05:00
|
|
|
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]
|
|
|
|
|
2015-03-06 08:54:14 -06:00
|
|
|
def getFormattedValue(self, obj, value, layoutType='view',
|
|
|
|
showChanges=False, language=None):
|
2014-09-08 08:13:18 -05:00
|
|
|
return obj.translate(self.getValueLabel(value), language=language)
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-09-05 10:13:23 -05:00
|
|
|
def getStorableValue(self, obj, value):
|
|
|
|
if not self.isEmptyValue(obj, value):
|
2013-07-08 16:39:16 -05:00
|
|
|
exec 'res = %s' % value
|
|
|
|
return res
|
2013-07-10 02:56:35 -05:00
|
|
|
|
2014-08-08 05:36:19 -05:00
|
|
|
def isTrue(self, obj, dbValue):
|
2013-07-10 02:56:35 -05:00
|
|
|
'''When rendering this field as a checkbox, must it be checked or
|
|
|
|
not?'''
|
|
|
|
rq = obj.REQUEST
|
|
|
|
# Get the value we must compare (from request or from database)
|
|
|
|
if rq.has_key(self.name):
|
2014-08-08 05:36:19 -05:00
|
|
|
return rq.get(self.name) in ('True', 1, '1')
|
2013-07-10 02:56:35 -05:00
|
|
|
return dbValue
|
2013-07-08 16:39:16 -05:00
|
|
|
# ------------------------------------------------------------------------------
|