Skip to content

Home Information - Your home, your data, organized.

Python

Fixed some redirection bugs around Location edit views.

Anthony Cassandrafec3a90984a0
diff --git a/src/hi/apps/location/edit/tests/test_svg_edit_views.py b/src/hi/apps/location/edit/tests/test_svg_edit_views.py
index 6d7878b8..0bddd51d 100644
--- a/src/hi/apps/location/edit/tests/test_svg_edit_views.py
+++ b/src/hi/apps/location/edit/tests/test_svg_edit_views.py
@@ -2,6 +2,7 @@ import logging
import os
import shutil
import tempfile
+import urllib.parse
from django.core.files.storage import default_storage
from django.test import override_settings
@@ -66,6 +67,15 @@ class LocationSvgEditViewTestBase(DualModeViewTestCase):
if content is not None:
self.manager.save_draft_svg(self.location, content)
+ def _assert_exit_redirect_to_location_edit(self, response):
+ """Editor exit paths redirect to home with the location edit sidebar loaded."""
+ expected_side_url = reverse(
+ 'location_edit_mode', kwargs={'location_id': self.location.id}
+ )
+ redirect_url = response.json().get('location', '')
+ self.assertIn(reverse('home'), redirect_url)
+ self.assertIn(f'details={urllib.parse.quote(expected_side_url, safe="")}', redirect_url)
+
class TestLocationSvgEditView(LocationSvgEditViewTestBase):
"""Tests for LocationSvgEditView - GET creates draft and renders editor page."""
@@ -142,6 +152,8 @@ class TestLocationSvgEditCancelView(LocationSvgEditViewTestBase):
self.assertJsonResponse(response)
# Should clean up draft
self.assertFalse(self.manager.draft_svg_exists(self.location))
+ # Redirect should land back on home with the location edit sidebar loaded
+ self._assert_exit_redirect_to_location_edit(response)
def test_get_with_changes_shows_modal(self):
"""GET with draft changes should show confirmation modal."""
@@ -155,7 +167,7 @@ class TestLocationSvgEditCancelView(LocationSvgEditViewTestBase):
self.assertTemplateRendered(response, 'location/edit/modals/location_svg_edit_cancel.html')
def test_post_deletes_draft_and_redirects(self):
- """POST should delete draft and redirect to home."""
+ """POST should delete draft and redirect to the location edit sidebar."""
self._create_draft('<circle r="999"/>')
url = reverse('location_svg_edit_cancel', kwargs={'location_id': self.location.id})
@@ -164,6 +176,7 @@ class TestLocationSvgEditCancelView(LocationSvgEditViewTestBase):
self.assertSuccessResponse(response)
self.assertJsonResponse(response)
self.assertFalse(self.manager.draft_svg_exists(self.location))
+ self._assert_exit_redirect_to_location_edit(response)
def test_post_does_not_modify_live_svg(self):
"""POST cancel should leave the live SVG file unchanged."""
@@ -195,6 +208,7 @@ class TestLocationSvgEditExitView(LocationSvgEditViewTestBase):
self.assertSuccessResponse(response)
self.assertJsonResponse(response)
+ self._assert_exit_redirect_to_location_edit(response)
def test_get_with_changes_shows_modal(self):
"""GET with draft changes should show confirmation modal."""
@@ -214,7 +228,7 @@ class TestLocationSvgEditExitView(LocationSvgEditViewTestBase):
old_filename = self.location.svg_fragment_filename
url = reverse('location_svg_edit_exit', kwargs={'location_id': self.location.id})
- self.client.post(url)
+ response = self.client.post(url)
self.location.refresh_from_db()
# Commit writes to a new unique filename
@@ -222,6 +236,7 @@ class TestLocationSvgEditExitView(LocationSvgEditViewTestBase):
with default_storage.open(self.location.svg_fragment_filename, 'r') as f:
live_content = f.read()
self.assertEqual(live_content, edited_content)
+ self._assert_exit_redirect_to_location_edit(response)
def test_post_removes_draft_file(self):
"""POST should remove the draft file after committing."""
diff --git a/src/hi/apps/location/edit/tests/test_views.py b/src/hi/apps/location/edit/tests/test_views.py
index 38d6a1ff..ccf81bd6 100644
--- a/src/hi/apps/location/edit/tests/test_views.py
+++ b/src/hi/apps/location/edit/tests/test_views.py
@@ -122,10 +122,12 @@ class TestLocationAddView(DualModeViewTestCase):
# Test actual redirect behavior (JSON redirect)
self.assertSuccessResponse(response)
self.assertJsonResponse(response)
-
+
data = response.json()
home_url = reverse('home')
self.assertTrue(data['location'].startswith(home_url))
+ # Normal add redirects to home with the location edit sidebar preloaded
+ self.assertIn('details=', data['location'])
# Test that new Location was created
self.assertEqual(Location.objects.count(), initial_location_count + 1)
@@ -161,6 +163,54 @@ class TestLocationAddView(DualModeViewTestCase):
self.assertEqual(Location.objects.filter(name='').count(), 0)
+class TestLocationAddFirstView(DualModeViewTestCase):
+ """
+ Tests for LocationAddFirstView - used during first-time profile
+ initialization. Differs from LocationAddView only in its post-create
+ redirect target: plain home (where the Getting Started helper sidebar
+ guides next steps), not home-with-location-edit-sidebar.
+ """
+
+ def setUp(self):
+ super().setUp()
+ self.setSessionViewMode(ViewMode.EDIT)
+ self._temp_media_dir = tempfile.mkdtemp()
+ self._settings_patcher = override_settings(MEDIA_ROOT=self._temp_media_dir)
+ self._settings_patcher.enable()
+
+ def tearDown(self):
+ if hasattr(self, '_settings_patcher'):
+ self._settings_patcher.disable()
+ if hasattr(self, '_temp_media_dir'):
+ shutil.rmtree(self._temp_media_dir, ignore_errors=True)
+ LocationManager._instance = None
+ CollectionManager._instance = None
+ EntityManager._instance = None
+ super().tearDown()
+
+ def test_post_redirects_to_plain_home(self):
+ """First-time location add lands on plain home so the Getting Started
+ sidebar can guide the user, not on the Location Edit sidebar."""
+ form_data = {
+ 'name': 'First Location',
+ 'use_default_svg_file': 'on',
+ }
+ url = reverse('location_edit_location_add_first')
+ response = self.client.post(url, form_data)
+
+ self.assertSuccessResponse(response)
+ self.assertJsonResponse(response)
+
+ data = response.json()
+ home_url = reverse('home')
+ # Must be plain home, NOT home with a details= sidebar parameter
+ self.assertEqual(data['location'], home_url)
+ self.assertNotIn('details=', data['location'])
+
+ # Still creates the location like the parent class
+ self.assertTrue(Location.objects.filter(name='First Location').exists())
+
+
class TestLocationSvgReplaceView(DualModeViewTestCase):
"""
Tests for LocationSvgReplaceView - demonstrates SVG replacement testing.
diff --git a/src/hi/apps/location/edit/urls.py b/src/hi/apps/location/edit/urls.py
index 309ab33b..1e367127 100644
--- a/src/hi/apps/location/edit/urls.py
+++ b/src/hi/apps/location/edit/urls.py
@@ -5,10 +5,14 @@ from . import views
urlpatterns = [
- re_path( r'^add$',
- views.LocationAddView.as_view(),
+ re_path( r'^add$',
+ views.LocationAddView.as_view(),
name='location_edit_location_add'),
+ re_path( r'^add-first$',
+ views.LocationAddFirstView.as_view(),
+ name='location_edit_location_add_first'),
+
re_path( r'^edit-mode/(?P<location_id>\d+)$',
views.LocationEditModeView.as_view(),
name='location_edit_mode' ),
diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.py
index 576346a6..8527d264 100644
--- a/src/hi/apps/location/edit/views.py
+++ b/src/hi/apps/location/edit/views.py
@@ -2,7 +2,6 @@ import json
import logging
import os
import re
-import urllib.parse
from django.core.exceptions import BadRequest
from django.core.files.storage import default_storage
@@ -117,7 +116,7 @@ class LocationItemEditModeView( View ):
@method_decorator( edit_required, name='dispatch' )
-class LocationAddView( HiModalView ):
+class LocationAddView( HiModalView, LocationViewMixin ):
def get_template_name( self ) -> str:
return 'location/edit/modals/location_add.html'
@@ -155,12 +154,7 @@ class LocationAddView( HiModalView ):
return self.post_create_redirect( request, location, location_view )
def post_create_redirect( self, request, location, location_view ):
- side_url = reverse( 'location_edit_mode', kwargs={ 'location_id': location.id } )
- redirect_url = (
- reverse( 'home' )
- + '?' + urllib.parse.urlencode({ HiSideView.SIDE_URL_PARAM_NAME: side_url })
- )
- return antinode.redirect_response( redirect_url )
+ return self.redirect_to_location_edit_side_view( location )
class LocationAddFirstView( LocationAddView ):
@@ -666,8 +660,7 @@ class LocationSvgEditCancelView( HiModalView, LocationViewMixin ):
if not has_changes:
request.session.pop( session_key, None )
manager.delete_draft_svg( location )
- redirect_url = reverse('home')
- return antinode.redirect_response( redirect_url )
+ return self.redirect_to_location_edit_side_view( location )
context = { 'location': location }
return self.modal_response( request, context )
@@ -677,8 +670,7 @@ class LocationSvgEditCancelView( HiModalView, LocationViewMixin ):
session_key = f'{LocationSvgEditView.SVG_EDIT_VIEWBOX_SESSION_PREFIX}{location.id}'
request.session.pop( session_key, None )
LocationManager().delete_draft_svg( location )
- redirect_url = reverse('home')
- return antinode.redirect_response( redirect_url )
+ return self.redirect_to_location_edit_side_view( location )
@method_decorator( edit_required, name='dispatch' )
@@ -698,8 +690,7 @@ class LocationSvgEditExitView( HiModalView, LocationViewMixin ):
if not has_changes:
request.session.pop( session_key, None )
manager.delete_draft_svg( location )
- redirect_url = reverse('home')
- return antinode.redirect_response( redirect_url )
+ return self.redirect_to_location_edit_side_view( location )
context = { 'location': location }
return self.modal_response( request, context )
@@ -716,8 +707,7 @@ class LocationSvgEditExitView( HiModalView, LocationViewMixin ):
location.svg_view_box_str = viewbox_str
location.save()
- redirect_url = reverse('home')
- return antinode.redirect_response( redirect_url )
+ return self.redirect_to_location_edit_side_view( location )
@method_decorator( edit_required, name='dispatch' )
diff --git a/src/hi/apps/location/templates/location/edit/modals/location_add_first.html b/src/hi/apps/location/templates/location/edit/modals/location_add_first.html
index b07f75fd..66bbfa51 100644
--- a/src/hi/apps/location/templates/location/edit/modals/location_add_first.html
+++ b/src/hi/apps/location/templates/location/edit/modals/location_add_first.html
@@ -2,7 +2,7 @@
{% load icons %}
{% block preamble %}
-<form action="{% url 'location_edit_location_add' %}" method="post" enctype="multipart/form-data"
+<form action="{% url 'location_edit_location_add_first' %}" method="post" enctype="multipart/form-data"
class="form" data-async="modal">
{% csrf_token %}
{% endblock %}
diff --git a/src/hi/apps/location/view_mixins.py b/src/hi/apps/location/view_mixins.py
index d11fb16c..1d4cfe6a 100644
--- a/src/hi/apps/location/view_mixins.py
+++ b/src/hi/apps/location/view_mixins.py
@@ -1,5 +1,8 @@
+import urllib.parse
+
from django.core.exceptions import BadRequest
from django.http import Http404, HttpResponse
+from django.urls import reverse
import hi.apps.common.antinode as antinode
from hi.apps.entity.models import Entity
@@ -7,6 +10,7 @@ from hi.apps.location.location_manager import LocationManager
from hi.apps.location.models import Location, LocationView
from hi.apps.monitor.status_display_data import StatusDisplayData
from hi.apps.monitor.status_display_manager import StatusDisplayManager
+from hi.hi_async_view import HiSideView
class LocationViewMixin:
@@ -41,6 +45,15 @@ class LocationViewMixin:
except LocationView.DoesNotExist:
raise Http404( request )
+ def redirect_to_location_edit_side_view( self, location : Location ) -> HttpResponse:
+ """ Redirect to home with the location edit sidebar loaded alongside. """
+ side_url = reverse( 'location_edit_mode', kwargs={ 'location_id': location.id } )
+ redirect_url = (
+ reverse( 'home' )
+ + '?' + urllib.parse.urlencode({ HiSideView.SIDE_URL_PARAM_NAME: side_url })
+ )
+ return antinode.redirect_response( redirect_url )
+
def get_entity_svg_update_reponse( self, entity : Entity ) -> HttpResponse:
""" For updating a single entity in the location view vis antinode reponse """