Skip to content

Home Information - Your home, your data, organized.

Python

Added Location edit features.

Anthony Cassandra7601f86adc2f
diff --git a/src/hi/apps/common/svg_forms.py b/src/hi/apps/common/svg_forms.py
index f8859f16..af591ccb 100644
--- a/src/hi/apps/common/svg_forms.py
+++ b/src/hi/apps/common/svg_forms.py
@@ -49,13 +49,21 @@ class SvgFileForm(forms.Form):
required = False,
)
+ def allow_default_svg_file(self) -> bool:
+ """ If returns 'True', then also need to implement these:
+
+ get_default_source_directory()
+ get_default_basename()
+ """
+ raise NotImplementedError( 'Subclasses must override this method.' )
+
def get_default_source_directory(self):
# e.g., static image area
- raise NotImplementedError( 'Subclasses must override this method.' )
+ return None
def get_default_basename(self):
# Base filename for the default source (and destination) files.
- raise NotImplementedError( 'Subclasses must override this method.' )
+ return None
def get_media_destination_directory(self):
# Relative to MEDIA_ROOT.
@@ -91,11 +99,15 @@ class SvgFileForm(forms.Form):
)
if self._has_dangerous_svg_items:
require_svg_file = True
+ else:
+ require_svg_file = False
remove_dangerous_svg_items = cleaned_data.get( 'remove_dangerous_svg_items' )
svg_file_handle = cleaned_data.get('svg_file')
if not svg_file_handle:
+ if not self.allow_default_svg_file():
+ raise ValidationError( 'You need to select an SVG file.' )
if require_svg_file:
raise ValidationError( 'You need to re-select the SVG file.' )
diff --git a/src/hi/apps/edit/templates/edit/panes/side_header.html b/src/hi/apps/edit/templates/edit/panes/side_header.html
index 191e6157..0621c5cd 100644
--- a/src/hi/apps/edit/templates/edit/panes/side_header.html
+++ b/src/hi/apps/edit/templates/edit/panes/side_header.html
@@ -1,6 +1,15 @@
<div>
{% if request.view_parameters.view_type.is_location_view %}
- <div class="h3"><small>View:</small> {{ request.view_parameters.location_view.name }}</div>
+ <div class="d-flex justify-content-between">
+ <div class="h3"><small>View:</small> {{ request.view_parameters.location_view.name }}</div>
+ <div>
+ <a href="{% url 'location_details' location_view_id=request.view_parameters.view_type.location_view_id %}"
+ class="btn btn-tertiary" role="button" data-async="true">
+ Location/View Details
+ </a>
+
+ </div>
+ </div>
{% elif request.view_parameters.view_type.is_collection %}
<div class="h3"><small>Collection:</small> {{ request.view_parameters.collection.name }}</div>
{% endif %}
diff --git a/src/hi/apps/location/async_views.py b/src/hi/apps/location/async_views.py
index 7de9b484..787dd3e9 100644
--- a/src/hi/apps/location/async_views.py
+++ b/src/hi/apps/location/async_views.py
@@ -10,7 +10,6 @@ from hi.apps.entity.async_views import EntityDetailsView
from hi.apps.location.edit.forms import (
LocationAttributeFormset,
LocationEditForm,
- LocationSvgFileForm,
LocationViewEditForm,
)
@@ -59,7 +58,6 @@ class LocationViewDetailsView( HiSideView ):
'is_editable': True,
},
),
- 'location_svg_file_form': LocationSvgFileForm( ),
'location_view': location_view,
'location_view_edit_form': LocationViewEditForm( instance = location_view ),
}
diff --git a/src/hi/apps/location/edit/async_views.py b/src/hi/apps/location/edit/async_views.py
index 31ffdd36..5a629c11 100644
--- a/src/hi/apps/location/edit/async_views.py
+++ b/src/hi/apps/location/edit/async_views.py
@@ -2,6 +2,7 @@ import json
import logging
from django.core.exceptions import BadRequest
+from django.db import transaction
from django.http import Http404
from django.template.loader import get_template
from django.utils.decorators import method_decorator
@@ -15,7 +16,7 @@ from hi.apps.entity.edit.async_views import EntityPositionEditView
from hi.apps.entity.entity_manager import EntityManager
from hi.apps.entity.models import Entity
from hi.apps.location.location_manager import LocationManager
-from hi.apps.location.models import LocationView
+from hi.apps.location.models import Location, LocationView
from hi.constants import DIVID
from hi.decorators import edit_required
@@ -27,6 +28,48 @@ from . import forms
logger = logging.getLogger(__name__)
+@method_decorator( edit_required, name='dispatch' )
+class LocationEditView( View ):
+
+ def post( self, request, *args, **kwargs ):
+ location_id = kwargs.get('location_id')
+ try:
+ location = Location.objects.get( id = location_id )
+ except Location.DoesNotExist:
+ raise Http404( request )
+
+ location_edit_form = forms.LocationEditForm(
+ request.POST,
+ instance = location,
+ )
+ location_attribute_formset = forms.LocationAttributeFormset(
+ request.POST,
+ instance = location,
+ )
+
+ context = {
+ 'location': location,
+ 'location_edit_form': location_edit_form,
+ 'location_attribute_formset': location_attribute_formset,
+ }
+
+ if ( not location_edit_form.is_valid()
+ or not location_attribute_formset.is_valid() ):
+ 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,
+ },
+ )
+
+ with transaction.atomic():
+ location_edit_form.save()
+ location_attribute_formset.save()
+
+ return antinode.refresh_response()
+
+
@method_decorator( edit_required, name='dispatch' )
class LocationViewManageItemsView( HiSideView ):
@@ -67,13 +110,6 @@ class LocationViewReorder( View ):
return antinode.response( main_content = 'OK' )
-@method_decorator( edit_required, name='dispatch' )
-class LocationEditView( View ):
-
- def post( self, request, *args, **kwargs ):
- raise NotImplementedError()
-
-
@method_decorator( edit_required, name='dispatch' )
class LocationViewEditView( View ):
diff --git a/src/hi/apps/location/edit/forms.py b/src/hi/apps/location/edit/forms.py
index 8111ba2c..f6b9f92e 100644
--- a/src/hi/apps/location/edit/forms.py
+++ b/src/hi/apps/location/edit/forms.py
@@ -18,7 +18,7 @@ logger = logging.getLogger(__name__)
class LocationSvgFileForm( SvgFileForm ):
-
+
def get_default_source_directory(self):
return os.path.join(
settings.BASE_DIR,
@@ -43,18 +43,14 @@ class LocationAddForm( LocationSvgFileForm ):
required = True,
)
- def get_default_source_directory(self):
- return os.path.join(
- settings.BASE_DIR,
- 'static',
- 'img',
- )
+ def allow_default_svg_file(self):
+ return True
- def get_default_basename(self):
- return 'location-default.svg'
- def get_media_destination_directory(self):
- return 'location/svg'
+class LocationSvgReplaceForm( LocationSvgFileForm ):
+
+ def allow_default_svg_file(self):
+ return False
class LocationEditForm( forms.ModelForm ):
diff --git a/src/hi/apps/location/edit/urls.py b/src/hi/apps/location/edit/urls.py
index 3ff3fb58..bae784ac 100644
--- a/src/hi/apps/location/edit/urls.py
+++ b/src/hi/apps/location/edit/urls.py
@@ -14,6 +14,10 @@ urlpatterns = [
async_views.LocationEditView.as_view(),
name='location_edit_location_edit'),
+ re_path( r'^location/svg/replace/(?P<location_id>\d+)$',
+ views.LocationSvgReplaceView.as_view(),
+ name='location_edit_svg_replace'),
+
re_path( r'^location/delete/(?P<location_id>\d+)$',
views.LocationDeleteView.as_view(),
name='location_edit_location_delete' ),
diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.py
index 61e09923..d9642023 100644
--- a/src/hi/apps/location/edit/views.py
+++ b/src/hi/apps/location/edit/views.py
@@ -59,6 +59,57 @@ class LocationAddView( HiModalView ):
return antinode.redirect_response( redirect_url )
+@method_decorator( edit_required, name='dispatch' )
+class LocationSvgReplaceView( HiModalView ):
+
+ def get_template_name( self ) -> str:
+ return 'location/edit/modals/location_svg_replace.html'
+
+ def get( self, request, *args, **kwargs ):
+ location_id = kwargs.get( 'location_id' )
+ if not location_id:
+ raise BadRequest( 'Missing location id.' )
+ try:
+ location = Location.objects.get( id = location_id )
+ except Location.DoesNotExist:
+ raise Http404( request )
+ context = {
+ 'location': location,
+ 'location_svg_file_form': forms.LocationSvgReplaceForm(),
+ }
+ return self.modal_response( request, context )
+
+ def post( self, request, *args, **kwargs ):
+ location_id = kwargs.get( 'location_id' )
+ if not location_id:
+ raise BadRequest( 'Missing location id.' )
+ try:
+ location = Location.objects.get( id = location_id )
+ except Location.DoesNotExist:
+ raise Http404( request )
+
+ location_svg_file_form = forms.LocationSvgReplaceForm( request.POST, request.FILES )
+ if not location_svg_file_form.is_valid():
+ context = {
+ 'location': location,
+ 'location_svg_file_form': location_svg_file_form,
+ }
+ return self.modal_response( request, context )
+
+ try:
+ location = LocationManager().update_location_svg(
+ location = location,
+ svg_fragment_filename = location_svg_file_form.cleaned_data.get('svg_fragment_filename'),
+ svg_fragment_content = location_svg_file_form.cleaned_data.get('svg_fragment_content'),
+ svg_viewbox = location_svg_file_form.cleaned_data.get('svg_viewbox'),
+ )
+ except ValueError as ve:
+ raise BadRequest( str(ve) )
+
+ redirect_url = reverse('home')
+ return antinode.redirect_response( redirect_url )
+
+
@method_decorator( edit_required, name='dispatch' )
class LocationDeleteView( HiModalView ):
diff --git a/src/hi/apps/location/location_manager.py b/src/hi/apps/location/location_manager.py
index 8297c5b2..9e84b2fe 100644
--- a/src/hi/apps/location/location_manager.py
+++ b/src/hi/apps/location/location_manager.py
@@ -71,6 +71,22 @@ class LocationManager(Singleton):
return location
+ def update_location_svg( self,
+ location : Location,
+ svg_fragment_filename : str,
+ svg_fragment_content : str,
+ svg_viewbox : SvgViewBox ) -> LocationView:
+
+ self._ensure_directory_exists( svg_fragment_filename )
+ with default_storage.open( svg_fragment_filename, 'w') as destination:
+ destination.write( svg_fragment_content )
+
+ location.svg_fragment_filename = svg_fragment_filename
+ location.svg_view_box_str = str( svg_viewbox )
+ location.save()
+
+ return
+
def _ensure_directory_exists( self, filepath ):
if isinstance( default_storage, FileSystemStorage ):
directory = os.path.dirname( default_storage.path( filepath ))
diff --git a/src/hi/apps/location/templates/location/edit/modals/location_svg_replace.html b/src/hi/apps/location/templates/location/edit/modals/location_svg_replace.html
new file mode 100644
index 00000000..8eb029c4
--- /dev/null
+++ b/src/hi/apps/location/templates/location/edit/modals/location_svg_replace.html
@@ -0,0 +1,29 @@
+{% extends "modals/action_with_cancel.html" %}
+
+{% block preamble %}
+<form action="{% url 'location_edit_svg_replace' location_id=location.id %}"
+ method="post" enctype="multipart/form-data"
+ class="form" data-async="modal">
+ {% csrf_token %}
+{% endblock %}
+
+{% block title_text %}
+Replace Location Image
+{% endblock %}
+
+{% block body %}
+<div>
+{% include "location/edit/panes/location_svg_replace_form.html" %}
+</div>
+{% endblock %}
+
+{% block action_right %}
+<button id="hi-svg-replace-button" type="submit" class="btn btn-lg btn-success"
+ name="action" value="replace">
+ UPDATE
+</button>
+{% endblock %}
+
+{% block postamble %}
+</form>
+{% endblock %}
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_edit.html b/src/hi/apps/location/templates/location/edit/panes/location_edit.html
index 078f0c7c..f672e7ec 100644
--- a/src/hi/apps/location/templates/location/edit/panes/location_edit.html
+++ b/src/hi/apps/location/templates/location/edit/panes/location_edit.html
@@ -15,9 +15,13 @@
</form>
-
-<h5>Order Index</h5>
-<div>{{ location.order_id }}</div>
+<div>
+ <a href="{% url 'location_edit_svg_replace' location_id=location.id %}"
+ id="hi-svg-replace-link" class="btn btn-info btn-control" role="button"
+ data-async="true">
+ REPLACE IMAGE
+ </a>
+</div>
<div>
<a href="{% url 'location_edit_location_delete' location_id=location.id %}"
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_edit_form.html b/src/hi/apps/location/templates/location/edit/panes/location_edit_form.html
index 5db27e7d..e082d892 100644
--- a/src/hi/apps/location/templates/location/edit/panes/location_edit_form.html
+++ b/src/hi/apps/location/templates/location/edit/panes/location_edit_form.html
@@ -26,6 +26,3 @@
{% include "attribute/panes/attribute_form.html" with attribute_formset=location_attribute_formset %}
</div>
-{% if location_svg_file_form %}
-{% include "common/svg_file_form.html" with svg_file_form=location_svg_file_form label='Replace SVG file:' %}
-{% endif %}
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_svg_replace_form.html b/src/hi/apps/location/templates/location/edit/panes/location_svg_replace_form.html
new file mode 100644
index 00000000..a2e9ee92
--- /dev/null
+++ b/src/hi/apps/location/templates/location/edit/panes/location_svg_replace_form.html
@@ -0,0 +1,16 @@
+{% for hidden in location_svg_file_form.hidden_fields %}
+{{ hidden }}
+{% endfor %}
+
+{% if location_svg_file_form.non_field_errors %}
+<div class="non-field-errors">
+ {% for err in location_svg_file_form.non_field_errors %}
+ <p class="form-error text-danger">ERROR: {{ err }}</p>
+ {% endfor %}
+</div>
+{% endif %}
+
+<p>
+ Replace the background SVG image for this location.
+</p>
+{% include "common/svg_file_form.html" with svg_file_form=location_svg_file_form label='Select a replacement SVG file:' %}
diff --git a/src/hi/apps/location/urls.py b/src/hi/apps/location/urls.py
index acd45630..1355110a 100644
--- a/src/hi/apps/location/urls.py
+++ b/src/hi/apps/location/urls.py
@@ -10,6 +10,10 @@ urlpatterns = [
views.LocationSwitchView.as_view(),
name='location_switch'),
+ re_path( r'^details/(?P<location_view_id>\d*)$',
+ async_views.LocationViewDetailsView.as_view(),
+ name='location_details' ),
+
re_path( r'^view/(?P<id>\d+)$',
views.LocationViewView.as_view(),
name='location_view'),
@@ -22,10 +26,6 @@ urlpatterns = [
async_views.LocationItemDetailsView.as_view(),
name='location_item_details' ),
- re_path( r'^details/(?P<location_view_id>\d*)$',
- async_views.LocationViewDetailsView.as_view(),
- name='location_details' ),
-
re_path( r'^edit/', include('hi.apps.location.edit.urls' )),
]
diff --git a/src/hi/static/js/svg-location.js b/src/hi/static/js/svg-location.js
index 477c4b16..87a9033b 100644
--- a/src/hi/static/js/svg-location.js
+++ b/src/hi/static/js/svg-location.js
@@ -81,7 +81,7 @@
if ( gCurrentSelectionModule != 'svg-path' ) {
if ( Hi.DEBUG ) { console.log( `Mouse down event [${MODULE_NAME}]`, event ); }
createTransformData( event, event.target );
- handleLocationViewSvgClick( event, event.target );
+ gSelectedLocationViewSvg = event.target;
event.preventDefault();
event.stopImmediatePropagation();
return;
@@ -159,7 +159,7 @@
if ( $(event.target).hasClass( Hi.LOCATION_VIEW_SVG_CLASS ) ) {
if ( Hi.DEBUG ) { console.log( `Click [${MODULE_NAME}]`, event ); }
- handleLocationViewSvgClick( event, event.target );
+ gSelectedLocationViewSvg = event.target;
gSvgTransformData = null;
gSvgTransformType = SvgTransformType.MOVE;
event.preventDefault();
@@ -247,18 +247,6 @@
}
}
- function handleLocationViewSvgClick( event, locationViewSvg ) {
- gSelectedLocationViewSvg = locationViewSvg;
-
- if ( Hi.isEditMode ) {
- let data = {
- moduleName: MODULE_NAME,
- };
- Hi.edit.eventBus.emit( Hi.edit.SELECTION_MADE_EVENT_NAME, data );
- AN.get( `${Hi.API_LOCATION_DETAILS_URL}` );
- }
- }
-
function startDrag( event ) {
}