Skip to content

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.py
index 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 chosen
view_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_editing
and is_ajax( request )
and ( view_type_changed or view_id_changed )):
diff --git a/src/hi/exceptions.py b/src/hi/exceptions.py
index 3f7925f3..4c156e00 100644
--- a/src/hi/exceptions.py
+++ b/src/hi/exceptions.py
@@ -8,3 +8,7 @@ class ForceRedirectException( Exception ):
@property
def url(self):
return self._url
+
+
+class ForceSynchronousException( Exception ):
+ pass
diff --git a/src/hi/hi_grid_view.py b/src/hi/hi_grid_view.py
index 2de1fc88..c7c4e4ee 100644
--- a/src/hi/hi_grid_view.py
+++ b/src/hi/hi_grid_view.py
@@ -4,7 +4,7 @@ import urllib.parse
from django.shortcuts import redirect, render
from django.template.loader import get_template
-from django.urls import resolve, reverse
+from django.urls import resolve
from django.views.generic import View
import hi.apps.common.antinode as antinode
@@ -12,7 +12,6 @@ from hi.apps.common.utils import is_ajax
from hi.apps.location.edit.async_views import LocationViewManageItemsView
from hi.apps.collection.edit.async_views import CollectionManageItemsView
-from hi.apps.location.location_manager import LocationManager
from hi.apps.collection.models import Collection
from 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.py
new file mode 100644
index 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.py
index 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 Collection
from hi.apps.location.models import Location, LocationView
@@ -29,7 +31,7 @@ class ViewParameters:
self._location_view = None # Lazy loaded
self._collection = None # Lazy loaded
return
-
+
@property
def location_id(self) -> int:
location = self.location
@@ -93,7 +95,7 @@ class ViewParameters:
self._collection = collection
return
- def to_session( self, request ):
+ def to_session( self, request : HttpRequest ):
if not hasattr( request, 'session' ):
return
request.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' ):