Skip to content

Home Information - Your home, your data, organized.

Python

Got forced sync view logic in stable state. WIP

Anthony Cassandra4887e4288b0f
diff --git a/src/hi/apps/collection/views.py b/src/hi/apps/collection/views.py
index 6cd3ee6c..1e50b735 100644
--- a/src/hi/apps/collection/views.py
+++ b/src/hi/apps/collection/views.py
@@ -7,7 +7,7 @@ from django.views.generic import View
from hi.apps.common.utils import is_ajax
from hi.enums import ViewType
-from hi.exceptions import ForceRedirectException
+from hi.exceptions import ForceSynchronousException
from hi.hi_grid_view import HiGridView
from .collection_manager import CollectionManager
@@ -48,25 +48,16 @@ class CollectionViewView( HiGridView, CollectionViewMixin ):
def get_template_context( self, request, *args, **kwargs ):
collection = self.get_collection( request, *args, **kwargs )
- # Remember last collection chosen
- view_type_changed = bool( request.view_parameters.view_type != ViewType.COLLECTION )
- view_id_changed = bool( request.view_parameters.collection_id != collection.id )
+ if self.should_force_sync_request(
+ request = request,
+ next_view_type = ViewType.COLLECTION,
+ next_id = collection.id ):
+ raise ForceSynchronousException()
request.view_parameters.view_type = ViewType.COLLECTION
request.view_parameters.update_collection( collection )
request.view_parameters.to_session( request )
- # When in edit mode, a collection 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 )):
- redirect_url = reverse( 'collection_view', kwargs = kwargs )
- raise ForceRedirectException( url = redirect_url )
-
collection_data = CollectionManager().get_collection_data(
collection = collection,
)
diff --git a/src/hi/apps/config/enums.py b/src/hi/apps/config/enums.py
index 994c5bf3..ef1e2528 100644
--- a/src/hi/apps/config/enums.py
+++ b/src/hi/apps/config/enums.py
@@ -2,8 +2,11 @@ from hi.apps.common.enums import LabeledEnum
class ConfigPageType(LabeledEnum):
- # Defines those features that appear on the main config (admin) home
- # page and links to their content.
+ """
+ Defines those features that appear on the main config (admin) home
+ page and links to their content. Each tab in the config pane will
+ have an enum entry.
+ """
def __init__( self,
label : str,
diff --git a/src/hi/apps/config/views.py b/src/hi/apps/config/views.py
index 0085abbb..cd934627 100644
--- a/src/hi/apps/config/views.py
+++ b/src/hi/apps/config/views.py
@@ -1,5 +1,4 @@
import hi.apps.common.antinode as antinode
-from hi.apps.common.utils import is_ajax
from hi.enums import ViewMode, ViewType
from hi.hi_grid_view import HiGridView
@@ -9,18 +8,20 @@ from .enums import ConfigPageType
class ConfigPageView( HiGridView ):
"""
- The app's config/admin page is shown as a tabbed pane with one tab for
- each separate configuration concern. We want them to share some
- standard state tracking and consistent page rendering. However, we
- also want the different config areas to remain somewhat independent.
+ The app's config/admin page is shown in the main area of the HiGridView
+ layout. It is a tabbed pane with one tab for each separate
+ configuration concern. We want them to share some standard state
+ tracking and consistent page rendering for each individual
+ configuration concern. However, we also want the different config
+ areas to remain somewhat independent.
We do this with these:
- - ConfigPageType (enum) - An entry for each configuration concenr,
- coupled only by the URL name for its mian/entry page.
+ - ConfigPageType (enum) - An entry for each configuration concern,
+ coupled only by the URL name for its main/entry page.
- - ConfigPageView (this view) - Each section (tab) of the
- configuration/admin view should subclass. It contains some state
+ - ConfigPageView (this view) - Each enum or section (tab) of the
+ configuration/admin view should subclass this. It contains some state
management and common needs for rendering itself in the overall
HiGridView view paradigm.
@@ -36,20 +37,17 @@ class ConfigPageView( HiGridView ):
Override Django dispatch() method to handle dispatching to ensure
states and views are consistent for all config tab/pages.
"""
+ if self.should_force_sync_request(
+ request = request,
+ next_view_type = ViewType.CONFIGURATION,
+ next_id = None ):
+ redirect_url = request.get_full_path()
+ return antinode.redirect_response( redirect_url )
- view_type_changed = bool( request.view_parameters.view_type != ViewType.CONFIGURATION )
request.view_parameters.view_type = ViewType.CONFIGURATION
request.view_parameters.view_mode = ViewMode.MONITOR
request.view_parameters.to_session( request )
- # Especially for editing mode, where it may have editing context in
- # many page sections, we should not asynchronously load the config
- # content without refreshing all areas first.
- #
- if view_type_changed and is_ajax( request ):
- redirect_url = request.get_full_path()
- return antinode.redirect_response( redirect_url )
-
request.config_page_type_list = list( ConfigPageType )
request.current_config_page_type = self.config_page_type
diff --git a/src/hi/apps/location/views.py b/src/hi/apps/location/views.py
index 2ac354e9..a0e0949b 100644
--- a/src/hi/apps/location/views.py
+++ b/src/hi/apps/location/views.py
@@ -8,11 +8,11 @@ from django.views.generic import View
from hi.apps.common.utils import is_ajax
from hi.enums import ViewType
-from hi.exceptions import ForceRedirectException
+from hi.exceptions import ForceSynchronousException
from hi.hi_grid_view import HiGridView
from .location_manager import LocationManager
-from .models import Location, LocationView
+from .models import LocationView
from .view_mixin import LocationViewMixin
logger = logging.getLogger(__name__)
@@ -21,7 +21,6 @@ logger = logging.getLogger(__name__)
class LocationViewDefaultView( View ):
def get(self, request, *args, **kwargs):
-
try:
location_view = LocationManager().get_default_location_view( request = request )
request.view_parameters.view_type = ViewType.LOCATION_VIEW
@@ -45,27 +44,16 @@ 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
-
-
+ if self.should_force_sync_request(
+ request = request,
+ next_view_type = ViewType.LOCATION_VIEW,
+ next_id = location_view.id ):
+ raise 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 )
-
request.view_parameters.view_type = ViewType.LOCATION_VIEW
request.view_parameters.update_location_view( location_view )
request.view_parameters.to_session( request )
- if ( request.is_editing
- and is_ajax( request )
- and ( view_type_changed or view_id_changed )):
- redirect_url = reverse( 'location_view', kwargs = kwargs )
- raise ForceRedirectException( url = redirect_url )
-
location_view_data = LocationManager().get_location_view_data(
location_view = location_view,
)
diff --git a/src/hi/exceptions.py b/src/hi/exceptions.py
index 4c156e00..7a0547f9 100644
--- a/src/hi/exceptions.py
+++ b/src/hi/exceptions.py
@@ -1,6 +1,6 @@
class ForceRedirectException( Exception ):
- def __init__(self, url, message = f'Force redirect' ):
+ def __init__(self, url, message = 'Force redirect' ):
self._url = url
super().__init__(message)
return
diff --git a/src/hi/hi_grid_view.py b/src/hi/hi_grid_view.py
index c7c4e4ee..8cc89010 100644
--- a/src/hi/hi_grid_view.py
+++ b/src/hi/hi_grid_view.py
@@ -2,6 +2,7 @@ import logging
from typing import Dict
import urllib.parse
+from django.http import HttpRequest
from django.shortcuts import redirect, render
from django.template.loader import get_template
from django.urls import resolve
@@ -16,7 +17,8 @@ from hi.apps.collection.models import Collection
from hi.apps.location.models import Location
from hi.constants import DIVID
-from hi.exceptions import ForceRedirectException
+from hi.enums import ViewType
+from hi.exceptions import ForceRedirectException, ForceSynchronousException
from hi.hi_async_view import HiSideView
logger = logging.getLogger(__name__)
@@ -48,7 +50,7 @@ class HiGridView(View):
raise NotImplementedError('Subclasses must override this method.')
def get_template_context( self, request, *args, **kwargs ) -> Dict[ str, str ]:
- """ Can raise exceptions like BadRequest, Http404, etc. """
+ """ Can raise exceptions like BadRequest, Http404, ForceRedirectException, etc. """
raise NotImplementedError('Subclasses must override this method.')
def get_top_template_name( self ) -> str:
@@ -60,24 +62,23 @@ class HiGridView(View):
def get_top_template_context( self, request, *args, **kwargs ):
if not request.view_parameters.location:
return dict()
+ location_list = list( Location.objects.all() )
location_view_list = list( request.view_parameters.location.views.order_by( 'order_id' ))
- return { 'location_view_list': location_view_list }
+ return {
+ 'location_view_list': location_view_list,
+ 'location_list': location_list,
+ }
def get_bottom_template_context( self, request, *args, **kwargs ):
collection_list = list( Collection.objects.all().order_by( 'order_id' ))
return { 'collection_list': collection_list }
- def get_content( self, request, *args, **kwargs ) -> str:
- template_name = self.get_main_template_name()
- template = get_template( template_name )
- context = self.get_template_context( request, *args, **kwargs )
- return template.render( context, request = request )
-
def get(self, request, *args, **kwargs):
-
if is_ajax( request ):
return self.get_async_response( request, *args, **kwargs )
+ return self.get_synchronous_response( request, *args, **kwargs )
+ def get_synchronous_response( self, request, *args, **kwargs ):
try:
context = self.get_template_context( request, *args, **kwargs )
except ForceRedirectException as fde:
@@ -96,15 +97,14 @@ class HiGridView(View):
context.update( self.get_top_template_context( request, *args, **kwargs ))
context.update( self.get_bottom_template_context( request, *args, **kwargs ))
- context['location_list'] = list( Location.objects.all() )
return render( request, self.HI_GRID_TEMPLATE_NAME, context )
-
+
def get_async_response( self, request, *args, **kwargs ):
try:
main_content = self.get_content( request, *args, **kwargs )
- except ForceRedirectException as fde:
- return antinode.redirect_response( url = fde.url )
- except ForceSynchronousException as fse:
+ except ForceRedirectException as fre:
+ return antinode.redirect_response( url = fre.url )
+ except ForceSynchronousException:
redirect_url = request.get_full_path()
return antinode.redirect_response( redirect_url )
@@ -116,6 +116,12 @@ class HiGridView(View):
push_url = push_url,
)
+ def get_content( self, request, *args, **kwargs ) -> str:
+ template_name = self.get_main_template_name()
+ template = get_template( template_name )
+ context = self.get_template_context( request, *args, **kwargs )
+ return template.render( context, request = request )
+
def get_push_url( self, request ):
referrer_url = request.META.get('HTTP_REFERER', '')
if referrer_url:
@@ -187,3 +193,37 @@ class HiGridView(View):
view_class = resolved_match.func.view_class
view = view_class()
return ( view, view_kwargs )
+
+ 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_mixin.py b/src/hi/view_mixin.py
deleted file mode 100644
index b892eaca..00000000
--- a/src/hi/view_mixin.py
+++ /dev/null
@@ -1,47 +0,0 @@
-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
-
-
-