Files
@ 6fe5a626d13d
Branch filter:
Location: conntrackt/conntrackt/templatetags/conntrackt_tags.py
6fe5a626d13d
3.7 KiB
text/x-python
CONNT-34: Rewrote current_url_equals implementation:
- Implemented tests.
- Simplified implementation.
- Added proper support for passing-in arguments to the view being
checked.
- Implemented tests.
- Simplified implementation.
- Added proper support for passing-in arguments to the view being
checked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Branko Majic
#
# This file is part of Django Conntrackt.
#
# Django Conntrackt is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Django Conntrackt is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Django Conntrackt. If not, see <http://www.gnu.org/licenses/>.
#
# Django imports.
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.
register = template.Library()
@register.simple_tag(takes_context=False)
def html_link(text, view, *args, **kwargs):
"""
A small wrapper for showing HTML links.
Positional arguments:
text - Text that should be used as a link.
view - View name for which the URL should be shown
args - Additional positional arguments that will be passed to resolver
for creating the URL.
Keyword arguments:
id - Identifier for the <a> HTML element.
class - Class(es) for the <a> HTML element.
title - Title for the HTML <a> element.
get - Additional GET parameter that should be appended to the URL.
"""
# Verify the passed-in keyword arguments first.
for key in kwargs.keys():
if key not in ("get", "class", "title", "id"):
raise template.TemplateSyntaxError("Unknown argument for 'html_link' tag: %r" % key)
# Generate the URL by using the supplied view name and arguments that should
# be passed to the view.
url = urls.reverse(view, args=args)
# Set-up the base pattern (url, parameters, text).
if 'get' in kwargs:
pattern = '<a href="{url}?{get}"'
else:
pattern = '<a href="{url}"'
if 'class' in kwargs:
pattern += ' class="{class}"'
if 'title' in kwargs:
pattern += ' title="{title}"'
if 'id' in kwargs:
pattern += ' id="{id}"'
pattern += '>{text}</a>'
# Render the tag.
return format_html(pattern, url=url, text=text, **kwargs)
@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, view_name, *args, **kwargs):
"""
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.
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.
"""
request = context.get('request')
reversed_url = reverse(view_name, args=args, kwargs=kwargs)
if request.path == reversed_url:
return True
return False
|