Home Information - Your home, your data, organized.
Python
Added saving path edits to database.
Anthony Cassandra6dd32b2bbd17
diff --git a/src/hi/apps/edit/urls.py b/src/hi/apps/edit/urls.pyindex 0ee77ade..bd5c91e1 100644--- a/src/hi/apps/edit/urls.py+++ b/src/hi/apps/edit/urls.py@@ -25,6 +25,10 @@ urlpatterns = [views.EditSvgPositionView.as_view(),name='edit_svg_position' ),+ re_path( r'^svg/path/(?P<html_id>[\w\-]+)$',+ views.EditSvgPathView.as_view(),+ name='edit_svg_path' ),+re_path( r'^add-remove$',views.AddRemoveView.as_view(),name='edit_add_remove' ),diff --git a/src/hi/apps/edit/views.py b/src/hi/apps/edit/views.pyindex 3753b023..339141f1 100644--- a/src/hi/apps/edit/views.py+++ b/src/hi/apps/edit/views.py@@ -14,6 +14,7 @@ import hi.apps.common.antinode as antinodeimport hi.apps.collection.edit.views as collection_edit_viewsfrom hi.apps.collection.helpers import CollectionHelpersimport hi.apps.entity.edit.views as entity_edit_views+from hi.apps.entity.edit.helpers import EntityEditHelpersfrom hi.apps.entity.helpers import EntityHelpersimport hi.apps.location.edit.views as location_edit_viewsfrom hi.apps.location.forms import SvgPositionForm@@ -161,7 +162,7 @@ class EditSvgPositionView( View, EditViewMixin ):location = location_view.location,)else:- raise NotImplementedError( 'Not yet handling unknown edit detail type.' )+ return bad_request_response( request, message = f'Cannot set SVG position for "{item_type}"' )svg_position_form = SvgPositionForm(request.POST,@@ -192,6 +193,31 @@ class EditSvgPositionView( View, EditViewMixin ):)+@method_decorator( edit_required, name='dispatch' )+class EditSvgPathView( View, EditViewMixin ):++ def post(self, request, *args, **kwargs):++ ( item_type, item_id ) = self.parse_html_id( kwargs.get('html_id'))+ svg_path_str = request.POST.get('svg_path')+ if not svg_path_str:+ return bad_request_response( request, message = 'Missing SVG path' );++ location_view = request.view_parameters.location_view+ if item_type == 'entity':+ EntityEditHelpers.set_entity_path(+ entity_id = item_id,+ location = location_view.location,+ svg_path_str = svg_path_str,+ )+ else:+ return bad_request_response( request, message = f'Cannot set SVG path for "{item_type}"' )++ return antinode.response(+ main_content = 'OK',+ )++@method_decorator( edit_required, name='dispatch' )class AddRemoveView( View, EditViewMixin ):diff --git a/src/hi/apps/entity/edit/helpers.py b/src/hi/apps/entity/edit/helpers.pyindex 4d2947d3..ec678338 100644--- a/src/hi/apps/entity/edit/helpers.py+++ b/src/hi/apps/entity/edit/helpers.py@@ -1,8 +1,9 @@from django.http import HttpRequestfrom hi.apps.entity.enums import EntityType-from hi.apps.entity.models import Entity+from hi.apps.entity.models import Entity, EntityPathfrom hi.integrations.core.enums import IntegrationType+from hi.apps.location.models import Locationclass EntityEditHelpers:@@ -18,3 +19,29 @@ class EntityEditHelpers:integration_type_str = str(IntegrationType.NONE),integration_key = None,)++ @classmethod+ def set_entity_path( cls,+ entity_id : int,+ location : Location,+ svg_path_str : str ) -> EntityPath:++ try:+ entity_path = EntityPath.objects.get(+ location = location,+ entity_id = entity_id,+ )+ entity_path.svg_path = svg_path_str+ entity_path.save()+ return entity_path++ except EntityPath.DoesNotExist:+ pass++ entity = Entity.objects.get( id = entity_id )+ return EntityPath.objects.create(+ entity = entity,+ location = location,+ svg_path = svg_path_str,+ )+diff --git a/src/hi/static/js/edit.js b/src/hi/static/js/edit.jsindex 3271c0d1..c4434275 100644--- a/src/hi/static/js/edit.js+++ b/src/hi/static/js/edit.js@@ -30,6 +30,7 @@const API_REORDER_ITEMS_URL = '/edit/reorder-items';const API_EDIT_DETAILS_URL = '/edit/details';const API_EDIT_SVG_POSITION_URL = '/edit/svg/position';+ const API_EDIT_SVG_PATH_URL = '/edit/svg/path';const ICON_ACTION_STATE_ATTR_NAME = 'action-state';const ICON_ACTION_SCALE_KEY = 's';@@ -450,7 +451,7 @@function clearSelectedSvgPath() {if ( gSelectedPathSvgGroup ) {- collapseSvgPath( gSelectedPathSvgGroup );+ collapseSvgPath( );gSelectedPathSvgGroup = null;}}@@ -1082,6 +1083,7 @@if ( newProxyPoint ) {setSelectedProxyElement( newProxyPoint );+ saveSvgPath();}}@@ -1242,6 +1244,8 @@$(svgProxyPoint).remove();setSelectedProxyElement( null );}++ saveSvgPath();}function deleteProxyLine( svgProxyLine ) {@@ -1270,6 +1274,7 @@y: ( beforeY + afterY ) / 2};insertNewProxyPoint( midSvgPoint, svgProxyLine );+ saveSvgPath();}function addProxyPath( ) {@@ -1377,6 +1382,7 @@} else {console.log( `Unknown proxy path type: ${proxyPathType}` );}+ saveSvgPath();}function createProxyPathGroup( proxyPathType ) {@@ -1460,6 +1466,7 @@// Function to handle mouse up (end of drag)function onMouseUp( event ) {event.preventDefault();+ saveSvgPath();gSvgPathEditData.dragProxyPoint = null;$(document).off('mousemove', onMouseMove);$(document).off('mouseup', onMouseUp);@@ -1471,46 +1478,59 @@});}- function collapseSvgPath( pathSvgGroup ) {- if ( DEBUG ) { console.log( 'Collapse SVG Path', pathSvgGroup ); }+ function collapseSvgPath( ) {+ if ( DEBUG ) { console.log( 'Collapse SVG Path' ); }-+ let newSvgPath = getSvgPathStringFromProxyPaths();+ $(gSelectedPathSvgGroup).find('path').attr( 'd', newSvgPath );- /*- const svg = document.getElementById('my-svg');- const lines = svg.querySelectorAll('line');- let newPathData = '';-- // Loop through lines and recombine them into a single path- lines.forEach(line => {- const x1 = line.getAttribute('x1');- const y1 = line.getAttribute('y1');- const x2 = line.getAttribute('x2');- const y2 = line.getAttribute('y2');- Newpathdata += `M ${x1} ${y1} L ${x2} ${y2} `;- svg.removeChild(line); // Remove each line after processing- });+ const proxyPathContainer = $( '#' + PROXY_PATH_CONTAINER_ID );+ $(proxyPathContainer).remove();- proxyPoints.forEach(point => svg.removeChild(point)); // Remove proxy points+ gSelectedPathSvgGroup.show();- if (selectedPath) {- selectedPath.setAttribute('d', newPathData.trim()); // Set the new path data- selectedPath.style.display = ''; // Make the path visible again+ gSvgPathEditData = null;}-*/-+ function saveSvgPath() {+ if ( ! gSelectedPathSvgGroup ) {+ return;+ }- // ZZZ error if less than 2 points+ let svgItemId = $(gSelectedPathSvgGroup).attr('id');+ let svgPathString = getSvgPathStringFromProxyPaths();+ let data = {+ svg_path: svgPathString+ };+ AN.post( `${API_EDIT_SVG_PATH_URL}/${svgItemId}`, data );+ }++ function getSvgPathStringFromProxyPaths() {+ const proxyPathContainer = $( '#' + PROXY_PATH_CONTAINER_ID );+ const proxyPathGroups = $(proxyPathContainer).find( PROXY_PATH_GROUP_SELECTOR );- pathSvgGroup.show();+ let pathString = '';+ $(proxyPathGroups).each(function( index, proxyPathGroup ) {+ let proxyPoints = $(proxyPathGroup).find( PROXY_POINT_SELECTOR );+ $(proxyPoints).each(function( index, proxyPoint ) {+ if ( index == 0 ) {+ pathString += ' M ';+ } else {+ pathString += ' L ';+ }+ pathString += proxyPoint.getAttribute('cx') + ',' + proxyPoint.getAttribute('cy');+ });+ if ( $(proxyPathGroup).attr( PROXY_PATH_TYPE_ATTR ) == ProxyPathType.CLOSED ) {+ pathString += ' Z';+ }+ });- gSvgPathEditData = null;+ console.log( `PATH = ${pathString}` );+ return pathString;}--+function getReferenceElementForExtendingProxyPath( ) {if ( gSvgPathEditData.selectedProxyElement ) {return gSvgPathEditData.selectedProxyElement;