|
@@ -23,6 +23,7 @@
|
|
|
from django import template
|
|
|
from django import urls
|
|
|
from django.utils.html import format_html
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
|
# Get an instance of Django's template library.
|
|
@@ -104,36 +105,34 @@ def active_link(context, url_name, retur
|
|
|
return return_value if matches else ''
|
|
|
|
|
|
|
|
|
def current_url_equals(context, url_name, **kwargs):
|
|
|
def current_url_equals(context, view_name, *args, **kwargs):
|
|
|
"""
|
|
|
Helper function for checking if the specified URL corresponds to the current
|
|
|
request path in the context.
|
|
|
Helper function for checking if the specified view with provided
|
|
|
arguments corresponds to the current request path in the context.
|
|
|
|
|
|
Passed-in positional and keyword arguments are used to resolve URL
|
|
|
for views that use them.
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
- context - Context of the view being rendered.
|
|
|
context
|
|
|
Context of the view being rendered.
|
|
|
|
|
|
- url_name - Name of the URL against which the context request path is
|
|
|
view_name
|
|
|
Name of the view against which the context request path is
|
|
|
being checked.
|
|
|
|
|
|
args
|
|
|
Positional parametrs for the view.
|
|
|
|
|
|
kwargs
|
|
|
Keyword arguments for the view.
|
|
|
"""
|
|
|
|
|
|
# Assume that we have not been able to resolve the request path to an URL.
|
|
|
resolved = False
|
|
|
try:
|
|
|
# Use the request path, and resolve it to a URL name.
|
|
|
resolved = urls.resolve(context.get('request').path)
|
|
|
except urls.Resolver404:
|
|
|
# This means we haven't been able to resolve the path from request.
|
|
|
pass
|
|
|
request = context.get('request')
|
|
|
reversed_url = reverse(view_name, args=args, kwargs=kwargs)
|
|
|
|
|
|
# If the request was resolved and URL names match, verify that the kwargs
|
|
|
# match as well.
|
|
|
matches = resolved and resolved.url_name == url_name
|
|
|
if matches and kwargs:
|
|
|
for key in kwargs:
|
|
|
kwarg = kwargs.get(key)
|
|
|
resolved_kwarg = resolved.kwargs.get(key)
|
|
|
if not resolved_kwarg or kwarg != resolved_kwarg:
|
|
|
if request.path == reversed_url:
|
|
|
return True
|
|
|
|
|
|
return False
|
|
|
|
|
|
return matches
|