Projet

Général

Profil

0001-misc-add-roles-filter-42061.patch

Lauréline Guérin, 30 juin 2020 11:41

Télécharger (3,2 ko)

Voir les différences:

Subject: [PATCH] misc: add roles filter (#42061)

 tests/test_formdata.py            | 29 ++++++++++++++++++++++++++++-
 wcs/qommon/templatetags/qommon.py | 10 ++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)
tests/test_formdata.py
1065 1065
    assert condition.evaluate() is True
1066 1066

  
1067 1067

  
1068
def test_has_role_templatetag(pub, variable_test_data):
1068
def test_has_role_templatefilter(pub, variable_test_data):
1069 1069
    condition = Condition({'type': 'django', 'value': 'form_user|has_role:"foobar"'})
1070 1070
    assert condition.evaluate() is False
1071 1071
    condition = Condition({'type': 'django', 'value': 'form_user|has_role:form_role_receiver_name'})
......
1092 1092
    assert condition.evaluate() is False
1093 1093

  
1094 1094

  
1095
def test_roles_templatefilter(pub, variable_test_data):
1096
    condition = Condition({'type': 'django', 'value': '"foobar" in form_user|roles'})
1097
    assert condition.evaluate() is False
1098
    condition = Condition({'type': 'django', 'value': 'form_role_receiver_name in form_user|roles'})
1099
    assert condition.evaluate() is False
1100

  
1101
    role = Role.select()[0]
1102
    user = pub.user_class.select()[0]
1103
    user.roles = [role.id, '42']  # role.id 42 does not exist
1104
    user.store()
1105

  
1106
    condition = Condition({'type': 'django', 'value': '"foobar" in form_user|roles'})
1107
    assert condition.evaluate() is True
1108
    condition = Condition({'type': 'django', 'value': 'form_role_receiver_name in form_user|roles'})
1109
    assert condition.evaluate() is True
1110
    condition = Condition({'type': 'django', 'value': '"barfoo" in form_user|roles'})
1111
    assert condition.evaluate() is False
1112
    condition = Condition({'type': 'django', 'value': 'form_var_foo_foo in form_user|roles'})
1113
    assert condition.evaluate() is False
1114

  
1115
    # non-user object
1116
    condition = Condition({'type': 'django', 'value': '"foobar" in form_var_foo_foo|roles'})
1117
    assert condition.evaluate() is False
1118
    condition = Condition({'type': 'django', 'value': '"foobar" in xxx|roles'})
1119
    assert condition.evaluate() is False
1120

  
1121

  
1095 1122
def test_lazy_now_and_today(pub, variable_test_data):
1096 1123
    for condition_value in (
1097 1124
            'now > "1970-01-01"',
wcs/qommon/templatetags/qommon.py
481 481
    return False
482 482

  
483 483

  
484
@register.filter
485
def roles(user):
486
    if not callable(getattr(user, 'get_roles', None)):
487
        # do not fail on non-user objects, just return empty list
488
        return []
489
    role_ids = user.get_roles()
490
    roles = [Role.get(x, ignore_errors=True, ignore_migration=True) for x in role_ids]
491
    return [x.name for x in roles if x]
492

  
493

  
484 494
@register.filter
485 495
def language_detect(value):
486 496
    if langdetect is None:
487
-