mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-05 09:26:27 +00:00
Pylint & Pep8 compliance
This commit is contained in:
parent
cc8b140bf9
commit
92888edc7f
1 changed files with 25 additions and 9 deletions
|
@ -20,7 +20,7 @@
|
||||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Set of templatags for using acl in templates:
|
Set of templatetags for using acl in templates:
|
||||||
- can_create
|
- can_create
|
||||||
- cannot_create
|
- cannot_create
|
||||||
|
|
||||||
|
@ -41,11 +41,18 @@ Set of templatags for using acl in templates:
|
||||||
|
|
||||||
**Example**:
|
**Example**:
|
||||||
{% can_create Machine targeted_user %}
|
{% can_create Machine targeted_user %}
|
||||||
<p>I'm authorized to create new machines for this guy \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 for this guy ? :(</p>
|
<p>Why can't I create a little machine for this guy ? :(</p>
|
||||||
{% can_end %}
|
{% can_end %}
|
||||||
|
|
||||||
|
**How to modify**:
|
||||||
|
To add a new acl function (can_xxx or cannot_xxx), add an entry in
|
||||||
|
'get_callback' and register your tag with the other ones juste before
|
||||||
|
'can_generic' definition
|
||||||
|
To add a new model, add an entry in 'get_model' and be sure the acl
|
||||||
|
function exists in the model definition
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django import template
|
from django import template
|
||||||
|
@ -61,6 +68,8 @@ register = template.Library()
|
||||||
|
|
||||||
|
|
||||||
def get_model(model_name):
|
def get_model(model_name):
|
||||||
|
"""Retrieve the model object from its name"""
|
||||||
|
|
||||||
# cotisations
|
# cotisations
|
||||||
# TODO
|
# TODO
|
||||||
# logs
|
# logs
|
||||||
|
@ -127,20 +136,25 @@ def get_callback(tag_name, model_name):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def acl_fct(cb, reverse):
|
def acl_fct(callback, reverse):
|
||||||
"""Build a function to use as an acl checker"""
|
"""Build a function to use as an acl checker"""
|
||||||
|
|
||||||
def acl_fct_normal(*args, **kwargs):
|
def acl_fct_normal(*args, **kwargs):
|
||||||
return cb(*args, **kwargs)
|
"""The can_xxx checker callback"""
|
||||||
|
return callback(*args, **kwargs)
|
||||||
|
|
||||||
def acl_fct_reverse(*args, **kwargs):
|
def acl_fct_reverse(*args, **kwargs):
|
||||||
can, msg = cb(*args, **kwargs)
|
"""The cannot_xxx checker callback"""
|
||||||
|
can, msg = callback(*args, **kwargs)
|
||||||
return not can, msg
|
return not can, msg
|
||||||
|
|
||||||
return acl_fct_reverse if reverse else acl_fct_normal
|
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):
|
||||||
|
"""Generic definition of an acl templatetag"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tag_content = token.split_contents()
|
tag_content = token.split_contents()
|
||||||
|
@ -149,7 +163,8 @@ def can_generic(parser, token):
|
||||||
args = tag_content[2:]
|
args = tag_content[2:]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise template.TemplateSyntaxError(
|
raise template.TemplateSyntaxError(
|
||||||
"%r tag require at least 1 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)
|
||||||
|
@ -172,6 +187,7 @@ def can_generic(parser, token):
|
||||||
|
|
||||||
|
|
||||||
class CanNode(Node):
|
class CanNode(Node):
|
||||||
|
"""A node for the compiled ACL block"""
|
||||||
|
|
||||||
def __init__(self, callback, oknodes, konodes, *args):
|
def __init__(self, callback, oknodes, konodes, *args):
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
|
|
Loading…
Reference in a new issue