0 3 0
default
Branko Majic (branko) - 2 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:
↑ Collapse Diff ↑
...
 
@@ -35,7 +35,7 @@
35 35
 
              {% block header %}
36 36
 
            <ul class="nav">
37 37
 
              <li class="{% active_link 'index' %}"><a href="{% url "index" %}"><i class="icon-home icon-white"></i> Main Page</a></li>
38
 
              <li class="{% active_link 'admin' %}"><a href="{% url "admin:app_list" "conntrackt" %}"><i class="icon-wrench icon-white"></i> Administration</a></li>
38
 
              <li><a href="{% url "admin:app_list" "conntrackt" %}"><i class="icon-wrench icon-white"></i> Administration</a></li>
39 39
 
            </ul>
40 40
 
            <ul class="nav pull-right">
41 41
 
              <li>
...
 
@@ -23,6 +23,7 @@
23 23
 
from django import template
24 24
 
from django import urls
25 25
 
from django.utils.html import format_html
26
 
from django.urls import reverse
26 27
 

	
27 28
 

	
28 29
 
# Get an instance of Django's template library.
...
 
@@ -104,36 +105,34 @@ def active_link(context, url_name, retur
104 105
 
    return return_value if matches else ''
105 106
 

	
106 107
 

	
107
 
def current_url_equals(context, url_name, **kwargs):
108
 
def current_url_equals(context, view_name, *args, **kwargs):
108 109
 
    """
109
 
    Helper function for checking if the specified URL corresponds to the current
110
 
    request path in the context.
110
 
    Helper function for checking if the specified view with provided
111
 
    arguments corresponds to the current request path in the context.
112
 

	
113
 
    Passed-in positional and keyword arguments are used to resolve URL
114
 
    for views that use them.
111 115
 

	
112 116
 
    Arguments:
113 117
 

	
114
 
      - context - Context of the view being rendered.
118
 
      context
119
 
        Context of the view being rendered.
115 120
 

	
116
 
      - url_name - Name of the URL against which the context request path is
117
 
      being checked.
121
 
      view_name
122
 
        Name of the view against which the context request path is
123
 
        being checked.
124
 

	
125
 
      args
126
 
        Positional parametrs for the view.
127
 

	
128
 
      kwargs
129
 
        Keyword arguments for the view.
118 130
 
    """
119 131
 

	
120
 
    # Assume that we have not been able to resolve the request path to an URL.
121
 
    resolved = False
122
 
    try:
123
 
        # Use the request path, and resolve it to a URL name.
124
 
        resolved = urls.resolve(context.get('request').path)
125
 
    except urls.Resolver404:
126
 
        # This means we haven't been able to resolve the path from request.
127
 
        pass
132
 
    request = context.get('request')
133
 
    reversed_url = reverse(view_name, args=args, kwargs=kwargs)
128 134
 

	
129
 
    # If the request was resolved and URL names match, verify that the kwargs
130
 
    # match as well.
131
 
    matches = resolved and resolved.url_name == url_name
132
 
    if matches and kwargs:
133
 
        for key in kwargs:
134
 
            kwarg = kwargs.get(key)
135
 
            resolved_kwarg = resolved.kwargs.get(key)
136
 
            if not resolved_kwarg or kwarg != resolved_kwarg:
137
 
                return False
135
 
    if request.path == reversed_url:
136
 
        return True
138 137
 

	
139
 
    return matches
138
 
    return False
...
 
@@ -30,10 +30,14 @@ import mock
30 30
 
# Django imports.
31 31
 
from django.template import Context, Template, TemplateSyntaxError
32 32
 
from django.test import TestCase
33
 
from django.urls import reverse
33 34
 

	
34 35
 
# Application imports
35 36
 
from conntrackt.templatetags.conntrackt_tags import html_link, active_link, current_url_equals
36 37
 

	
38
 
# Test imports.
39
 
from .helpers import create_get_request
40
 

	
37 41
 

	
38 42
 
@mock.patch('conntrackt.templatetags.conntrackt_tags.urls.reverse')
39 43
 
class HtmlLinkTest(TestCase):
...
 
@@ -187,3 +191,59 @@ class HtmlLinkTest(TestCase):
187 191
 
        )
188 192
 

	
189 193
 
        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>')
194
 

	
195
 

	
196
 
class CurrentUrlEqualsTest(TestCase):
197
 

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

	
204
 
        Arguments:
205
 

	
206
 
          view
207
 
            View function or name for request object.
208
 

	
209
 
          args
210
 
            Positional arguments to pass into the view.
211
 

	
212
 
          kwargs
213
 
            Keyword arguments to pass into the view.
214
 

	
215
 
        Returns:
216
 

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

	
222
 
        return context
223
 

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

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

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

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

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

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

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

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

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

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