# HG changeset patch # User Branko Majic # Date 2013-07-24 22:25:56 # Node ID 491fa548a42fcdab4da9dcf2d088387e141d8964 # Parent c153097ca1994c771b82936757b724bda73bbc05 CONNT-6: Reworked the way success URL is constructed when creating communication. Includes updates to tests. diff --git a/conntrackt/templates/conntrackt/entity_detail.html b/conntrackt/templates/conntrackt/entity_detail.html --- a/conntrackt/templates/conntrackt/entity_detail.html +++ b/conntrackt/templates/conntrackt/entity_detail.html @@ -89,7 +89,8 @@ {% endif %}
{% with entity_id=entity.id|slugify %} - {% html_link "Add communication" "communication_create" class="btn btn-primary btn-mini" get="to_entity="|add:entity_id %} + {% url "entity" entity.id as next %} + {% html_link "Add communication" "communication_create" class="btn btn-primary btn-mini" get="to_entity="|add:entity_id|add:"&next="|add:next %} {% endwith %}
@@ -113,7 +114,8 @@ {% endif %}
{% with entity_id=entity.id|slugify %} - {% html_link "Add communication" "communication_create" class="btn btn-primary btn-mini" get="from_entity="|add:entity_id %} + {% url "entity" entity.id as next %} + {% html_link "Add communication" "communication_create" class="btn btn-primary btn-mini" get="from_entity="|add:entity_id|add:"&next="|add:next %} {% endwith %}
diff --git a/conntrackt/tests/test_views.py b/conntrackt/tests/test_views.py --- a/conntrackt/tests/test_views.py +++ b/conntrackt/tests/test_views.py @@ -1062,10 +1062,10 @@ class CommunicationCreateViewTest(Permis self.assertEqual(len(initial), 0) - def test_success_url_from_entity(self): + def test_success_url_next(self): """ Validate that the success URL is set properly after communication is - created if origin entity is provided. + created if "next" GET parameter is provided. """ # Get the view. @@ -1079,47 +1079,20 @@ class CommunicationCreateViewTest(Permis "protocol": "TCP", "port": "22", "description": "SSH."} - request = RequestFactory().post("/fake-path?from_entity=1", data=post_data) + request = RequestFactory().post("/fake-path?next=/next-page", data=post_data) request.user = mock.Mock() request._dont_enforce_csrf_checks = True # Get the response. response = view(request) - self.assertEqual(response["Location"], reverse("entity", args=(1,))) + self.assertEqual(response["Location"], "/next-page") self.assertEqual(response.status_code, 302) - def test_success_url_to_entity(self): + def test_success_url_no_next(self): """ Validate that the success URL is set properly after communication is - created if destination entity is provided. - """ - - # Get the view. - view = CommunicationCreateView.as_view() - - # Generate the request. - source = Interface.objects.get(pk=1) - destination = Interface.objects.get(pk=2) - post_data = {"source": source.pk, - "destination": destination.pk, - "protocol": "TCP", - "port": "22", - "description": "SSH."} - request = RequestFactory().post("/fake-path?to_entity=2", data=post_data) - request.user = mock.Mock() - request._dont_enforce_csrf_checks = True - - # Get the response. - response = view(request) - - self.assertEqual(response["Location"], reverse("entity", args=(2,))) - self.assertEqual(response.status_code, 302) - - def test_success_url_no_entity(self): - """ - Validate that the success URL is set properly after communication is - created if no entity source/destinantion is passed on. + created if no "next" GET parameter is provided. """ # Get the view. @@ -1140,7 +1113,7 @@ class CommunicationCreateViewTest(Permis # Get the response. response = view(request) - self.assertEqual(response["Location"], reverse("entity", args=(1,))) + self.assertEqual(response["Location"], reverse("project", args=(1,))) self.assertEqual(response.status_code, 302) @@ -1190,9 +1163,10 @@ class CommunicationUpdateViewTest(Permis self.assertQuerysetEqual(form.fields["source"].queryset, expected_interfaces) self.assertQuerysetEqual(form.fields["destination"].queryset, expected_interfaces) - def test_success_url_from_entity(self): + def test_success_url_next(self): """ - Validate that the success URL is set properly after update. + Validate that the success URL is set properly after update if GET + parameter is passed. """ # Get the view. @@ -1206,45 +1180,20 @@ class CommunicationUpdateViewTest(Permis "destination": communication.destination.pk, "protocol": communication.protocol, "port": communication.port} - request = RequestFactory().post("/fake-path?from_entity=1", data=post_data) + request = RequestFactory().post("/fake-path?next=/next-page", data=post_data) request.user = mock.Mock() request._dont_enforce_csrf_checks = True # Get the response. response = view(request, pk=1) - self.assertEqual(response["Location"], reverse("entity", args=(1,))) + self.assertEqual(response["Location"], "/next-page") self.assertEqual(response.status_code, 302) - def test_success_url_to_entity(self): - """ - Validate that the success URL is set properly after update. + def test_success_url_no_next(self): """ - - # Get the view. - view = CommunicationUpdateView.as_view() - - # Get the communication object. - communication = Communication.objects.get(pk=1) - - # Generate the request. - post_data = {"source": communication.source.pk, - "destination": communication.destination.pk, - "protocol": communication.protocol, - "port": communication.port} - request = RequestFactory().post("/fake-path?to_entity=1", data=post_data) - request.user = mock.Mock() - request._dont_enforce_csrf_checks = True - - # Get the response. - response = view(request, pk=1) - - self.assertEqual(response["Location"], reverse("entity", args=(1,))) - self.assertEqual(response.status_code, 302) - - def test_success_url_no_entity(self): - """ - Validate that the success URL is set properly after update. + Validate that the success URL is set properly after communication is + created if no "next" GET parameter is provided. """ # Get the view. @@ -1265,7 +1214,7 @@ class CommunicationUpdateViewTest(Permis # Get the response. response = view(request, pk=1) - self.assertEqual(response["Location"], reverse("entity", args=(communication.source.entity.pk,))) + self.assertEqual(response["Location"], reverse("project", args=(communication.source.entity.project.id,))) self.assertEqual(response.status_code, 302) diff --git a/conntrackt/views.py b/conntrackt/views.py --- a/conntrackt/views.py +++ b/conntrackt/views.py @@ -707,15 +707,11 @@ class CommunicationCreateView(MultiplePe Returns the URL to which the user should be redirected after a communication has been created. - The URL will be set to entity details page of an entity that was - provided as part of the from/to GET request (in that order), or as a - fallback it'll direct the user to source interface's entity details - page. + The URL will either point to value provided via GET parameter "next", or + to project page to which the communication belongs. """ - entity_id = self.request.GET.get("from_entity", None) or self.request.GET.get("to_entity", None) or self.object.source.entity.pk - - return reverse("entity", args=(entity_id,)) + return self.request.GET.get("next", reverse("project", args=(self.object.source.entity.project.pk,))) class CommunicationUpdateView(MultiplePermissionsRequiredMixin, UpdateView): @@ -759,15 +755,11 @@ class CommunicationUpdateView(MultiplePe Returns the URL to which the user should be redirected after a communication has been created. - The URL will be set to entity details page of an entity that was - provided as part of the from/to GET request (in that order), or as a - fallback it'll direct the user to source interface's entity details - page. + The URL will either point to value provided via GET parameter "next", or + to project page to which the communication belongs. """ - entity_id = self.request.GET.get("from_entity", None) or self.request.GET.get("to_entity", None) or self.object.source.entity.pk - - return reverse("entity", args=(entity_id,)) + return self.request.GET.get("next", reverse("project", args=(self.object.source.entity.project.pk,))) class CommunicationDeleteView(MultiplePermissionsRequiredMixin, DeleteView):