Changeset - ea8aa73f8ebb
[Not reviewed]
default
0 2 1
Branko Majic (branko) - 11 years ago 2013-08-02 21:24:20
branko@majic.rs
CONNT-12: Implemented the mixin for returning success URL using the passed GET parameter.
3 files changed with 98 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conntrackt/tests/test_views.py
Show inline comments
 
@@ -23,7 +23,10 @@ from conntrackt.views import EntityView,
 
from conntrackt.views import InterfaceCreateView, InterfaceUpdateView, InterfaceDeleteView
 
from conntrackt.views import CommunicationCreateView, CommunicationUpdateView, CommunicationDeleteView
 

	
 
from helpers import PermissionTestMixin, create_get_request, generate_get_response, FakeMessages
 
# Test imports.
 
from .forms import FormWithWidgetCSSClassFormMixin, FormWithPlaceholderFormMixin
 
from .helpers import PermissionTestMixin, create_get_request, generate_get_response, FakeMessages
 
from .views import RedirectToNextMixinView
 

	
 

	
 
class IndexViewTest(PermissionTestMixin, TestCase):
 
@@ -1382,3 +1385,48 @@ class ProjectDiagramTest(PermissionTestM
 

	
 
        self.assertContains(response, '"-//W3C//DTD SVG 1.1//EN"')
 
        self.assertContains(response, "Test Project 1")
 

	
 

	
 
class RedirectToNextMixinTest(TestCase):
 

	
 
    def test_request_with_next(self):
 
        """
 
        Test if the get_success_url returns correct URL if "next" is present in
 
        request's GET parameters.
 
        """
 

	
 
        # Generate the request.
 
        request = RequestFactory().post("/fake-path?next=/next")
 

	
 
        # Initialise the pseudo-view.
 
        view = RedirectToNextMixinView(request)
 

	
 
        self.assertEqual("/next", view.get_success_url())
 

	
 
    def test_request_without_next(self):
 
        """
 
        Test if the get_success_url returns correct URL if "next" is not present
 
        in request's GET parameters.
 
        """
 

	
 
        # Generate the request.
 
        request = RequestFactory().post("/fake-path")
 

	
 
        # Initialise the pseudo-view.
 
        view = RedirectToNextMixinView(request)
 

	
 
        self.assertEqual("/STATIC", view.get_success_url())
 

	
 
    def test_request_custom_parameter_name(self):
 
        """
 
        Test if the mixin honours the custom parameter name.
 
        """
 

	
 
        # Generate the request.
 
        request = RequestFactory().post("/fake-path?custom=/next")
 

	
 
        # Initialise the pseudo-view.
 
        view = RedirectToNextMixinView(request)
 
        view.next_parameter = "custom"
 

	
 
        self.assertEqual("/next", view.get_success_url())
conntrackt/tests/views.py
Show inline comments
 
new file 100644
 
# Application imports.
 
from conntrackt.views import RedirectToNextMixin
 

	
 

	
 
class StaticSuccessUrlFakeMixin(object):
 
    """
 
    Helper view for testing the RedirectToNextMixinView mixin.
 
    """
 

	
 
    def get_success_url(self):
 
        return self.success_url
 

	
 

	
 
class RedirectToNextMixinView(RedirectToNextMixin, StaticSuccessUrlFakeMixin):
 
    """
 
    Helper view for testing the RedirectToNextMixinView mixin. StaticSuccessUrl
 
    is there just to provide default for get_success_url().
 
    """
 

	
 
    success_url = "/STATIC"
 

	
 
    def __init__(self, request):
 
        """
 
        Initialise the request to provided value.
 
        """
 

	
 
        self.request = request
conntrackt/views.py
Show inline comments
 
@@ -19,6 +19,28 @@ from .models import Project, Entity, Loc
 
from .utils import generate_entity_iptables, generate_project_diagram
 

	
 

	
 
class RedirectToNextMixin(object):
 
    """
 
    View mixin that can be used for redirecting the user to URL defined through
 
    a GET parameter. The mixin is usable with Create/Update/Delete views that
 
    utilise the get_success_url() call.
 

	
 
    The mixin accepts the following class options:
 

	
 
        next_parameter - Name of the GET parameter that contains the redirect
 
        URL. Defaults to "next".
 
    """
 

	
 
    next_parameter = "next"
 

	
 
    def get_success_url(self):
 
        """
 
        Returns the success URL to which the user will be redirected.
 
        """
 

	
 
        return self.request.GET.get(self.next_parameter, super(RedirectToNextMixin, self).get_success_url())
 

	
 

	
 
class IndexView(MultiplePermissionsRequiredMixin, TemplateView):
 
    """
 
    Custom view used for rendering the index page.
0 comments (0 inline, 0 general)