0 2 0
default
Branko Majic (branko) - 2 years ago 2017-12-25 19:39:03
branko@majic.rs
CONNT-34: Updated implementation for active_link Django template tag: - Added possibility of passing-in the view positional and keyword arguments. - Updated inline documentation. - Implemented tests.
2 files changed with 93 insertions and 5 deletions:
↑ Collapse Diff ↑
...
 
@@ -87,20 +87,32 @@ def html_link(text, view, *args, **kwarg
87 87
 

	
88 88
 

	
89 89
 
@register.simple_tag(takes_context=True)
90
 
def active_link(context, url_name, return_value='active', **kwargs):
90
 
def active_link(context, view_name, return_value='active', *args, **kwargs):
91 91
 
    """
92 92
 
    This template tag can be used to check if the provided URL matches against
93 93
 
    the path from the request or not.
94 94
 

	
95 95
 
    Arguments:
96 96
 

	
97
 
      context - Context of the current view being called.
97
 
      context
98
 
        Context of the view being rendered.
99
 

	
100
 
      view_name
101
 
        Name of the view against which the context request path is
102
 
        being checked.
98 103
 

	
99
 
      url_name - Name of the URL that's being checked against current path from
100
 
      request.
104
 
      args
105
 
        Positional parametrs for the view.
106
 

	
107
 
      kwargs
108
 
        Keyword arguments for the view.
109
 

	
110
 
    Returns:
111
 
      Value from return_value parameter if the rendered context
112
 
      corresponds to specified view, empty string otherwise.
101 113
 
    """
102 114
 

	
103
 
    matches = current_url_equals(context, url_name, **kwargs)
115
 
    matches = current_url_equals(context, view_name, *args, **kwargs)
104 116
 

	
105 117
 
    return return_value if matches else ''
106 118
 

	
...
 
@@ -127,6 +139,10 @@ def current_url_equals(context, view_nam
127 139
 

	
128 140
 
      kwargs
129 141
 
        Keyword arguments for the view.
142
 

	
143
 
    Returns:
144
 
      True, if the current URL equals to resolved view URL, False
145
 
      otherwise.
130 146
 
    """
131 147
 

	
132 148
 
    request = context.get('request')
...
 
@@ -247,3 +247,75 @@ class CurrentUrlEqualsTest(TestCase):
247 247
 
        context = Context({'request': request})
248 248
 

	
249 249
 
        self.assertEqual(current_url_equals(context, 'project', pk=1), True)
250
 

	
251
 

	
252
 
class ActiveLinkTest(TestCase):
253
 

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

	
260
 
        Arguments:
261
 

	
262
 
          view
263
 
            View function or name for request object.
264
 

	
265
 
          args
266
 
            Positional arguments to pass into the view.
267
 

	
268
 
          kwargs
269
 
            Keyword arguments to pass into the view.
270
 

	
271
 
        Returns:
272
 

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

	
278
 
        return context
279
 

	
280
 
    def test_empty_string_returned_on_view_without_arguments_and_no_match(self):
281
 

	
282
 
        context = self.get_context_for_view('index')
283
 

	
284
 
        self.assertEqual(active_link(context, 'project_create'), '')
285
 

	
286
 
    def test_default_value_returned_on_view_without_arguments_and_match(self):
287
 

	
288
 
        context = self.get_context_for_view('index')
289
 

	
290
 
        self.assertEqual(active_link(context, 'index'), 'active')
291
 

	
292
 
    def test_empty_string_returned_on_view_with_positional_arguments_and_no_match(self):
293
 

	
294
 
        context = self.get_context_for_view('project', 1)
295
 

	
296
 
        self.assertEqual(active_link(context, 'project', 'active', 2), '')
297
 

	
298
 
    def test_value_returned_on_view_with_positional_arguments_and_match(self):
299
 

	
300
 
        context = self.get_context_for_view('project', 1)
301
 

	
302
 
        self.assertEqual(active_link(context, 'project', 'active', 1), 'active')
303
 

	
304
 
    def test_empty_string_returned_on_view_with_keyword_arguments_and_no_match(self):
305
 

	
306
 
        context = self.get_context_for_view('project', 1)
307
 

	
308
 
        self.assertEqual(active_link(context, 'project', pk=2), '')
309
 

	
310
 
    def test_default_value_returned_on_view_with_keyword_arguments_and_match(self):
311
 

	
312
 
        context = self.get_context_for_view('project', 1)
313
 

	
314
 
        self.assertEqual(active_link(context, 'project', pk=1), 'active')
315
 

	
316
 
    def test_provided_value_returned_on_view_with_matching_url(self):
317
 

	
318
 
        context = self.get_context_for_view('project', 1)
319
 

	
320
 
        self.assertEqual(active_link(context, 'project', 'myvalue', 1), 'myvalue')
321
 
        self.assertEqual(active_link(context, 'project', 'myvalue', pk=1), 'myvalue')
0 comments (0 inline, 0 general)