Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update account_tags.py #384

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 43 additions & 56 deletions account/templatetags/account_tags.py
Original file line number Diff line number Diff line change
@@ -1,111 +1,98 @@
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

from account.utils import user_display

register = template.Library()


class UserDisplayNode(template.Node):

def __init__(self, user, as_var=None):
self.user_var = template.Variable(user)
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)