misc: add more variables for function multi attributions (#59402) #424

Merged
fpeters merged 1 commits from wip/59402-function-more-attrs into main 2023-07-14 09:06:42 +02:00
2 changed files with 70 additions and 21 deletions

View File

@ -2346,6 +2346,34 @@ def test_roles_templatefilter(pub, variable_test_data):
assert condition.evaluate() is False
def test_function_roles(pub):
formdef = FormDef()
formdef.name = 'foobar'
formdef.url_name = 'foobar'
formdef.fields = []
formdef.store()
pub.role_class.wipe()
role = pub.role_class('test role')
role.store()
role2 = pub.role_class('second role')
role2.store()
formdata = formdef.data_class()()
formdata.workflow_roles = {'_receiver': [str(role.id), str(role2.id)]}
static_vars = formdata.get_static_substitution_variables()
assert static_vars['form_role_receiver_name'] == 'test role, second role'
assert static_vars['form_role_receiver_names'] == ['test role', 'second role']
assert static_vars['form_role_receiver_role_slugs'] == ['test-role', 'second-role']
# missing role id; ignored
formdata.workflow_roles = {'_receiver': ['12345', str(role2.id)]}
static_vars = formdata.get_static_substitution_variables()
assert static_vars['form_role_receiver_name'] == 'second role'
assert static_vars['form_role_receiver_names'] == ['second role']
assert static_vars['form_role_receiver_role_slugs'] == ['second-role']
def test_function_users(pub):
user1 = pub.user_class(name='userA')
user1.store()
@ -2374,19 +2402,27 @@ def test_function_users(pub):
assert condition.evaluate() is True
static_vars = formdata.get_static_substitution_variables()
assert static_vars['form_role_receiver_name'] == 'userA, userB'
assert set(static_vars['form_role_receiver_names']) == {'userA', 'userB'}
# combined roles and users
pub.role_class.wipe()
role = pub.role_class('test combined')
role.store()
formdata.workflow_roles = {'_receiver': [str(role.id), '_user:%s' % user1.id, '_user:%s' % user2.id]}
role2 = pub.role_class('second role')
role2.store()
formdata.workflow_roles = {
'_receiver': [str(role.id), str(role2.id), '_user:%s' % user1.id, '_user:%s' % user2.id]
}
formdata.store()
pub.substitutions.feed(formdata)
condition = Condition(
{'type': 'django', 'value': 'form_role_receiver_name == "test combined, userA, userB"'}
{'type': 'django', 'value': 'form_role_receiver_name == "test combined, second role, userA, userB"'}
)
assert condition.evaluate() is True
static_vars = formdata.get_static_substitution_variables()
assert static_vars['form_role_receiver_name'] == 'test combined, userA, userB'
assert static_vars['form_role_receiver_name'] == 'test combined, second role, userA, userB'
assert set(static_vars['form_role_receiver_names']) == {'test combined', 'second role', 'userA', 'userB'}
assert static_vars['form_role_receiver_role_slugs'] == ['test-combined', 'second-role']
formdata.workflow_roles = {'_receiver': ['_user:%s' % user1.id, '_user:%s' % user2.id, str(role.id)]}
formdata.store()
@ -2398,6 +2434,20 @@ def test_function_users(pub):
static_vars = formdata.get_static_substitution_variables()
assert static_vars['form_role_receiver_name'] == 'userA, userB, test combined'
# missing role id; ignored
formdata.workflow_roles = {
'_receiver': ['12345', str(role2.id), '_user:%s' % user1.id, '_user:%s' % user2.id]
}
formdata.store()
static_vars = formdata.get_static_substitution_variables()
assert static_vars['form_role_receiver_name'] == 'second role, userA, userB'
# missing user id; ignored
formdata.workflow_roles = {'_receiver': ['12345', '_user:%s' % user1.id, '_user:12345']}
formdata.store()
static_vars = formdata.get_static_substitution_variables()
assert static_vars['form_role_receiver_name'] == 'userA'
def test_lazy_now_and_today(pub, variable_test_data):
for condition_value in (

View File

@ -121,25 +121,24 @@ def get_workflow_roles_substitution_variables(workflow_roles, prefix=''):
_prefix = '%s%s_' % (prefix, role_type.replace('-', '_').strip('_'))
if not isinstance(role_ids, list):
role_ids = [role_ids]
if any(x for x in role_ids if str(x).startswith('_user:')):
# there's some direct user attribution, only get names
try:
users_and_roles = [
get_publisher().user_class.get(role_id.split(':')[1])
if ':' in role_id
else get_publisher().role_class.get(role_id)
for role_id in role_ids
]
except KeyError:
continue
d['%sname' % _prefix] = ', '.join([u.name for u in users_and_roles])
continue
users_and_roles = [
get_publisher().user_class.get(str(role_id).split(':')[1], ignore_errors=True)
if ':' in str(role_id)
else get_publisher().role_class.cached_get(role_id, ignore_errors=True)
for role_id in role_ids
]
users_and_roles = [x for x in users_and_roles if x]
roles = [x for x in users_and_roles if isinstance(x, get_publisher().role_class)]
if roles:
d.update(roles[0].get_substitution_variables(_prefix))
d[f'{_prefix}name'] = ', '.join([x.name for x in users_and_roles])
d[f'{_prefix}names'] = [x.name for x in users_and_roles]
d[f'{_prefix}role_slugs'] = [x.slug for x in roles]
d[f'{_prefix}role_uuids'] = [x.uuid for x in roles]
role_id = role_ids[0]
try:
d.update(get_publisher().role_class.cached_get(role_id).get_substitution_variables(_prefix))
except KeyError:
pass
return d