prefill with a formula
This commit is contained in:
parent
4da41a2b3b
commit
14aa8ff0fd
|
@ -49,8 +49,11 @@ class PrefillSelectionWidget(CompositeWidget):
|
|||
if not value:
|
||||
value = {}
|
||||
|
||||
options = [('none', _('None')), ('string', _('String')),
|
||||
('user', _('User Field')), ('wsf', _('WSF Expression'))]
|
||||
options = [('none', _('None')),
|
||||
('string', _('String')),
|
||||
('formula', _('Formula (Python)')),
|
||||
('user', _('User Field')),
|
||||
('wsf', _('WSF Expression'))]
|
||||
|
||||
self.add(SingleSelectWidget, 'type', options = options, value = value.get('type'))
|
||||
|
||||
|
@ -58,7 +61,7 @@ class PrefillSelectionWidget(CompositeWidget):
|
|||
if not self.value:
|
||||
self.value = {}
|
||||
|
||||
if self.value.get('type') == 'string':
|
||||
if self.value.get('type') in ('string', 'formula'):
|
||||
self.add(StringWidget, 'value', value = value.get('value'))
|
||||
elif self.value.get('type') == 'user':
|
||||
formdef = get_publisher().user_class.get_formdef()
|
||||
|
|
|
@ -432,11 +432,24 @@ class FormPage(Directory):
|
|||
if userfield.id == x:
|
||||
v = user.form_data.get(x)
|
||||
break
|
||||
|
||||
if v:
|
||||
prefilled = True
|
||||
form.get_widget('f%s' % k).set_error(
|
||||
_('Value has been automatically prefilled.'))
|
||||
elif t == 'formula':
|
||||
# create a fake FormData to feed variables
|
||||
formdata = FormData()
|
||||
formdata._formdef = self.formdef
|
||||
formdata.user = get_request().user
|
||||
formdata.data = data
|
||||
formdata.status = str('')
|
||||
get_publisher().substitutions.feed(formdata)
|
||||
formula = field.prefill.get('value')
|
||||
variables = get_publisher().substitutions.get_context_variables()
|
||||
try:
|
||||
v = str(eval(formula, variables))
|
||||
except:
|
||||
pass
|
||||
if v:
|
||||
prefilled = True
|
||||
form.get_widget('f%s' % k).set_error(
|
||||
_('Value has been automatically prefilled.'))
|
||||
|
||||
if not prefilled and form.get_widget('f%s' % k):
|
||||
form.get_widget('f%s' % k).clear_error()
|
||||
|
|
Loading…
Reference in New Issue