Add support for substitution variables to workflows.

- This allows variables to be used in exported models (#465)
- This allows access to the previous status in emails (#447)
- This allows a workflow field to reference a form field (#248)
- This allows a workflow field to reference a user field (#249)
This commit is contained in:
Frédéric Péters 2011-06-27 08:49:29 +00:00
parent 32584ce182
commit bd4d3b4519
1 changed files with 19 additions and 4 deletions

View File

@ -288,6 +288,17 @@ class WorkflowStatusItem:
if widget:
setattr(self, f, widget.parse())
def compute(self, var):
if not isinstance(var, basestring):
return var
if not var.startswith('='):
return var
vars = get_publisher().substitutions.get_context_variables()
try:
return eval(var[1:], vars)
except:
return var
def __getstate__(self):
odict = self.__dict__.copy()
if odict.has_key('parent'):
@ -501,13 +512,13 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem):
url = formdata.get_url()
try:
mail_body = template_on_formdata(formdata, self.body)
mail_body = template_on_formdata(formdata, self.compute(self.body))
except ezt.EZTException:
get_logger().error('error in template for email body [%s], mail could not be generated' % url)
return
try:
mail_subject = template_on_formdata(formdata, self.subject)
mail_subject = template_on_formdata(formdata, self.compute(self.subject))
except ezt.EZTException:
get_logger().error('error in template for email subject [%s], mail could not be generated' % url)
return
@ -522,6 +533,8 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem):
addresses = []
for dest in self.to:
dest = self.compute(dest)
if '@' in str(dest):
addresses.append(dest)
continue
@ -575,6 +588,7 @@ register_item_class(SendmailWorkflowStatusItem)
def template_on_formdata(formdata, template, process=None):
dict = {}
dict.update(get_publisher().substitutions.get_context_variables())
dict['url'] = formdata.get_url()
dict['url_status'] = '%sstatus' % formdata.get_url()
dict['details'] = formdata.formdef.get_detailed_email_form(formdata, dict['url'])
@ -630,7 +644,7 @@ class SendSMSWorkflowStatusItem(WorkflowStatusItem):
return
try:
sms_body = template_on_formdata(formdata, self.body)
sms_body = template_on_formdata(formdata, self.compute(self.body))
except ezt.EZTException:
url = formdata.get_url()
get_logger().error('error in template for sms [%s], sms could not be generated' % url)
@ -641,7 +655,7 @@ class SendSMSWorkflowStatusItem(WorkflowStatusItem):
sms_cfg = get_cfg('sms', {})
sender = sms_cfg.get('sender', 'AuQuotidien')[:11]
try:
SMS().send(self.to, sms_body[:160], sender)
SMS().send([self.compute(x) for x in self.to], sms_body[:160], sender)
except errors.SMSError, e:
get_logger().error(e)
@ -661,6 +675,7 @@ class DisplayMessageWorkflowStatusItem(WorkflowStatusItem):
tmpl.parse(self.message)
dict = {}
dict.update(get_publisher().substitutions.get_context_variables())
dict['date'] = misc.localstrftime(filled.receipt_time)
dict['number'] = filled.id
if filled.formdef.receiver and filled.formdef.receiver.details: