mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-05 09:26:27 +00:00
Add support for args passed through the templatetag to the acl function
This commit is contained in:
parent
92944ec815
commit
cc8b140bf9
1 changed files with 32 additions and 15 deletions
|
@ -26,9 +26,11 @@ Set of templatags for using acl in templates:
|
||||||
|
|
||||||
**Parameters**:
|
**Parameters**:
|
||||||
model_name - The model_name that needs to be checked for the current user
|
model_name - The model_name that needs to be checked for the current user
|
||||||
|
args - Any other argument that is interpreted as a python object and passed
|
||||||
|
to the acl function (can_xxx)
|
||||||
|
|
||||||
**Usage**:
|
**Usage**:
|
||||||
{% <acl_name> model %}
|
{% <acl_name> model [arg1 [arg2 [...]]]%}
|
||||||
<template stuff>
|
<template stuff>
|
||||||
[{% can_else %}
|
[{% can_else %}
|
||||||
<template stuff>]
|
<template stuff>]
|
||||||
|
@ -38,10 +40,10 @@ Set of templatags for using acl in templates:
|
||||||
(can_xxx or cannot_xxx)
|
(can_xxx or cannot_xxx)
|
||||||
|
|
||||||
**Example**:
|
**Example**:
|
||||||
{% can_create Machine %}
|
{% can_create Machine targeted_user %}
|
||||||
<p>I'm authorized to create new machines \o/</p>
|
<p>I'm authorized to create new machines for this guy \o/</p>
|
||||||
{% can_else %}
|
{% can_else %}
|
||||||
<p>Why can't I create a little machine :(</p>
|
<p>Why can't I create a little machine for this guy ? :(</p>
|
||||||
{% can_end %}
|
{% can_end %}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -106,35 +108,48 @@ def get_model(model_name):
|
||||||
# TODO
|
# TODO
|
||||||
else:
|
else:
|
||||||
raise template.TemplateSyntaxError(
|
raise template.TemplateSyntaxError(
|
||||||
"%r is not a valid model for %r tag" % model_name, tag_name
|
"%r is not a valid model for an acl tag" % model_name
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_callback(tag_name, model_name):
|
def get_callback(tag_name, model_name):
|
||||||
|
"""Return the right function to call back to check for acl"""
|
||||||
|
|
||||||
model = get_model(model_name)
|
model = get_model(model_name)
|
||||||
|
|
||||||
if tag_name == 'can_create':
|
if tag_name == 'can_create':
|
||||||
return model.can_create
|
return acl_fct(model.can_create, False)
|
||||||
if tag_name == 'cannot_create':
|
if tag_name == 'cannot_create':
|
||||||
def res(*args, **kwargs):
|
return acl_fct(model.can_create, True)
|
||||||
can, msg = model.can_create(*args, **kwargs)
|
|
||||||
return not can, msg
|
|
||||||
return res
|
|
||||||
else:
|
else:
|
||||||
raise template.TemplateSyntaxError(
|
raise template.TemplateSyntaxError(
|
||||||
"%r tag is not a valid can_xxx tag" % tag_name
|
"%r tag is not a valid can_xxx tag" % tag_name
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def acl_fct(cb, reverse):
|
||||||
|
"""Build a function to use as an acl checker"""
|
||||||
|
|
||||||
|
def acl_fct_normal(*args, **kwargs):
|
||||||
|
return cb(*args, **kwargs)
|
||||||
|
def acl_fct_reverse(*args, **kwargs):
|
||||||
|
can, msg = cb(*args, **kwargs)
|
||||||
|
return not can, msg
|
||||||
|
return acl_fct_reverse if reverse else acl_fct_normal
|
||||||
|
|
||||||
|
|
||||||
@register.tag('can_create')
|
@register.tag('can_create')
|
||||||
@register.tag('cannot_create')
|
@register.tag('cannot_create')
|
||||||
def can_generic(parser, token):
|
def can_generic(parser, token):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tag_name, model_name = token.split_contents()
|
tag_content = token.split_contents()
|
||||||
|
tag_name = tag_content[0]
|
||||||
|
model_name = tag_content[1]
|
||||||
|
args = tag_content[2:]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise template.TemplateSyntaxError(
|
raise template.TemplateSyntaxError(
|
||||||
"%r tag require a single argument : the model" % token.contents.split()[0]
|
"%r tag require at least 1 argument : the model" % token.contents.split()[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
callback = get_callback(tag_name, model_name)
|
callback = get_callback(tag_name, model_name)
|
||||||
|
@ -153,18 +168,20 @@ def can_generic(parser, token):
|
||||||
# {% can_create_end %}
|
# {% can_create_end %}
|
||||||
assert token.contents == 'can_end'
|
assert token.contents == 'can_end'
|
||||||
|
|
||||||
return CanNode( callback, oknodes, konodes )
|
return CanNode( callback, oknodes, konodes, *args )
|
||||||
|
|
||||||
|
|
||||||
class CanNode(Node):
|
class CanNode(Node):
|
||||||
|
|
||||||
def __init__(self, callback, oknodes, konodes):
|
def __init__(self, callback, oknodes, konodes, *args):
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
self.oknodes = oknodes
|
self.oknodes = oknodes
|
||||||
self.konodes = konodes
|
self.konodes = konodes
|
||||||
|
self.args = [template.Variable(arg) for arg in args]
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
can, _ = self.callback(context['user'])
|
resolved_args = [arg.resolve(context) for arg in self.args]
|
||||||
|
can, _ = self.callback(context['user'], *(resolved_args))
|
||||||
if can:
|
if can:
|
||||||
return self.oknodes.render(context)
|
return self.oknodes.render(context)
|
||||||
return self.konodes.render(context)
|
return self.konodes.render(context)
|
||||||
|
|
Loading…
Reference in a new issue