|
|
# Import Django's template library.
|
|
|
from django import template
|
|
|
|
|
|
# Import for determining the active URL.
|
|
|
from django.core import urlresolvers
|
|
|
|
|
|
# Get an instance of Django's template library.
|
|
|
register = template.Library()
|
|
|
|
|
|
@register.inclusion_tag('conntrackt/link.html')
|
|
|
def html_link(view, title, identifier = None, html_class = None):
|
|
|
'''
|
|
|
"""
|
|
|
A small wrapper for showing HTML links.
|
|
|
|
|
|
Arguments:
|
|
@@ -17,22 +21,53 @@ def html_link(view, title, identifier =
|
|
|
identifier - Identifier which is passed to the view for processing.
|
|
|
|
|
|
html_class - Class which should be assigned to the link.
|
|
|
'''
|
|
|
"""
|
|
|
|
|
|
return {'view': view, 'identifier': identifier, 'title': title, 'html_class': html_class}
|
|
|
|
|
|
@register.simple_tag(takes_context = True)
|
|
|
def active_link(context, url_name, return_value='active', **kwargs):
|
|
|
"""
|
|
|
This template tag can be used to check if the provided URL matches against
|
|
|
the path from the request or not.
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
context - Context of the current view being called.
|
|
|
|
|
|
url_name - Name of the URL that's being checked against current path from
|
|
|
request.
|
|
|
"""
|
|
|
|
|
|
matches = current_url_equals(context, url_name, **kwargs)
|
|
|
|
|
|
return return_value if matches else ''
|
|
|
|
|
|
|
|
|
def current_url_equals(context, url_name, **kwargs):
|
|
|
"""
|
|
|
Helper function for checking if the specified URL corresponds to the current
|
|
|
request path in the context.
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
- context - Context of the view being rendered.
|
|
|
|
|
|
- url_name - Name of the URL against which the context request path is
|
|
|
being checked.
|
|
|
"""
|
|
|
|
|
|
# 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 = urlresolvers.resolve(context.get('request').path)
|
|
|
except:
|
|
|
except urlresolvers.Resolver404:
|
|
|
# This means we haven't been able to resolve the path from request.
|
|
|
pass
|
|
|
|
|
|
# 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:
|
|
@@ -40,5 +75,6 @@ def current_url_equals(context, url_name
|
|
|
resolved_kwarg = resolved.kwargs.get(key)
|
|
|
if not resolved_kwarg or kwarg != resolved_kwarg:
|
|
|
return False
|
|
|
|
|
|
return matches
|
|
|
|