workflows: add support for templates in auto dispatch (#20211)

This commit is contained in:
Frédéric Péters 2019-07-09 21:37:49 +02:00
parent a2fc38261c
commit 7421b7fe89
2 changed files with 46 additions and 37 deletions

View File

@ -442,37 +442,40 @@ def test_dispatch_auto(pub):
role2 = Role('xxx2')
role2.store()
item.variable = 'form_var_foo'
item.rules = [
{'role_id': role1.id, 'value': 'foo'},
{'role_id': role2.id, 'value': 'bar'},
]
for variable in ('form_var_foo', '{{form_var_foo}}'):
formdata.data = {}
formdata.workflow_roles = {}
item.variable = variable
item.rules = [
{'role_id': role1.id, 'value': 'foo'},
{'role_id': role2.id, 'value': 'bar'},
]
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
assert not formdata.workflow_roles
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
assert not formdata.workflow_roles
# no match
formdata.data = {'1': 'XXX'}
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
assert not formdata.workflow_roles
# no match
formdata.data = {'1': 'XXX'}
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
assert not formdata.workflow_roles
# match
formdata.data = {'1': 'foo'}
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
assert formdata.workflow_roles == {'_receiver': role1.id}
# match
formdata.data = {'1': 'foo'}
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
assert formdata.workflow_roles == {'_receiver': role1.id}
# other match
formdata.data = {'1': 'bar'}
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
assert formdata.workflow_roles == {'_receiver': role2.id}
# other match
formdata.data = {'1': 'bar'}
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
assert formdata.workflow_roles == {'_receiver': role2.id}
def test_dispatch_computed(pub, caplog):
pub.cfg['debug'] = {'logger': True}

View File

@ -19,6 +19,7 @@ import xml.etree.ElementTree as ET
from qommon import _
from qommon.form import *
from qommon.template import Template
from qommon import get_logger
from wcs.roles import Role, get_user_roles
from wcs.workflows import XmlSerialisable, WorkflowStatusItem, register_item_class
@ -191,16 +192,21 @@ class DispatchWorkflowStatusItem(WorkflowStatusItem):
elif self.dispatch_type == 'automatic':
if not (self.role_key and self.variable and self.rules):
return
variables = get_publisher().substitutions.get_context_variables()
# convert the given value to a few different types, to allow more
# diversity in matching.
variable_values = [variables.get(self.variable)]
if not variable_values[0]:
variable_values.append(None)
try:
variable_values.append(int(variable_values[0]))
except (ValueError, TypeError):
pass
variable_values = []
if Template.is_template_string(self.variable):
variable_values = [self.compute(self.variable, formdata=formdata, status_item=self)]
else:
# legacy, self.variable as a straight variable name
variables = get_publisher().substitutions.get_context_variables()
# convert the given value to a few different types, to allow more
# diversity in matching.
variable_values = [variables.get(self.variable)]
if not variable_values[0]:
variable_values.append(None)
try:
variable_values.append(int(variable_values[0]))
except (ValueError, TypeError):
pass
for rule in self.rules:
if rule.get('value') in variable_values: