Projet

Général

Profil

0001-formdata-form_role-variables-user-function-57506.patch

Lauréline Guérin, 07 octobre 2021 16:47

Télécharger (5,11 ko)

Voir les différences:

Subject: [PATCH] formdata: form_role variables & user-function (#57506)

 tests/test_formdata.py | 30 ++++++++++++++++++++++++++++++
 wcs/formdata.py        | 37 ++++++++++++++++++++++++++-----------
 wcs/variables.py       | 15 ++-------------
 3 files changed, 58 insertions(+), 24 deletions(-)
tests/test_formdata.py
2000 2000
    assert condition.evaluate() is False
2001 2001

  
2002 2002

  
2003
def test_function_users(pub):
2004
    user1 = pub.user_class(name='userA')
2005
    user1.store()
2006
    user2 = pub.user_class(name='userB')
2007
    user2.store()
2008

  
2009
    formdef = FormDef()
2010
    formdef.name = 'foobar'
2011
    formdef.url_name = 'foobar'
2012
    formdef.fields = []
2013
    formdef.store()
2014

  
2015
    formdata = formdef.data_class()()
2016
    formdata.workflow_roles = {'_receiver': '_user:%s' % user1.id}
2017
    formdata.store()
2018
    pub.substitutions.feed(formdata)
2019
    condition = Condition({'type': 'django', 'value': 'form_role_receiver_name == "userA"'})
2020
    assert condition.evaluate() is True
2021
    static_vars = formdata.get_static_substitution_variables()
2022
    assert static_vars['form_role_receiver_name'] == 'userA'
2023

  
2024
    formdata.workflow_roles = {'_receiver': ['_user:%s' % user1.id, '_user:%s' % user2.id]}
2025
    formdata.store()
2026
    pub.substitutions.feed(formdata)
2027
    condition = Condition({'type': 'django', 'value': 'form_role_receiver_name == "userA, userB"'})
2028
    assert condition.evaluate() is True
2029
    static_vars = formdata.get_static_substitution_variables()
2030
    assert static_vars['form_role_receiver_name'] == 'userA, userB'
2031

  
2032

  
2003 2033
def test_lazy_now_and_today(pub, variable_test_data):
2004 2034
    for condition_value in (
2005 2035
        'now > "1970-01-01"',
wcs/formdata.py
104 104
            del d[k]
105 105

  
106 106

  
107
def get_workflow_roles_substitution_variables(workflow_roles, prefix=''):
108
    d = {}
109
    for role_type, role_ids in workflow_roles.items():
110
        if not role_ids:
111
            continue
112

  
113
        _prefix = '%s%s_' % (prefix, role_type.replace('-', '_').strip('_'))
114
        if not isinstance(role_ids, list):
115
            role_ids = [role_ids]
116
        if role_ids[0].startswith('_user:'):
117
            try:
118
                users = [get_publisher().user_class.get(role_id.split(':')[1]) for role_id in role_ids]
119
            except KeyError:
120
                continue
121
            d['%sname' % _prefix] = ', '.join([u.name for u in users])
122
            continue
123

  
124
        role_id = role_ids[0]
125
        try:
126
            d.update(get_publisher().role_class.get(role_id).get_substitution_variables(prefix))
127
        except KeyError:
128
            pass
129
    return d
130

  
131

  
107 132
class Evolution:
108 133
    who = None
109 134
    status = None
......
887 912
        if self.workflow_roles:
888 913
            workflow_roles.update(self.workflow_roles)
889 914

  
890
        for role_type, role_id in workflow_roles.items():
891
            if not role_id:
892
                continue
893
            if isinstance(role_id, list):
894
                # only return first role
895
                role_id = role_id[0]
896
            prefix = 'form_role_%s_' % role_type.replace('-', '_').strip('_')
897
            try:
898
                d.update(get_publisher().role_class.get(role_id).get_substitution_variables(prefix))
899
            except KeyError:
900
                pass
915
        d.update(get_workflow_roles_substitution_variables(workflow_roles, prefix='form_role_'))
901 916

  
902 917
        if self.evolution and self.evolution[-1].comment:
903 918
            d['form_comment'] = self.evolution[-1].comment
wcs/variables.py
22 22
from quixote import get_publisher, get_request
23 23

  
24 24
from .carddef import CardDef
25
from .formdata import get_workflow_roles_substitution_variables
25 26
from .formdef import FormDef
26 27
from .qommon import _, force_str, misc
27 28
from .qommon.evalutils import make_datetime
......
512 513
        if self._formdata.workflow_roles:
513 514
            workflow_roles.update(self._formdata.workflow_roles)
514 515

  
515
        d = {}
516
        for role_type, role_id in workflow_roles.items():
517
            if not role_id:
518
                continue
519
            prefix = '%s_' % role_type.replace('-', '_').strip('_')
520
            if isinstance(role_id, list):
521
                # only return first role
522
                role_id = role_id[0]
523
            try:
524
                d.update(get_publisher().role_class.get(role_id).get_substitution_variables(prefix))
525
            except KeyError:
526
                pass
527
        return d
516
        return get_workflow_roles_substitution_variables(workflow_roles)
528 517

  
529 518
    @property
530 519
    def comment(self):
531
-