def do_has_perm_to(parser, token): """{% has_perm_to TYPE OBJECT as VAR %} eg: {% has_perm_to change action.creme_entity as has_perm %} TYPE: must be in ('create', 'view','change', 'delete', 'link', 'unlink', 'create', 'export', 'admin') OBJECT: * TYPE in ('view','change', 'delete', 'unlink') => must be a CremeEntity. * TYPE='link' => can be a CremeEntity instance or a class inheriting from CremeEntity. * TYPE in ('create', 'export') and a class inheriting from CremeEntity OR a ContentType instance. * TYPE='admin' => an app name, like 'creme_core'. """ try: # Splitting by None == splitting by spaces. tag_name, arg = token.contents.split(None, 1) except ValueError as e: raise TemplateSyntaxError('{!r} tag requires arguments'.format( token.contents.split()[0])) from e match = _haspermto_re.search(arg) if not match: raise TemplateSyntaxError( '{!r} tag had invalid arguments: {!r}'.format(tag_name, arg)) perm_type, entity_path, var_name = match.groups() perm_func = _PERMS_FUNCS.get(perm_type) if not perm_func: raise TemplateSyntaxError("{!r} invalid permission tag: {!r}".format( tag_name, perm_type)) entity_var = TemplateLiteral(parser.compile_filter(entity_path), entity_path) return HasPermToNode(perm_func, entity_var, var_name)
def do_include_creme_media(parser, token): try: # Splitting by None == splitting by spaces. tag_name, arg = token.contents.split(None, 1) except ValueError as e: raise TemplateSyntaxError('{!r} tag requires arguments'.format( token.contents.split()[0])) from e return MediaNode(TemplateLiteral(parser.compile_filter(arg), arg))
def do_print_field(parser, token): """Eg:{% print_field object=object field='created' %}""" try: # Splitting by None == splitting by spaces. tag_name, arg = token.contents.split(None, 1) except ValueError as e: raise TemplateSyntaxError("{!r} tag requires arguments".format( token.contents.split()[0])) from e match = _PRINT_FIELD_RE.search(arg) if not match: raise TemplateSyntaxError( "{!r} tag had invalid arguments".format(tag_name)) obj_str, field_str = match.groups() compile_filter = parser.compile_filter return FieldPrinterNode( obj_var=TemplateLiteral(compile_filter(obj_str), obj_str), field_var=TemplateLiteral(compile_filter(field_str), field_str), )
def _do_message_render(parser, token): try: tag_name, arg = token.contents.split( None, 1) # Splitting by None == splitting by spaces. except ValueError: raise TemplateSyntaxError, "%r tag requires arguments" % token.contents.split( )[0] match = _MESSAGE_RENDER_RE.search(arg) if not match: raise TemplateSyntaxError, "%r tag had invalid arguments" % tag_name message_str = match.groups()[0] return MessageRenderNode(message_var=TemplateLiteral( parser.compile_filter(message_str), message_str))
def create_var(self, value): return TemplateLiteral(self.template_parser.compile_filter(value), value)