def ready(self): from django.template import smartif smartif.OPERATORS[':='] = smartif.infix(11, _walrus_op) smartif.OPERATORS[':='].id = ':=' if os.getenv('WALRUS_OPTIMIZED'): _walrus_optimized()
def of_operator(context, x, y): return x.eval(context), y.eval(context) def has_operator(context, x, y): user = x.eval(context) perm = y.eval(context) if isinstance(perm, (list, tuple)): perm, obj = perm else: obj = None return user.has_perm(perm, obj) EXTRA_OPERATORS = {"of": infix(20, of_operator), "has": infix(10, has_operator)} EXTRA_OPERATORS.update(OPERATORS) # Assign 'id' to each: for key, op in EXTRA_OPERATORS.items(): op.id = key class PermissionIfParser(IfParser): OPERATORS = EXTRA_OPERATORS def translate_token(self, token): try: op = self.OPERATORS[token] except (KeyError, TypeError): return self.create_var(token) else:
""" 'has' operator of permission if This operator is used to specify the user object of permission """ user = x.eval(context) perm = y.eval(context) if isinstance(perm, (list, tuple)): perm, obj = perm else: obj = None return user.has_perm(perm, obj) # Add 'of' and 'has' operator to existing operators EXTRA_OPERATORS = { 'of': infix(20, of_operator), 'has': infix(10, has_operator), } EXTRA_OPERATORS.update(OPERATORS) for key, op in list(EXTRA_OPERATORS.items()): op.id = key class PermissionIfParser(IfParser): """Permission if parser""" OPERATORS = EXTRA_OPERATORS """use extra operator""" def translate_token(self, token): try: # use own operators instead of builtin operators op = self.OPERATORS[token]
limitations under the License. """ __AUTHOR__ = "lambdalisue ([email protected])" import warnings from django import template from django.template import Node, VariableDoesNotExist from django.template.smartif import infix from django.template.smartif import IfParser from django.template.smartif import OPERATORS as _OPERATORS from django.template.defaulttags import TemplateLiteral register = template.Library() OPERATORS = dict(_OPERATORS, of=infix(10, lambda context, x, y: template.resolve_variable('request', context).user.has_perm(x.eval(context), y.eval(context))) ) class ObjectPermissionIfParser(IfParser): def translate_token(self, token): try: op = OPERATORS[token] except (KeyError, TypeError): return self.create_var(token) else: return op() class TemplateObjectPermissionIfParser(ObjectPermissionIfParser): error_class = template.TemplateSyntaxError def __init__(self, parser, *args, **kwargs): self.template_parser = parser
'has' operator of permission if This operator is used to specify the user object of permission """ user = x.eval(context) perm = y.eval(context) if isinstance(perm, (list, tuple)): perm, obj = perm else: obj = None return user.has_perm(perm, obj) # Add 'of' and 'has' operator to existing operators EXTRA_OPERATORS = { 'of': infix(20, of_operator), 'has': infix(10, has_operator), } EXTRA_OPERATORS.update(OPERATORS) for key, op in EXTRA_OPERATORS.items(): op.id = key class PermissionIfParser(IfParser): """Permission if parser""" OPERATORS = EXTRA_OPERATORS """use extra operator""" def translate_token(self, token): try: # use own operators instead of builtin operators op = self.OPERATORS[token]