0001-misc-add-has_role-filter-29342.patch
tests/test_formdata.py | ||
---|---|---|
985 | 985 |
condition = Condition({'type': 'django', 'value': 'not form_var_datefield == form_var_barbarbar'}) |
986 | 986 |
assert condition.evaluate() is True |
987 | 987 | |
988 |
def test_has_role_templatetag(pub, variable_test_data): |
|
989 |
condition = Condition({'type': 'django', 'value': 'form_user|has_role:"foobar"'}) |
|
990 |
assert condition.evaluate() is False |
|
991 |
condition = Condition({'type': 'django', 'value': 'form_user|has_role:form_role_receiver_name'}) |
|
992 |
assert condition.evaluate() is False |
|
993 | ||
994 |
role = Role.select()[0] |
|
995 |
user = pub.user_class.select()[0] |
|
996 |
user.roles = [role.id, '42'] # role.id 42 does not exist |
|
997 |
user.store() |
|
998 | ||
999 |
condition = Condition({'type': 'django', 'value': 'form_user|has_role:"foobar"'}) |
|
1000 |
assert condition.evaluate() is True |
|
1001 |
condition = Condition({'type': 'django', 'value': 'form_user|has_role:form_role_receiver_name'}) |
|
1002 |
assert condition.evaluate() is True |
|
1003 |
condition = Condition({'type': 'django', 'value': 'form_user|has_role:"barfoo"'}) |
|
1004 |
assert condition.evaluate() is False |
|
1005 |
condition = Condition({'type': 'django', 'value': 'form_user|has_role:form_var_foo_foo'}) |
|
1006 |
assert condition.evaluate() is False |
|
1007 | ||
1008 |
# non-user object |
|
1009 |
condition = Condition({'type': 'django', 'value': 'form_var_foo_foo|has_role:"foobar"'}) |
|
1010 |
assert condition.evaluate() is False |
|
1011 |
condition = Condition({'type': 'django', 'value': 'xxx|has_role:"foobar"'}) |
|
1012 |
assert condition.evaluate() is False |
|
1013 | ||
988 | 1014 |
def test_lazy_now_and_today(pub, variable_test_data): |
989 | 1015 |
for condition_value in ( |
990 | 1016 |
'now > "1970-01-01"', |
wcs/qommon/templatetags/qommon.py | ||
---|---|---|
33 | 33 |
from wcs.qommon import evalutils |
34 | 34 |
from wcs.qommon import tokens |
35 | 35 |
from wcs.qommon.admin.texts import TextsDirectory |
36 |
from wcs.roles import Role |
|
36 | 37 | |
37 | 38 |
register = template.Library() |
38 | 39 | |
... | ... | |
357 | 358 |
proj = pyproj.Proj(init='EPSG:4326') |
358 | 359 |
target_proj = pyproj.Proj(init=projection_name) |
359 | 360 |
return pyproj.transform(proj, target_proj, coords['lon'], coords['lat']) |
361 | ||
362 |
@register.filter |
|
363 |
def has_role(user, role_name): |
|
364 |
if not callable(getattr(user, 'get_roles', None)): |
|
365 |
# do not fail on non-user objects, just return False |
|
366 |
return False |
|
367 |
for role_id in user.get_roles(): |
|
368 |
try: |
|
369 |
if role_name == Role.get(role_id).name: |
|
370 |
return True |
|
371 |
except KeyError: # role has been deleted |
|
372 |
pass |
|
373 |
return False |
|
360 |
- |