Projet

Général

Profil

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

Lauréline Guérin, 18 juin 2020 16:21

Télécharger (3,14 ko)

Voir les différences:

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

 tests/test_formdata.py            | 29 ++++++++++++++++++++++++++++-
 wcs/qommon/templatetags/qommon.py |  9 +++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
tests/test_formdata.py
1053 1053
    assert condition.evaluate() is True
1054 1054

  
1055 1055

  
1056
def test_has_role_templatetag(pub, variable_test_data):
1056
def test_has_role_templatefilter(pub, variable_test_data):
1057 1057
    condition = Condition({'type': 'django', 'value': 'form_user|has_role:"foobar"'})
1058 1058
    assert condition.evaluate() is False
1059 1059
    condition = Condition({'type': 'django', 'value': 'form_user|has_role:form_role_receiver_name'})
......
1080 1080
    assert condition.evaluate() is False
1081 1081

  
1082 1082

  
1083
def test_roles_templatefilter(pub, variable_test_data):
1084
    condition = Condition({'type': 'django', 'value': '"foobar" in form_user|roles'})
1085
    assert condition.evaluate() is False
1086
    condition = Condition({'type': 'django', 'value': 'form_role_receiver_name in form_user|roles'})
1087
    assert condition.evaluate() is False
1088

  
1089
    role = Role.select()[0]
1090
    user = pub.user_class.select()[0]
1091
    user.roles = [role.id, '42']  # role.id 42 does not exist
1092
    user.store()
1093

  
1094
    condition = Condition({'type': 'django', 'value': '"foobar" in form_user|roles'})
1095
    assert condition.evaluate() is True
1096
    condition = Condition({'type': 'django', 'value': 'form_role_receiver_name in form_user|roles'})
1097
    assert condition.evaluate() is True
1098
    condition = Condition({'type': 'django', 'value': '"barfoo" in form_user|roles'})
1099
    assert condition.evaluate() is False
1100
    condition = Condition({'type': 'django', 'value': 'form_var_foo_foo in form_user|roles'})
1101
    assert condition.evaluate() is False
1102

  
1103
    # non-user object
1104
    condition = Condition({'type': 'django', 'value': '"foobar" in form_var_foo_foo|roles'})
1105
    assert condition.evaluate() is False
1106
    condition = Condition({'type': 'django', 'value': '"foobar" in xxx|roles'})
1107
    assert condition.evaluate() is False
1108

  
1109

  
1083 1110
def test_lazy_now_and_today(pub, variable_test_data):
1084 1111
    for condition_value in (
1085 1112
            'now > "1970-01-01"',
wcs/qommon/templatetags/qommon.py
468 468
    return False
469 469

  
470 470

  
471
@register.filter
472
def roles(user):
473
    if not callable(getattr(user, 'get_roles', None)):
474
        # do not fail on non-user objects, just return empty list
475
        return []
476
    role_ids = user.get_roles()
477
    return [r.name for r in Role.select() if r.id in role_ids]
478

  
479

  
471 480
@register.filter
472 481
def language_detect(value):
473 482
    if langdetect is None:
474
-