Changeset - 6fe5a626d13d
[Not reviewed]
default
0 3 0
Branko Majic (branko) - 6 years ago 2017-12-25 19:13:36
branko@majic.rs
CONNT-34: Rewrote current_url_equals implementation:

- Implemented tests.
- Simplified implementation.
- Added proper support for passing-in arguments to the view being
checked.
3 files changed with 84 insertions and 25 deletions:
0 comments (0 inline, 0 general)
conntrackt/templates/conntrackt/base.html
Show inline comments
 
@@ -32,13 +32,13 @@
 
          </button>
 
          {% block header_title %}{% html_link 'Conntrackt' 'index' class="brand" %}{% endblock %}
 
          <div class="nav-collapse collapse">
 
              {% block header %}
 
            <ul class="nav">
 
              <li class="{% active_link 'index' %}"><a href="{% url "index" %}"><i class="icon-home icon-white"></i> Main Page</a></li>
 
              <li class="{% active_link 'admin' %}"><a href="{% url "admin:app_list" "conntrackt" %}"><i class="icon-wrench icon-white"></i> Administration</a></li>
 
              <li><a href="{% url "admin:app_list" "conntrackt" %}"><i class="icon-wrench icon-white"></i> Administration</a></li>
 
            </ul>
 
            <ul class="nav pull-right">
 
              <li>
 
                <form action="{% url "search" %}" class="navbar-search pull-left" method="GET">
 
                  <div class="input-prepend">
 
                    <button type="submit" class="btn btn-link"><span class="icon-search icon-white"></span></button>
conntrackt/templatetags/conntrackt_tags.py
Show inline comments
 
@@ -20,12 +20,13 @@
 

	
 

	
 
# 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()
 

	
 

	
 
@@ -101,39 +102,37 @@ def active_link(context, url_name, retur
 

	
 
    matches = current_url_equals(context, url_name, **kwargs)
 

	
 
    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
 
      being checked.
 
      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:
 
                return False
 
    if request.path == reversed_url:
 
        return True
 

	
 
    return matches
 
    return False
conntrackt/tests/test_tags.py
Show inline comments
 
@@ -27,16 +27,20 @@ from zipfile import ZipFile, ZIP_DEFLATE
 
# Python third-party library imports.
 
import mock
 

	
 
# Django imports.
 
from django.template import Context, Template, TemplateSyntaxError
 
from django.test import TestCase
 
from django.urls import reverse
 

	
 
# Application imports
 
from conntrackt.templatetags.conntrackt_tags import html_link, active_link, current_url_equals
 

	
 
# Test imports.
 
from .helpers import create_get_request
 

	
 

	
 
@mock.patch('conntrackt.templatetags.conntrackt_tags.urls.reverse')
 
class HtmlLinkTest(TestCase):
 

	
 
    def test_url_reverse_called_with_passed_in_args(self, mock_reverse):
 
        """
 
@@ -184,6 +188,62 @@ class HtmlLinkTest(TestCase):
 
                "title": "my</a>_title",
 
                "get": "MyGetParameter=</a>",
 
            }
 
        )
 

	
 
        self.assertEqual(link, '<a href="/my/url?MyGetParameter=&lt;/a&gt;" class="my&lt;/a&gt;_class" title="my&lt;/a&gt;_title" id="my&lt;/a&gt;_id">My &lt;/a&gt; link</a>')
 

	
 

	
 
class CurrentUrlEqualsTest(TestCase):
 

	
 
    def get_context_for_view(self, view, *args, **kwargs):
 
        """
 
        Returns a Context instance where the request path has been
 
        constructed using the passed-in view (or view name), and view
 
        positional/keyword arguments.
 

	
 
        Arguments:
 

	
 
          view
 
            View function or name for request object.
 

	
 
          args
 
            Positional arguments to pass into the view.
 

	
 
          kwargs
 
            Keyword arguments to pass into the view.
 

	
 
        Returns:
 

	
 
          django.template.Context instance with request.
 
        """
 
        request = create_get_request(reverse(view, args=args, kwargs=kwargs))
 
        context = Context({'request': request})
 

	
 
        return context
 

	
 
    def test_non_matching_url_returns_false(self):
 
        request = create_get_request("/this/url/does/not/exist")
 
        context = Context({'request': request})
 

	
 
        self.assertEqual(current_url_equals(context, 'index'), False)
 

	
 
    def test_matching_url_returns_true(self):
 
        context = self.get_context_for_view('project_create')
 

	
 
        self.assertEqual(current_url_equals(context, 'project_create'), True)
 

	
 
    def test_matching_url_with_different_args_returns_false(self):
 
        context = self.get_context_for_view('project', 1)
 

	
 
        self.assertEqual(current_url_equals(context, 'project', 2), False)
 

	
 
    def test_matching_url_with_different_kwargs_returns_false(self):
 
        context = self.get_context_for_view('project', pk=1)
 

	
 
        self.assertEqual(current_url_equals(context, 'project', pk=2), False)
 

	
 
    def test_matching_url_with_GET_parameters_returns_true(self):
 
        request = create_get_request(reverse('project', kwargs={'pk': 1}) + '?my_get_param=10')
 
        context = Context({'request': request})
 

	
 
        self.assertEqual(current_url_equals(context, 'project', pk=1), True)
0 comments (0 inline, 0 general)