Home Information - Your home, your data, organized.
Python
Emergency commit...in middle of force sync view logic.
Anthony Cassandra9390a6769604
diff --git a/src/hi/apps/location/views.py b/src/hi/apps/location/views.pyindex 144471f3..2ac354e9 100644--- a/src/hi/apps/location/views.py+++ b/src/hi/apps/location/views.py@@ -23,27 +23,19 @@ class LocationViewDefaultView( View ):def get(self, request, *args, **kwargs):try:- location_view = self._get_default_location_view( request )+ location_view = LocationManager().get_default_location_view( request = request )+ request.view_parameters.view_type = ViewType.LOCATION_VIEW+ request.view_parameters.update_location_view( location_view )+ request.view_parameters.to_session( request )redirect_url = reverse('location_view',kwargs = { 'location_view_id': location_view.id })- except Location.DoesNotExist:+ except LocationView.DoesNotExist:redirect_url = reverse( 'start' )return HttpResponseRedirect( redirect_url )- def _get_default_location_view( self, request ):- try:- location_view = LocationManager().get_default_location_view( request = request )- except LocationView.DoesNotExist:- raise BadRequest( 'No views and no locations defined.' )-- request.view_parameters.view_type = ViewType.LOCATION_VIEW- request.view_parameters.update_location_view( location_view )- request.view_parameters.to_session( request )- return location_view-class LocationViewView( HiGridView, LocationViewMixin ):@@ -53,6 +45,13 @@ class LocationViewView( HiGridView, LocationViewMixin ):def get_template_context( self, request, *args, **kwargs ):location_view = self.get_location_view( request, *args, **kwargs )++ try:+ zzz+ except ForceSynchronousException+++# Remember last location view chosenview_type_changed = bool( request.view_parameters.view_type != ViewType.LOCATION_VIEW )view_id_changed = bool( request.view_parameters.location_view_id != location_view.id )@@ -61,11 +60,6 @@ class LocationViewView( HiGridView, LocationViewMixin ):request.view_parameters.update_location_view( location_view )request.view_parameters.to_session( request )- # When in edit mode, a location view change needs a full- # synchronous page load to ensure any front-end editing state and- # views are invalidated. Else, the editing state and edit side- # panel will be invalid for the new view.- #if ( request.is_editingand is_ajax( request )and ( view_type_changed or view_id_changed )):diff --git a/src/hi/exceptions.py b/src/hi/exceptions.pyindex 3f7925f3..4c156e00 100644--- a/src/hi/exceptions.py+++ b/src/hi/exceptions.py@@ -8,3 +8,7 @@ class ForceRedirectException( Exception ):@propertydef url(self):return self._url+++class ForceSynchronousException( Exception ):+ passdiff --git a/src/hi/hi_grid_view.py b/src/hi/hi_grid_view.pyindex 2de1fc88..c7c4e4ee 100644--- a/src/hi/hi_grid_view.py+++ b/src/hi/hi_grid_view.py@@ -4,7 +4,7 @@ import urllib.parsefrom django.shortcuts import redirect, renderfrom django.template.loader import get_template-from django.urls import resolve, reverse+from django.urls import resolvefrom django.views.generic import Viewimport hi.apps.common.antinode as antinode@@ -12,7 +12,6 @@ from hi.apps.common.utils import is_ajaxfrom hi.apps.location.edit.async_views import LocationViewManageItemsViewfrom hi.apps.collection.edit.async_views import CollectionManageItemsView-from hi.apps.location.location_manager import LocationManagerfrom hi.apps.collection.models import Collectionfrom hi.apps.location.models import Location@@ -76,14 +75,6 @@ class HiGridView(View):def get(self, request, *args, **kwargs):- current_location = LocationManager().get_default_location( request = request )- if not current_location:- redirect_url = reverse('start')- if is_ajax( request ):- return antinode.redirect( redirect_url )- else:- return redirect( redirect_url )-if is_ajax( request ):return self.get_async_response( request, *args, **kwargs )@@ -113,6 +104,9 @@ class HiGridView(View):main_content = self.get_content( request, *args, **kwargs )except ForceRedirectException as fde:return antinode.redirect_response( url = fde.url )+ except ForceSynchronousException as fse:+ redirect_url = request.get_full_path()+ return antinode.redirect_response( redirect_url )insert_map = { DIVID['MAIN']: main_content }push_url = self.get_push_url( request )diff --git a/src/hi/view_mixin.py b/src/hi/view_mixin.pynew file mode 100644index 00000000..b892eaca--- /dev/null+++ b/src/hi/view_mixin.py@@ -0,0 +1,47 @@+from django.http import HttpRequest++from hi.apps.common.utils import is_ajax++from hi.enums import ViewType+from hi.exceptions import ForceSynchronousException+++class HiViewMixin:++ def should_force_sync_request( self,+ request : HttpRequest,+ next_view_type : ViewType,+ next_id : int ):+ """ Helper method for HiGridView subclases to know if they should force+ a full page refresh (turning an async request into a sync request. """+++ # When in edit mode, a location view change needs a full+ # synchronous page load to ensure any front-end editing state and+ # views are invalidated. Else, the editing state and edit side+ # panel will be invalid for the new view.+ #++ if not is_ajax( request ):+ return False+ view_type_changed = bool( request.view_parameters.view_type != next_view_type )++ if ( view_type_changed+ and (( request.view_parameters.view_type == ViewType.CONFIGURATION )+ or ( next_view_type == ViewType.CONFIGURATION ))):+ return True++ if next_view_type == ViewType.LOCATION_VIEW:+ view_id_changed = bool( request.view_parameters.location_view_id != next_id )+ return bool( request.is_editing+ and ( view_type_changed or view_id_changed ))++ elif next_view_type == ViewType.COLLECTION:+ view_id_changed = bool( request.view_parameters.collection_id != next_id )+ return bool( request.is_editing+ and ( view_type_changed or view_id_changed ))++ return False+++diff --git a/src/hi/view_parameters.py b/src/hi/view_parameters.pyindex 90ea619a..8fe40a3b 100644--- a/src/hi/view_parameters.py+++ b/src/hi/view_parameters.py@@ -1,5 +1,7 @@from dataclasses import dataclass+from django.http import HttpRequest+from hi.apps.collection.models import Collectionfrom hi.apps.location.models import Location, LocationView@@ -29,7 +31,7 @@ class ViewParameters:self._location_view = None # Lazy loadedself._collection = None # Lazy loadedreturn-+@propertydef location_id(self) -> int:location = self.location@@ -93,7 +95,7 @@ class ViewParameters:self._collection = collectionreturn- def to_session( self, request ):+ def to_session( self, request : HttpRequest ):if not hasattr( request, 'session' ):returnrequest.session['view_type'] = str(self.view_type)@@ -103,7 +105,7 @@ class ViewParameters:return@staticmethod- def from_session( request ):+ def from_session( request : HttpRequest ):if not request:return ViewParameters()if not hasattr( request, 'session' ):