Skip to content

Home Information - Your home, your data, organized.

Python

Migrate Location attribute editing to modal following Entity pattern (#129)

Tony C67046cf948cb
diff --git a/src/hi/apps/location/edit/tests/test_views.py b/src/hi/apps/location/edit/tests/test_views.py
index 74d5000d..389d960d 100644
--- a/src/hi/apps/location/edit/tests/test_views.py
+++ b/src/hi/apps/location/edit/tests/test_views.py
@@ -358,6 +358,57 @@ class TestLocationEditView(SyncViewTestCase):
self.assertEqual(response.status_code, 404)
+class TestLocationPropertiesEditView(SyncViewTestCase):
+ """
+ Tests for LocationPropertiesEditView - handles location properties (name, order_id, svg_view_box_str) editing only.
+ This view is used by the sidebar in edit mode and only accepts POST requests.
+ """
+
+ def setUp(self):
+ super().setUp()
+ # Create test location
+ self.location = Location.objects.create(
+ name='Test Properties Location',
+ svg_fragment_filename='test_props.svg',
+ svg_view_box_str='0 0 100 100'
+ )
+
+ def test_post_valid_properties_edit(self):
+ """Test successful location properties edit."""
+ url = reverse('location_properties_edit', kwargs={'location_id': self.location.id})
+
+ form_data = {
+ 'name': 'Updated Properties Name',
+ 'order_id': 5,
+ 'svg_view_box_str': '0 0 200 200',
+ }
+
+ response = self.client.post(url, form_data)
+
+ self.assertSuccessResponse(response)
+ self.assertJsonResponse(response)
+
+ # Verify location was actually updated
+ self.location.refresh_from_db()
+ self.assertEqual(self.location.name, 'Updated Properties Name')
+ self.assertEqual(self.location.order_id, 5)
+ self.assertEqual(self.location.svg_view_box_str, '0 0 200 200')
+
+ def test_nonexistent_location_returns_404(self):
+ """Test that editing nonexistent location returns 404."""
+ url = reverse('location_properties_edit', kwargs={'location_id': 99999})
+ response = self.client.post(url, {'name': 'Test', 'order_id': 1})
+
+ self.assertEqual(response.status_code, 404)
+
+ def test_get_not_allowed(self):
+ """Test that GET requests are not allowed (only POST)."""
+ url = reverse('location_properties_edit', kwargs={'location_id': self.location.id})
+ response = self.client.get(url)
+
+ self.assertEqual(response.status_code, 405)
+
+
class TestLocationAttributeUploadView(SyncViewTestCase):
"""
Tests for LocationAttributeUploadView - demonstrates location attribute upload testing.
diff --git a/src/hi/apps/location/edit/urls.py b/src/hi/apps/location/edit/urls.py
index 93d63a41..6e44f007 100644
--- a/src/hi/apps/location/edit/urls.py
+++ b/src/hi/apps/location/edit/urls.py
@@ -17,6 +17,10 @@ urlpatterns = [
views.LocationEditView.as_view(),
name='location_edit_location_edit'),
+ re_path( r'^location/properties/edit/(?P<location_id>\d+)$',
+ views.LocationPropertiesEditView.as_view(),
+ name='location_properties_edit'),
+
re_path( r'^location/attribute/upload/(?P<location_id>\d+)$',
views.LocationAttributeUploadView.as_view(),
name='location_attribute_upload'),
diff --git a/src/hi/apps/location/edit/view_mixins.py b/src/hi/apps/location/edit/view_mixins.py
index fa315501..57d6a607 100644
--- a/src/hi/apps/location/edit/view_mixins.py
+++ b/src/hi/apps/location/edit/view_mixins.py
@@ -9,27 +9,34 @@ from hi.constants import DIVID
class LocationEditViewMixin:
- def location_edit_response(
+ def location_modal_response(
self,
request : HttpRequest,
location_edit_data : LocationEditData,
status_code : int = 200 ):
-
+ """Return modal response for full location editing (properties + attributes)"""
context = location_edit_data.to_template_context()
- if request.view_parameters.is_editing:
- template = get_template( 'location/edit/panes/location_edit.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['LOCATION_EDIT_PANE']: content,
- },
- status = status_code,
- )
return antinode.modal_from_template(
request = request,
template_name = 'location/modals/location_edit.html',
context = context,
)
+
+ def location_properties_response(
+ self,
+ request : HttpRequest,
+ location_edit_data : LocationEditData,
+ status_code : int = 200 ):
+ """Return sidebar response for location properties editing only (name, order_id, svg_view_box_str)"""
+ context = location_edit_data.to_template_context()
+ template = get_template( 'location/edit/panes/location_properties_edit.html' )
+ content = template.render( context, request = request )
+ return antinode.response(
+ insert_map = {
+ DIVID['LOCATION_PROPERTIES_PANE']: content,
+ },
+ status = status_code,
+ )
def location_view_edit_response(
self,
diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.py
index cc92dd9c..5f9870b9 100644
--- a/src/hi/apps/location/edit/views.py
+++ b/src/hi/apps/location/edit/views.py
@@ -135,7 +135,7 @@ class LocationEditView( View, LocationViewMixin, LocationEditViewMixin ):
location_edit_data = LocationEditData(
location = location,
)
- return self.location_edit_response(
+ return self.location_modal_response(
request = request,
location_edit_data = location_edit_data,
status_code = 200,
@@ -182,7 +182,36 @@ class LocationEditView( View, LocationViewMixin, LocationEditViewMixin ):
location_edit_form = location_edit_form,
location_attribute_formset = location_attribute_formset,
)
- return self.location_edit_response(
+ return self.location_modal_response(
+ request = request,
+ location_edit_data = location_edit_data,
+ status_code = status_code,
+ )
+
+
+class LocationPropertiesEditView( View, LocationViewMixin, LocationEditViewMixin ):
+ """Handle location properties editing (name, order_id, svg_view_box_str) only - used by sidebar"""
+
+ def post( self, request, *args, **kwargs ):
+ location = self.get_location( request, *args, **kwargs )
+
+ location_edit_form = forms.LocationEditForm( request.POST, instance = location )
+ form_valid = location_edit_form.is_valid()
+
+ if form_valid:
+ with transaction.atomic():
+ location_edit_form.save()
+ status_code = 200
+ else:
+ status_code = 400
+
+ # For properties editing, we create LocationEditData without formset
+ location_edit_data = LocationEditData(
+ location = location,
+ location_edit_form = location_edit_form,
+ location_attribute_formset = None,
+ )
+ return self.location_properties_response(
request = request,
location_edit_data = location_edit_data,
status_code = status_code,
@@ -211,7 +240,7 @@ class LocationAttributeUploadView( View, LocationViewMixin, LocationEditViewMixi
location = location,
location_attribute_upload_form = location_attribute_upload_form,
)
- return self.location_edit_response(
+ return self.location_modal_response(
request = request,
location_edit_data = location_edit_data,
status_code = status_code,
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_properties_edit.html b/src/hi/apps/location/templates/location/edit/panes/location_properties_edit.html
new file mode 100644
index 00000000..3308a2a8
--- /dev/null
+++ b/src/hi/apps/location/templates/location/edit/panes/location_properties_edit.html
@@ -0,0 +1,34 @@
+{% load icons %}
+<form action="{% url 'location_properties_edit' location_id=location.id %}"
+ method="post"
+ class="form" data-async="true">
+ {% csrf_token %}
+ <div>
+ {% include "location/edit/panes/location_edit_form.html" %}
+ </div>
+
+ <div class="text-center" >
+ <button id="hi-location-properties-edit-button" type="submit" class="btn btn-primary"
+ name="action" value="update">
+ {% icon "save" size="sm" css_class="hi-icon-left" %}UPDATE
+ </button>
+ </div>
+</form>
+
+{% if request.view_parameters.is_editing %}
+<div class="text-center my-3" >
+ <a href="{% url 'location_edit_svg_replace' location_id=location.id %}"
+ id="hi-svg-replace-link" class="btn btn-primary btn-control" role="button"
+ data-async="true">
+ {% icon "upload" size="sm" css_class="hi-icon-left" %}REPLACE BACKGROUND SVG IMAGE
+ </a>
+</div>
+
+<div class="my-3">
+ <a href="{% url 'location_edit_location_delete' location_id=location.id %}"
+ id="hi-delete-location-link" class="btn btn-danger btn-control" role="button"
+ data-async="true">
+ {% icon "delete" size="sm" css_class="hi-icon-left" %}DELETE LOCATION AND ALL VIEWS
+ </a>
+</div>
+{% endif %}
\ No newline at end of file
diff --git a/src/hi/apps/location/templates/location/panes/location_details.html b/src/hi/apps/location/templates/location/panes/location_edit_mode_panel.html
similarity index 57%
rename from src/hi/apps/location/templates/location/panes/location_details.html
rename to src/hi/apps/location/templates/location/panes/location_edit_mode_panel.html
index 09a7edc2..5c0c7626 100644
--- a/src/hi/apps/location/templates/location/panes/location_details.html
+++ b/src/hi/apps/location/templates/location/panes/location_edit_mode_panel.html
@@ -1,5 +1,5 @@
<div class="d-flex justify-content-between my-2">
- <div class="align-self-center h4">Location</div>
+ <div class="align-self-center h4">Location Properties</div>
<div class="align-self-center">
<a href="{% url 'edit_item_details_close' %}"
class="btn btn-tertiary" role="button" data-async="true">
@@ -7,6 +7,6 @@
</a>
</div>
</div>
-<div id="{{ DIVID.LOCATION_EDIT_PANE }}">
- {% include "location/edit/panes/location_edit.html" %}
+<div id="{{ DIVID.LOCATION_PROPERTIES_PANE }}">
+ {% include "location/edit/panes/location_properties_edit.html" %}
</div>
diff --git a/src/hi/apps/location/tests/test_views.py b/src/hi/apps/location/tests/test_views.py
index fe7812bf..4b436e69 100644
--- a/src/hi/apps/location/tests/test_views.py
+++ b/src/hi/apps/location/tests/test_views.py
@@ -319,7 +319,7 @@ class TestLocationDetailsView(AsyncViewTestCase):
self.assertSuccessResponse(response)
self.assertJsonResponse(response)
- self.assertTemplateRendered(response, 'location/panes/location_details.html')
+ self.assertTemplateRendered(response, 'location/panes/location_edit_mode_panel.html')
def test_location_details_should_push_url(self):
"""Test that LocationDetailsView should push URL."""
diff --git a/src/hi/apps/location/views.py b/src/hi/apps/location/views.py
index f3e5baba..5e89a61e 100644
--- a/src/hi/apps/location/views.py
+++ b/src/hi/apps/location/views.py
@@ -92,7 +92,7 @@ class LocationSwitchView( View, LocationViewMixin ):
class LocationDetailsView( HiSideView, LocationViewMixin ):
def get_template_name( self ) -> str:
- return 'location/panes/location_details.html'
+ return 'location/panes/location_edit_mode_panel.html'
def should_push_url( self ):
return True
diff --git a/src/hi/constants.py b/src/hi/constants.py
index 5027f686..9f4eea1d 100644
--- a/src/hi/constants.py
+++ b/src/hi/constants.py
@@ -44,7 +44,7 @@ DIVID = {
'MAIN': 'hi-main-content',
'SIDE': 'hi-side-content',
- 'LOCATION_EDIT_PANE': 'hi-location-edit',
+ 'LOCATION_PROPERTIES_PANE': 'hi-location-properties',
'LOCATION_VIEW_EDIT_PANE': 'hi-location-view-edit',
'COLLECTION_EDIT_PANE': 'hi-collection-edit',
'ENTITY_PROPERTIES_PANE': 'hi-entity-properties',