Home Information - Your home, your data, organized.
Python
Working on adding new Location instances. WIP
Anthony Cassandra4987eb401834
diff --git a/src/hi/apps/location/edit/forms.py b/src/hi/apps/location/edit/forms.pyindex 09c5a89a..5726561d 100644--- a/src/hi/apps/location/edit/forms.py+++ b/src/hi/apps/location/edit/forms.py@@ -1,6 +1,13 @@from django import forms+class LocationForm(forms.Form):++ name = forms.CharField()++ svg_file = forms.FileField( label = 'Select an SVG file' )++class LocationViewForm(forms.Form):name = forms.CharField()diff --git a/src/hi/apps/location/edit/helpers.py b/src/hi/apps/location/edit/helpers.pyindex 0776295a..09517f49 100644--- a/src/hi/apps/location/edit/helpers.py+++ b/src/hi/apps/location/edit/helpers.py@@ -7,10 +7,9 @@ from django.http import HttpRequestfrom hi.apps.collection.collection_manager import CollectionManagerfrom hi.apps.collection.models import (Collection,- CollectionPath,- CollectionPosition,CollectionView,)+from hi.apps.common.svg_models import SvgViewBoxfrom hi.apps.entity.delegation_manager import DelegationManagerfrom hi.apps.entity.entity_manager import EntityManagerfrom hi.apps.entity.models import (@@ -30,6 +29,30 @@ from .transient_models import (class LocationEditHelpers:+ @classmethod+ def create_location( cls,+ request : HttpRequest,+ name : str,+ svg_filename : str,+ svg_view_box : SvgViewBox++ ) -> LocationView:++ location = cls.get_location_for_new_location_view( request )+ if not location:+ raise ValueError( 'No locations defined.' )++ last_location_view = location.views.order_by( '-order_id' ).first()++ return LocationView.objects.create(+ location = location,+ location_view_type_str = LocationViewType.default(),+ name = name,+ svg_view_box_str = str( location.svg_view_box ),+ svg_rotate = Decimal( 0.0 ),+ order_id = last_location_view.order_id + 1,+ )+@classmethoddef create_location_view( cls,request : HttpRequest,diff --git a/src/hi/apps/location/edit/urls.py b/src/hi/apps/location/edit/urls.pyindex a905be0f..67885a02 100644--- a/src/hi/apps/location/edit/urls.py+++ b/src/hi/apps/location/edit/urls.py@@ -5,6 +5,10 @@ from . import viewsurlpatterns = [+ re_path( r'^location/add$',+ views.LocationAddView.as_view(),+ name='location_edit_location_add'),+re_path( r'^location-view/add$',views.LocationViewAddView.as_view(),name='location_edit_location_view_add' ),diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.pyindex 3ded929d..ae45909b 100644--- a/src/hi/apps/location/edit/views.py+++ b/src/hi/apps/location/edit/views.py@@ -34,6 +34,89 @@ from .helpers import LocationEditHelperslogger = logging.getLogger(__name__)+@method_decorator( edit_required, name='dispatch' )+class LocationAddView( View ):++ def get( self, request, *args, **kwargs ):+ return self._show_modal( request, location_form = forms.LocationForm() )++ def post( self, request, *args, **kwargs ):+ location_form = forms.LocationForm( request.POST, request.FILES )+ if not location_form.is_valid():+ return self._show_modal( request, location_form = location_form )++ svg_file = request.FILES['svg_file']+ try:+ svg_file_path = self._handle_uploaded_svg_file( svg_file )++ except ValueError as e:+ location_form.add_error( 'svg_file', str(e) )+ return self._show_modal( request, location_form = location_form )++ try:+ location = LocationEditHelpers.create_location(+ request = request,+ name = location_form.cleaned_data.get('name'),+ )+ except ValueError as e:+ return bad_request_response( request, message = str(e) )+++++++ location_view = create_all_zzz+++++ request.view_parameters.view_type = ViewType.LOCATION_VIEW+ request.view_parameters.location_view_id = location_view.id+ request.view_parameters.to_session( request )++ redirect_url = reverse('home')+ return redirect( redirect_url )++ def _show_modal( self, request, location_form : forms.LocationForm ):+ context = {+ 'location_form': location_form,+ }+ return antinode.modal_from_template(+ request = request,+ template_name = 'location/edit/modals/location_add.html',+ context = context,+ )++ def _handle_uploaded_svg_file( self, svg_file ):++ if svg_file.size > 1024 * 1024 * 5: # Example: Limit file size to 5 MB+ raise ValueError("File is too large")++ for chunk in svg_file.chunks():+ # Process each chunk of the file (optional)+ pass+++++++ return svg_file.name+++++++ # Save the file to MEDIA_ROOT after processing+ file_path = os.path.join( settings.MEDIA_ROOT, svg_file.name )+ with default_storage.open( file_path, 'wb+' ) as destination:+ for chunk in svg_file.chunks():+ destination.write(chunk)+ return file_path++@method_decorator( edit_required, name='dispatch' )class LocationViewDetailsView( View ):@@ -75,10 +158,10 @@ class LocationViewAddView( View ):)def post( self, request, *args, **kwargs ):- name_form = forms.LocationViewForm( request.POST )- if not name_form.is_valid():+ location_view_form = forms.LocationViewForm( request.POST )+ if not location_view_form.is_valid():context = {- 'location_view_form': name_form,+ 'location_view_form': location_view_form,}return antinode.modal_from_template(request = request,@@ -89,7 +172,7 @@ class LocationViewAddView( View ):try:location_view = LocationEditHelpers.create_location_view(request = request,- name = name_form.cleaned_data.get('name'),+ name = location_view_form.cleaned_data.get('name'),)except ValueError as e:return bad_request_response( request, message = str(e) )diff --git a/src/hi/apps/location/templates/location/edit/modals/location_add.html b/src/hi/apps/location/templates/location/edit/modals/location_add.htmlnew file mode 100644index 00000000..c37343f1--- /dev/null+++ b/src/hi/apps/location/templates/location/edit/modals/location_add.html@@ -0,0 +1,28 @@+{% extends "modals/action_with_cancel.html" %}++{% block preamble %}+<form action="{% url 'location_edit_location_add' %}" method="post"+ class="form" data-async="modal">+ {% csrf_token %}+{% endblock %}++{% block title_text %}+New Location+{% endblock %}++{% block body %}+<div>+{% include "location/edit/panes/location_form.html" %}+</div>+{% endblock %}++{% block action_right %}+<button id="hi-add-location-button" type="submit" class="btn btn-lg btn-success"+ name="action" value="add">+ ADD+</button>+{% endblock %}++{% block postamble %}+</form>+{% endblock %}diff --git a/src/hi/apps/location/templates/location/edit/panes/location_form.html b/src/hi/apps/location/templates/location/edit/panes/location_form.htmlnew file mode 100644index 00000000..3c39e31c--- /dev/null+++ b/src/hi/apps/location/templates/location/edit/panes/location_form.html@@ -0,0 +1,22 @@+{% for hidden in location_form.hidden_fields %}+{{ hidden }}+{% endfor %}++{% if location_form.non_field_errors %}+<div class="non-field-errors">+ {% for err in location_form.non_field_errors %}+ <p class="form-error text-danger">ERROR: {{ err }}</p>+ {% endfor %}+</div>+{% endif %}++<div>+ {{ location_form.name.label }}+ {{ location_form.name }}+ {{ location_form.name.errors }}+</div>++<div>+ <label for="id_svg_file">Select an SVG file:</label>+ <input type="file" name="svg_file" required="" id="id_svg_file">+</div>diff --git a/src/hi/apps/location/templates/location/location_drop_down.html b/src/hi/apps/location/templates/location/location_drop_down.htmlnew file mode 100644index 00000000..cb80901e--- /dev/null+++ b/src/hi/apps/location/templates/location/location_drop_down.html@@ -0,0 +1,33 @@+<div>+ <div class="dropdown">+ <button class="btn btn-secondary btn-control dropdown-toggle" type="button"+ id="hi-location-menu" data-toggle="dropdown">+ {{ current_location.name }}+ <span class="caret"></span>+ </button>+ <div class="dropdown-menu"+ role="menu" aria-labelledby="hi-location-menu">+ {% for location in location_list %}+ {% if forloop.counter > 1 %}+ <div class="dropdown-divider"></div>+ {% endif %}+ <a role="menuitem" class="dropdown-item plain"+ href="{% url 'location_switch' location_id=location.id %} ">+ {{ location.name }}+ </a>+ {% empty %}+ <div class="dropdown-item alert alert-secondary text-center">+ <em>No locations.</em>+ </div>+ {% endfor %}+ {% if request.is_editing %}+ <div class="dropdown-divider"></div>+ <a role="menuitem" class="dropdown-item plain"+ href="{% url 'location_edit_location_add' %}"+ data-async="modal">+ ADD NEW LOCATION+ </a>+ {% endif %}+ </div>+ </div>+</div>diff --git a/src/hi/apps/location/templates/location/location_view.html b/src/hi/apps/location/templates/location/location_view.htmlindex 145c5a1d..b50d6337 100644--- a/src/hi/apps/location/templates/location/location_view.html+++ b/src/hi/apps/location/templates/location/location_view.html@@ -8,8 +8,10 @@extents="{{ location_view_data.location_view.location.svg_view_box }}"width="100%" height="100%" preserveAspectRatio="xMidYMid meet"transform="rotate({{ location_view_data.location_view.svg_rotate }} 0 0)">- {% include_media_template location_view_data.location_view.location.svg_filename %}-+ <g class="hi-location-view-base">+ {% include_media_template location_view_data.location_view.location.svg_filename %}+ </g>+{% for svg_path_item in location_view_data.svg_path_items %}{% include "location/svg_path_item.html" with svg_path_item=svg_path_item %}{% endfor %}diff --git a/src/hi/apps/location/urls.py b/src/hi/apps/location/urls.pyindex 7258c292..7e2a8ed1 100644--- a/src/hi/apps/location/urls.py+++ b/src/hi/apps/location/urls.py@@ -5,6 +5,10 @@ from . import viewsurlpatterns = [+ re_path( r'^switch/(?P<location_id>\d+)$',+ views.LocationSwitchView.as_view(),+ name='location_switch'),+re_path( r'^view/(?P<id>\d+)$',views.LocationViewView.as_view(),name='location_view'),diff --git a/src/hi/apps/location/views.py b/src/hi/apps/location/views.pyindex d5868c7d..9981bcb3 100644--- a/src/hi/apps/location/views.py+++ b/src/hi/apps/location/views.py@@ -16,6 +16,10 @@ from .models import Location, LocationViewlogger = logging.getLogger(__name__)+class LocationSwitchView( View ):+ pass++class LocationViewDefaultView( View ):def get(self, request, *args, **kwargs):diff --git a/src/hi/hi_grid_view.py b/src/hi/hi_grid_view.pyindex 12e21066..eb4f4ffa 100644--- a/src/hi/hi_grid_view.py+++ b/src/hi/hi_grid_view.py@@ -2,7 +2,7 @@ import loggingfrom typing import Dictfrom django.http import HttpRequest-from django.shortcuts import render+from django.shortcuts import redirect, renderfrom django.template import Contextfrom django.template.loader import get_templatefrom django.urls import NoReverseMatch@@ -12,6 +12,7 @@ from django.views.generic import Viewimport hi.apps.common.antinode as antinodefrom hi.apps.common.utils import is_ajaxfrom hi.apps.collection.models import Collection+from hi.apps.location.models import Locationfrom hi.constants import DIVID@@ -47,6 +48,14 @@ class HiGridView(View):if not context:context = dict()+ current_location = request.view_parameters.location+ if not current_location:+ redirect_url = reverse('start')+ if is_ajax( request ):+ antinode.redirect( redirect_url )+ else:+ return redirect( redirect_url )+if not is_ajax( request ):# For full syncronous render, we need all content, so we fill# in defaults for any missing.@@ -67,15 +76,16 @@ class HiGridView(View):# This list of views is needed for top buttonsif ( 'location_view_list' not in context ):- location = request.view_parameters.location_view.location- location_view_list = list( location.views.order_by( 'order_id' ))+ location_view_list = list( current_location.views.order_by( 'order_id' ))context['location_view_list'] = location_view_list# This list of collections is needed for bottom buttonsif 'collection_list' not in context:collection_list = list( Collection.objects.all().order_by( 'order_id' ))context['collection_list'] = collection_list-++ context['current_location'] = current_location+ context['location_list'] = list( Location.objects.all() )return render( request, self.HI_GRID_TEMPLATE_NAME, context )div_id_to_template_name = {diff --git a/src/hi/templates/pages/start.html b/src/hi/templates/pages/start.htmlnew file mode 100644index 00000000..98e43387--- /dev/null+++ b/src/hi/templates/pages/start.html@@ -0,0 +1,14 @@+{% extends "pages/base.html" %}++{% block content %}+<h1>Start Page</h1>++<div>+ <a role="button" class="btn btn-success"+ href="{% url 'location_edit_location_add' %}"+ data-async="modal">+ ADD NEW LOCATION+ </a>+</div>++{% endblock %}diff --git a/src/hi/templates/panes/top_buttons.html b/src/hi/templates/panes/top_buttons.htmlindex 31bae1fe..874a3930 100644--- a/src/hi/templates/panes/top_buttons.html+++ b/src/hi/templates/panes/top_buttons.html@@ -1,22 +1,23 @@<div class="bg-white p-1"><div class="d-flex justify-content-between">{% include "location/location_view_buttons.html" %}- {% if request.is_editing %}- <div class="">- <a href="{% url 'edit_end' %}"- class="btn btn-tertiary btn-control" role="button" >- EXIT EDITING- </a>+ <div class="d-flex justify-content-end">+ {% include "location/location_drop_down.html" %}+ <div>+ {% if request.is_editing %}+ <a href="{% url 'edit_end' %}"+ class="btn btn-tertiary btn-control" role="button" >+ EXIT EDITING+ </a>+ {% else %}+ {% if request.view_parameters.view_type.allows_edit_mode %}+ <a href="{% url 'edit_start' %}"+ class="btn btn-tertiary btn-control" role="button" >+ EDIT+ </a>+ {% endif %}+ {% endif %}+ </div></div>- {% else %}- {% if request.view_parameters.view_type.allows_edit_mode %}- <div>- <a href="{% url 'edit_start' %}"- class="btn btn-tertiary btn-control" role="button" >- EDIT- </a>- </div>- {% endif %}- {% endif %}</div></div>diff --git a/src/hi/urls.py b/src/hi/urls.pyindex 19d68605..011f987b 100644--- a/src/hi/urls.py+++ b/src/hi/urls.py@@ -25,6 +25,7 @@ urlpatterns = [re_path( r'^$', views.HomeView.as_view(), name='home' ),re_path( r'^index.html$', views.HomeView.as_view(), name='home_index' ),+ re_path( r'^start$', views.StartView.as_view(), name='start' ),re_path( r'^api/', include('hi.apps.api.urls' )),re_path( r'^config/', include('hi.apps.config.urls' )),diff --git a/src/hi/view_parameters.py b/src/hi/view_parameters.pyindex 4554c6b0..911a6fdb 100644--- a/src/hi/view_parameters.py+++ b/src/hi/view_parameters.py@@ -17,18 +17,29 @@ class ViewParameters:view_type : ViewType = Noneview_mode : ViewMode = None- location_view_id : int = None- collection_id : int = None+ location_view_id : int = None # Last LocationView viewed+ collection_id : int = None # Last Collection vieweddef __post_init__(self):if self.view_type is None:self.view_type = ViewType.default()if self.view_mode is None:self.view_mode = ViewMode.default()+ self._location = None # Lazy loadedself._location_view = None # Lazy loadedself._collection = None # Lazy loadedreturn+ @property+ def location(self):+ if self._location:+ return self._location+ location_view = self.location_view+ if not location_view:+ return None+ self._location = location_view.location+ return self._location+@propertydef location_view(self):if self._location_view:diff --git a/src/hi/views.py b/src/hi/views.pyindex 6971dbe5..bc62e4ec 100644--- a/src/hi/views.py+++ b/src/hi/views.py@@ -8,6 +8,8 @@ from django.views.generic import Viewimport hi.apps.common.antinode as antinodefrom hi.apps.common.utils import is_ajax+from hi.apps.location.edit.forms import LocationForm+from hi.apps.location.models import Locationdef error_response( request : HttpRequest,@@ -135,8 +137,32 @@ class HomeView( View ):def get(self, request, *args, **kwargs):- if request.view_parameters.view_type.is_collection:+ current_location = request.view_parameters.location+ if not current_location:+ redirect_url = reverse('start')+ elif request.view_parameters.view_type.is_collection:redirect_url = reverse( 'collection_view_default' )else:redirect_url = reverse( 'location_view_default' )return HttpResponseRedirect( redirect_url )+++class StartView( View ):++ def get(self, request, *args, **kwargs):++++ # ZZZ TODO: REMOVE ME+ if False:+ if Location.objects.all().exists():+ return HomeView().get( request )++++++ context = {+ 'location_form': LocationForm(),+ }+ return render( request, 'pages/start.html', context )