From 14a74439d13d0aa62e1bccc5d45071f246329d10 Mon Sep 17 00:00:00 2001 From: Kerim Myratlyyev <137401913+Kerim-Myratlyev@users.noreply.github.com> Date: Fri, 5 Jan 2024 15:31:23 +0500 Subject: [PATCH] Update account_tags.py Updated UserDisplayNode to gracefully handle variable resolution errors and added checks for user presence. Improved the URLNextNode for better URL management with query parameters. These changes increase the robustness and maintainability of the template tags. --- account/templatetags/account_tags.py | 99 ++++++++++++---------------- 1 file changed, 43 insertions(+), 56 deletions(-) diff --git a/account/templatetags/account_tags.py b/account/templatetags/account_tags.py index 99d307dc..a1c122d9 100644 --- a/account/templatetags/account_tags.py +++ b/account/templatetags/account_tags.py @@ -1,6 +1,4 @@ from django import template -from django.template.base import kwarg_re -from django.template.defaulttags import URLNode from django.utils.html import conditional_escape from django.utils.http import urlencode @@ -8,7 +6,6 @@ register = template.Library() - class UserDisplayNode(template.Node): def __init__(self, user, as_var=None): @@ -16,96 +13,86 @@ def __init__(self, user, as_var=None): self.as_var = as_var def render(self, context): - user = self.user_var.resolve(context) - display = user_display(user) + try: + user = self.user_var.resolve(context) + except template.VariableDoesNotExist: + return "" + + display = user_display(user) if user else "Unknown User" if self.as_var: context[self.as_var] = display return "" return conditional_escape(display) - @register.tag(name="user_display") -def do_user_display(parser, token): # skipcq: PYL-W0613 - """ - Example usage:: - - {% user_display user %} - - or if you need to use in a {% blocktrans %}:: - - {% user_display user as user_display} - {% blocktrans %}{{ user_display }} has sent you a gift.{% endblocktrans %} - - """ +def do_user_display(parser, token): bits = token.split_contents() - if len(bits) == 2: - user = bits[1] - as_var = None - elif len(bits) == 4: - user = bits[1] - as_var = bits[3] - else: - raise template.TemplateSyntaxError("'{0}' takes either two or four arguments".format(bits[0])) + try: + if len(bits) == 2: + user = bits[1] + as_var = None + elif len(bits) == 4: + user = bits[1] + as_var = bits[3] + else: + raise template.TemplateSyntaxError( + "'{0}' takes either two or four arguments".format(bits[0]) + ) + except ValueError: + raise template.TemplateSyntaxError("Error in 'user_display' tag arguments") return UserDisplayNode(user, as_var) - class URLNextNode(URLNode): @staticmethod def add_next(url, context): - """ - With both `redirect_field_name` and `redirect_field_value` available in - the context, add on a querystring to handle "next" redirecting. - """ - if all( - key in context for key in ["redirect_field_name", "redirect_field_value"] - ) and context["redirect_field_value"]: - url += "?" + urlencode({ - context["redirect_field_name"]: context["redirect_field_value"], - }) + redirect_field_name = context.get("redirect_field_name") + redirect_field_value = context.get("redirect_field_value") + if redirect_field_name and redirect_field_value: + url += "?" + urlencode({redirect_field_name: redirect_field_value}) return url def render(self, context): - url = super(URLNextNode, self).render(context) + try: + url = super(URLNextNode, self).render(context) + except Exception as e: + return f"Error rendering URL: {e}" + if self.asvar: url = context[self.asvar] - # add on next handling + url = self.add_next(url, context) + if self.asvar: context[self.asvar] = url return "" return url - @register.tag def urlnext(parser, token): - """ - {% url %} copied from Django 1.7. - """ bits = token.split_contents() if len(bits) < 2: raise template.TemplateSyntaxError( - "'%s' takes at least one argument" - " (path to a view)" % bits[0] + "'%s' takes at least one argument (path to a view)" % bits[0] ) + viewname = parser.compile_filter(bits[1]) args = [] kwargs = {} asvar = None - bits = bits[2:] + if len(bits) >= 2 and bits[-2] == "as": asvar = bits[-1] bits = bits[:-2] - if len(bits) > 0: - for bit in bits: - match = kwarg_re.match(bit) - if not match: - raise template.TemplateSyntaxError("Malformed arguments to url tag") - name, value = match.groups() - if name: - kwargs[name] = parser.compile_filter(value) - else: - args.append(parser.compile_filter(value)) + for bit in bits[2:]: + match = kwarg_re.match(bit) + if not match: + raise template.TemplateSyntaxError("Malformed arguments to url tag") + name, value = match.groups() + if name: + kwargs[name] = parser.compile_filter(value) + else: + args.append(parser.compile_filter(value)) return URLNextNode(viewname, args, kwargs, asvar)