From ff0a78e682504cb283e95f63a3c22810c65fed4b Mon Sep 17 00:00:00 2001 From: LEVY-FALK Hugo Date: Thu, 28 Dec 2017 20:00:51 +0100 Subject: [PATCH] =?UTF-8?q?AclModelNode=20=E2=86=92=20AclNode=20+=20cr?= =?UTF-8?q?=C3=A9ation=20du=20templatetag=20can=5Fedit=5Fhistory.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- re2o/templatetags/acl.py | 51 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/re2o/templatetags/acl.py b/re2o/templatetags/acl.py index 0363475c..da12302b 100644 --- a/re2o/templatetags/acl.py +++ b/re2o/templatetags/acl.py @@ -149,7 +149,7 @@ def get_model(model_name): ) -def get_callback(tag_name, obj): +def get_callback(tag_name, obj=None): """Return the right function to call back to check for acl""" if tag_name == 'can_create': @@ -196,6 +196,10 @@ def get_callback(tag_name, obj): ), True ) + if tag_name == 'can_edit_history': + return acl_fct(lambda user:(user.has_perms(('admin',)),None),False) + if tag_name == 'cannot_edit_history': + return acl_fct(lambda user:(user.has_perms(('admin',)),None),True) raise template.TemplateSyntaxError( "%r tag is not a valid can_xxx tag" % tag_name @@ -216,6 +220,27 @@ def acl_fct(callback, reverse): return acl_fct_reverse if reverse else acl_fct_normal + +@register.tag('can_edit_history') +@register.tag('cannot_edit_history') +def acl_history_filter(parser, token): + """Templatetag for acl checking on history.""" + tag_name,_ = token.split_contents() + + callback = get_callback(tag_name) + oknodes = parser.parse(('acl_else', 'acl_end')) + token = parser.next_token() + if token.contents == 'acl_else': + konodes = parser.parse(('acl_end')) + token = parser.next_token() + else: + konodes = NodeList() + + assert token.contents == 'acl_end' + + return AclNode(callback, oknodes, konodes) + + @register.tag('can_view_app') @register.tag('cannot_view_app') def acl_app_filter(parser, token): @@ -245,7 +270,7 @@ def acl_app_filter(parser, token): assert token.contents == 'acl_end' - return AclAppNode(callback, oknodes, konodes) + return AclNode(callback, oknodes, konodes) @register.tag('can_create') @@ -287,7 +312,7 @@ def acl_model_filter(parser, token): # {% can_create_end %} assert token.contents == 'acl_end' - return AclModelNode(callback, oknodes, konodes, *args) + return AclNode(callback, oknodes, konodes, *args) @register.tag('can_edit') @@ -327,23 +352,9 @@ def acl_instance_filter(parser, token): return AclInstanceNode(tag_name, instance_name, oknodes, konodes, *args) -class AclAppNode(Node): - """A node for compiled ACL block when ACL is based on application.""" - - def __init__(self, callback, oknodes, konodes): - self.callback = callback - self.oknodes = oknodes - self.konodes = konodes - - def render(self, context): - can, _ = self.callback(context['user']) - if can: - return self.oknodes.render(context) - return self.konodes.render(context) - - -class AclModelNode(Node): - """A node for the compiled ACL block when acl is base on model""" +class AclNode(Node): + """A node for the compiled ACL block when acl callback doesn't require + context.""" def __init__(self, callback, oknodes, konodes, *args): self.callback = callback