Changeset - 632e4747042f
[Not reviewed]
default
0 2 0
Branko Majic (branko) - 7 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:
0 comments (0 inline, 0 general)
conntrackt/templatetags/conntrackt_tags.py
Show inline comments
 
@@ -87,20 +87,32 @@ def html_link(text, view, *args, **kwarg
 

	
 

	
 
@register.simple_tag(takes_context=True)
 
def active_link(context, url_name, return_value='active', **kwargs):
 
def active_link(context, view_name, return_value='active', *args, **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.
 
      context
 
        Context of the view being rendered.
 

	
 
      view_name
 
        Name of the view against which the context request path is
 
        being checked.
 

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

	
 
      kwargs
 
        Keyword arguments for the view.
 

	
 
    Returns:
 
      Value from return_value parameter if the rendered context
 
      corresponds to specified view, empty string otherwise.
 
    """
 

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

	
 
    return return_value if matches else ''
 

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

	
 
      kwargs
 
        Keyword arguments for the view.
 

	
 
    Returns:
 
      True, if the current URL equals to resolved view URL, False
 
      otherwise.
 
    """
 

	
 
    request = context.get('request')
conntrackt/tests/test_tags.py
Show inline comments
 
@@ -247,3 +247,75 @@ class CurrentUrlEqualsTest(TestCase):
 
        context = Context({'request': request})
 

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

	
 

	
 
class ActiveLinkTest(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_empty_string_returned_on_view_without_arguments_and_no_match(self):
 

	
 
        context = self.get_context_for_view('index')
 

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

	
 
    def test_default_value_returned_on_view_without_arguments_and_match(self):
 

	
 
        context = self.get_context_for_view('index')
 

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

	
 
    def test_empty_string_returned_on_view_with_positional_arguments_and_no_match(self):
 

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

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

	
 
    def test_value_returned_on_view_with_positional_arguments_and_match(self):
 

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

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

	
 
    def test_empty_string_returned_on_view_with_keyword_arguments_and_no_match(self):
 

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

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

	
 
    def test_default_value_returned_on_view_with_keyword_arguments_and_match(self):
 

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

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

	
 
    def test_provided_value_returned_on_view_with_matching_url(self):
 

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

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