Skip to content

Home Information - Your home, your data, organized.

Python

Major refactoring of how async views work. WIP

Anthony Cassandra5349dd2e795f
diff --git a/src/hi/apps/collection/async_views.py b/src/hi/apps/collection/async_views.py
new file mode 100644
index 00000000..ce6f0542
--- /dev/null
+++ b/src/hi/apps/collection/async_views.py
@@ -0,0 +1,46 @@
+import logging
+
+from django.core.exceptions import BadRequest
+from django.http import Http404
+
+from hi.hi_async_view import HiSideView
+
+from .collection_manager import CollectionManager
+from .models import Collection
+
+logger = logging.getLogger(__name__)
+
+
+class CollectionDetailsView( HiSideView ):
+
+ def get_template_name( self ) -> str:
+ return 'collection/panes/collection_details.html'
+
+ def should_push_url( self ):
+ return True
+
+ def get_template_context( self, request, *args, **kwargs ):
+ collection_id = kwargs.get( 'collection_id' )
+ if not collection_id:
+ raise BadRequest( 'Missing collection id in request.' )
+ try:
+ collection = Collection.objects.get( id = collection_id )
+ except Collection.DoesNotExist:
+ raise Http404()
+
+ current_location_view = None
+ if request.view_parameters.view_type.is_location_view:
+ current_location_view = request.view_parameters.location_view
+
+ collection_detail_data = CollectionManager().get_collection_detail_data(
+ collection = collection,
+ current_location_view = current_location_view,
+ is_editing = request.is_editing,
+ )
+
+ return {
+ 'collection_detail_data': collection_detail_data,
+ }
+
+
+
diff --git a/src/hi/apps/collection/edit/async_views.py b/src/hi/apps/collection/edit/async_views.py
new file mode 100644
index 00000000..8ae49e8b
--- /dev/null
+++ b/src/hi/apps/collection/edit/async_views.py
@@ -0,0 +1,27 @@
+import logging
+
+from django.utils.decorators import method_decorator
+
+from hi.apps.collection.collection_manager import CollectionManager
+from hi.decorators import edit_required
+
+from hi.hi_async_view import HiSideView
+
+logger = logging.getLogger(__name__)
+
+
+@method_decorator( edit_required, name='dispatch' )
+class CollectionManageItemsView( HiSideView ):
+
+ def get_template_name( self ) -> str:
+ return 'collection/edit/panes/collection_manage_items.html'
+
+ def get_template_context( self, request, *args, **kwargs ):
+
+ collection = request.view_parameters.collection
+ entity_collection_group_list = CollectionManager().create_entity_collection_group_list(
+ collection = collection,
+ )
+ return {
+ 'entity_collection_group_list': entity_collection_group_list,
+ }
diff --git a/src/hi/apps/collection/edit/urls.py b/src/hi/apps/collection/edit/urls.py
index 6574fdbf..583e31f6 100644
--- a/src/hi/apps/collection/edit/urls.py
+++ b/src/hi/apps/collection/edit/urls.py
@@ -1,5 +1,6 @@
from django.urls import re_path
+from . import async_views
from . import views
@@ -13,9 +14,9 @@ urlpatterns = [
views.CollectionDeleteView.as_view(),
name='collection_edit_collection_delete' ),
- re_path( r'^collection/add-remove-item$',
- views.CollectionAddRemoveItemView.as_view(),
- name='collection_edit_collection_add_remove_item' ),
+ re_path( r'^collection/manage-item$',
+ async_views.CollectionManageItemsView.as_view(),
+ name='collection_edit_collection_manage_items' ),
re_path( r'^collection/entity/toggle/(?P<collection_id>\d+)/(?P<entity_id>\d+)$',
views.CollectionEntityToggleView.as_view(),
diff --git a/src/hi/apps/collection/edit/views.py b/src/hi/apps/collection/edit/views.py
index a53ec509..d8689af2 100644
--- a/src/hi/apps/collection/edit/views.py
+++ b/src/hi/apps/collection/edit/views.py
@@ -126,30 +126,6 @@ class CollectionDeleteView( View ):
return antinode.redirect_response( redirect_url )
-@method_decorator( edit_required, name='dispatch' )
-class CollectionAddRemoveItemView( View ):
-
- def get(self, request, *args, **kwargs):
- collection = request.view_parameters.collection
- context = self.get_add_remove_template_context( collection )
- template = get_template( 'collection/edit/panes/collection_add_remove_item.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['EDIT_ITEM']: content,
- },
- )
-
- @classmethod
- def get_add_remove_template_context( self, collection : Collection ):
- entity_collection_group_list = CollectionManager().create_entity_collection_group_list(
- collection = collection,
- )
- return {
- 'entity_collection_group_list': entity_collection_group_list,
- }
-
-
@method_decorator( edit_required, name='dispatch' )
class CollectionEntityToggleView( View ):
diff --git a/src/hi/apps/collection/templates/collection/collection_buttons.html b/src/hi/apps/collection/templates/collection/collection_buttons.html
index 85868bc0..365b6e1a 100644
--- a/src/hi/apps/collection/templates/collection/collection_buttons.html
+++ b/src/hi/apps/collection/templates/collection/collection_buttons.html
@@ -7,7 +7,7 @@
<a href="{% url 'collection_view' id=collection.id %}"
class="btn btn-secondary btn-control"
role="button"
- data-async="#{{ DIVID.MAIN }}" >
+ data-async="true" >
{{ collection.name }}
</a>
</div>
diff --git a/src/hi/apps/collection/templates/collection/edit/panes/collection_add_remove_item.html b/src/hi/apps/collection/templates/collection/edit/panes/collection_manage_items.html
similarity index 84%
rename from src/hi/apps/collection/templates/collection/edit/panes/collection_add_remove_item.html
rename to src/hi/apps/collection/templates/collection/edit/panes/collection_manage_items.html
index 1a405b5c..ed599cda 100644
--- a/src/hi/apps/collection/templates/collection/edit/panes/collection_add_remove_item.html
+++ b/src/hi/apps/collection/templates/collection/edit/panes/collection_manage_items.html
@@ -1,4 +1,8 @@
-<div class="h5">Membership</div>
+{% if request.is_editing %}
+{% include "edit/panes/side_header.html" %}
+{% endif %}
+
+<div class="h5">Items in Collection</div>
<div class="">
<a href="{% url 'entity_edit_entity_add' %}"
diff --git a/src/hi/apps/collection/templates/collection/edit/panes/side_header.html b/src/hi/apps/collection/templates/collection/edit/panes/side_header.html
deleted file mode 100644
index 2e909331..00000000
--- a/src/hi/apps/collection/templates/collection/edit/panes/side_header.html
+++ /dev/null
@@ -1,8 +0,0 @@
-<div>
- <div class="">
- <a href="{% url 'collection_edit_collection_add_remove_item' %}"
- class="btn btn-tertiary btn-control" role="button" data-async="true">
- ADD/REMOVE {{ request.view_parameters.collection.name }} ITEMS
- </a>
- </div>
-</div>
diff --git a/src/hi/apps/collection/templates/collection/panes/collection_details.html b/src/hi/apps/collection/templates/collection/panes/collection_details.html
index b6b235e4..b94631a4 100644
--- a/src/hi/apps/collection/templates/collection/panes/collection_details.html
+++ b/src/hi/apps/collection/templates/collection/panes/collection_details.html
@@ -1,3 +1,7 @@
+{% if request.is_editing %}
+{% include "edit/panes/side_header.html" %}
+{% endif %}
+
<div class="d-flex justify-content-between">
<div class="h5"><small>Collection:</small>{{ collection_detail_data.collection.name }}</div>
<div class="">
diff --git a/src/hi/apps/collection/urls.py b/src/hi/apps/collection/urls.py
index 248ec4ea..1aecd16b 100644
--- a/src/hi/apps/collection/urls.py
+++ b/src/hi/apps/collection/urls.py
@@ -1,5 +1,6 @@
from django.urls import include, re_path
+from . import async_views
from . import views
@@ -14,7 +15,7 @@ urlpatterns = [
name='collection_view_default'),
re_path( r'^details/(?P<collection_id>\d+)$',
- views.CollectionDetailsView.as_view(),
+ async_views.CollectionDetailsView.as_view(),
name='collection_details' ),
re_path( r'^edit/', include('hi.apps.collection.edit.urls' )),
diff --git a/src/hi/apps/collection/views.py b/src/hi/apps/collection/views.py
index f39f01bf..3db07dd3 100644
--- a/src/hi/apps/collection/views.py
+++ b/src/hi/apps/collection/views.py
@@ -1,14 +1,14 @@
import logging
-from django.http import HttpResponseRedirect
+from django.http import HttpResponseRedirect, Http404
from django.template.loader import get_template
from django.urls import reverse
from django.views.generic import View
-from hi.apps.collection.edit.views import CollectionAddRemoveItemView
import hi.apps.common.antinode as antinode
from hi.apps.common.utils import is_ajax
from hi.enums import ViewType
+from hi.exceptions import ForceRedirectException
from hi.hi_grid_view import HiGridView
from hi.views import bad_request_response, page_not_found_response
@@ -47,18 +47,21 @@ class CollectionViewDefaultView( View ):
class CollectionView( HiGridView ):
- def get(self, request, *args, **kwargs):
+ def get_main_template_name( self ) -> str:
+ return 'collection/collection_view.html'
+
+ def get_template_context( self, request, *args, **kwargs ):
collection_id = kwargs.get('id')
try:
collection = Collection.objects.get( id = collection_id )
except Collection.DoesNotExist:
- message = f'Collection "{collection_id}" does not exist.'
- logger.warning( message )
- return bad_request_response( request, message = message )
+ raise Http404()
# 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 )
+
request.view_parameters.view_type = ViewType.COLLECTION
request.view_parameters.collection_id = collection.id
request.view_parameters.to_session( request )
@@ -68,69 +71,21 @@ class CollectionView( HiGridView ):
# views are invalidated. Else, the editing state and edit side
# panel will be invalid for the new view.
#
- if view_type_changed and is_ajax( request ):
- sync_url = reverse( 'collection_view', kwargs = kwargs )
- return antinode.redirect_response( url = sync_url )
+ 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,
)
- context = {
+ return {
'is_async_request': is_ajax( request ),
'collection': collection,
'collection_data': collection_data,
}
- side_template_name = None
- if request.is_editing:
- context.update(
- CollectionAddRemoveItemView.get_add_remove_template_context(
- collection = collection,
- )
- )
- side_template_name = 'edit/panes/side.html'
-
- return self.hi_grid_response(
- request = request,
- context = context,
- main_template_name = 'collection/collection_view.html',
- side_template_name = side_template_name,
- push_url_name = 'collection_view',
- push_url_kwargs = kwargs,
- )
-
-
-class CollectionDetailsView( View ):
-
- def get( self, request, *args, **kwargs ):
- collection_id = kwargs.get( 'collection_id' )
- if not collection_id:
- return bad_request_response( request, message = 'Missing collection id in request.' )
- try:
- collection = Collection.objects.get( id = collection_id )
- except Collection.DoesNotExist:
- return page_not_found_response( request )
-
- current_location_view = None
- if request.view_parameters.view_type.is_location_view:
- current_location_view = request.view_parameters.location_view
-
- collection_detail_data = CollectionManager().get_collection_detail_data(
- collection = collection,
- current_location_view = current_location_view,
- is_editing = request.is_editing,
- )
-
- context = {
- 'collection_detail_data': collection_detail_data,
- }
- template = get_template( 'collection/panes/collection_details.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['EDIT_ITEM']: content,
- },
- )
diff --git a/src/hi/apps/config/async_views.py b/src/hi/apps/config/async_views.py
new file mode 100644
index 00000000..08a78653
--- /dev/null
+++ b/src/hi/apps/config/async_views.py
@@ -0,0 +1,16 @@
+from hi.integrations.core.views import IntegrationViewMixin
+
+from hi.constants import DIVID
+from hi.hi_async_view import HiAsyncView
+
+
+class ConfigTabPaneView( HiAsyncView, IntegrationViewMixin ):
+
+ def get_target_div_id( self ) -> str:
+ return DIVID['INTEGRATION_TAB']
+
+ def get_template_name( self ) -> str:
+ return 'core/panes/integration_config_tab.html'
+
+ def get_template_context( self, request, *args, **kwargs ):
+ return self.get_integration_config_tab_context()
diff --git a/src/hi/apps/config/urls.py b/src/hi/apps/config/urls.py
index 205b0a0d..ed13b28f 100644
--- a/src/hi/apps/config/urls.py
+++ b/src/hi/apps/config/urls.py
@@ -1,5 +1,6 @@
from django.urls import re_path
+from . import async_views
from . import views
@@ -10,7 +11,7 @@ urlpatterns = [
name='config_home_pane' ),
re_path( r'^tab$',
- views.ConfigTabPaneView.as_view(),
+ async_views.ConfigTabPaneView.as_view(),
name='config_tab_pane' ),
]
diff --git a/src/hi/apps/config/views.py b/src/hi/apps/config/views.py
index 92d0a39e..ed156932 100644
--- a/src/hi/apps/config/views.py
+++ b/src/hi/apps/config/views.py
@@ -1,16 +1,20 @@
-from django.shortcuts import render
from django.urls import reverse
-import hi.apps.common.antinode as antinode
from hi.apps.common.utils import is_ajax
+
+from hi.integrations.core.views import IntegrationViewMixin
+
from hi.enums import ViewMode, ViewType
+from hi.exceptions import ForceRedirectException
from hi.hi_grid_view import HiGridView
-from hi.integrations.core.views import IntegrationViewMixin
class ConfigHomePaneView( HiGridView, IntegrationViewMixin ):
- def get(self, request, *args, **kwargs):
+ def get_main_template_name( self ) -> str:
+ return 'config/panes/home.html'
+
+ def get_template_context( self, request, *args, **kwargs ):
view_type_changed = bool( request.view_parameters.view_type != ViewType.CONFIGURATION )
request.view_parameters.view_type = ViewType.CONFIGURATION
@@ -18,23 +22,7 @@ class ConfigHomePaneView( HiGridView, IntegrationViewMixin ):
request.view_parameters.to_session( request )
if view_type_changed and is_ajax( request ):
- sync_url = reverse('config_home_pane')
- return antinode.redirect_response( url = sync_url )
-
- context = {
- }
- context.update( self.get_integration_config_tab_context() )
- return self.hi_grid_response(
- request = request,
- context = context,
- main_template_name = 'config/panes/home.html',
- push_url_name = 'config_home_pane',
- )
-
-
-class ConfigTabPaneView( HiGridView, IntegrationViewMixin ):
-
- def get(self, request, *args, **kwargs):
-
- context = self.get_integration_config_tab_context()
- return render( request, 'core/panes/integration_config_tab.html', context )
+ redirect_url = reverse('config_home_pane')
+ raise ForceRedirectException( url = redirect_url )
+
+ return self.get_integration_config_tab_context()
diff --git a/src/hi/apps/edit/templates/edit/panes/side.html b/src/hi/apps/edit/templates/edit/panes/side.html
deleted file mode 100644
index 2ad0ab2a..00000000
--- a/src/hi/apps/edit/templates/edit/panes/side.html
+++ /dev/null
@@ -1,22 +0,0 @@
-<div id="hi-edit-side-content" class="">
- {% if request.view_parameters.view_type.is_location_view %}
- {% include "location/edit/panes/side_header.html" %}
- {% elif request.view_parameters.view_type.is_collection %}
- {% include "collection/edit/panes/side_header.html" %}
- {% endif %}
-
- <div id="hi-edit-item">
- {% if entity_detail_data %}
- {% include "entity/panes/entity_details.html" %}
- {% elif collection_detail_data %}
- {% include "collection/panes/collection_details.html" %}
- {% elif location_detail_data %}
- {% include "location/panes/location_details.html" %}
- {% elif entity_view_group_list and collection_view_group %}
- {% include "location/edit/panes/location_view_add_remove_item.html" %}
- {% elif entity_collection_group_list %}
- {% include "collection/edit/panes/collection_add_remove_item.html" %}
- {% endif %}
- </div>
-
-</div>
diff --git a/src/hi/apps/edit/templates/edit/panes/side_header.html b/src/hi/apps/edit/templates/edit/panes/side_header.html
new file mode 100644
index 00000000..191e6157
--- /dev/null
+++ b/src/hi/apps/edit/templates/edit/panes/side_header.html
@@ -0,0 +1,7 @@
+<div>
+ {% if request.view_parameters.view_type.is_location_view %}
+ <div class="h3"><small>View:</small> {{ request.view_parameters.location_view.name }}</div>
+ {% elif request.view_parameters.view_type.is_collection %}
+ <div class="h3"><small>Collection:</small> {{ request.view_parameters.collection.name }}</div>
+ {% endif %}
+</div>
diff --git a/src/hi/apps/edit/views.py b/src/hi/apps/edit/views.py
index 7b9ec0fa..679ad70b 100644
--- a/src/hi/apps/edit/views.py
+++ b/src/hi/apps/edit/views.py
@@ -6,8 +6,8 @@ from django.urls import reverse
from django.utils.decorators import method_decorator
from django.views.generic import View
-import hi.apps.collection.edit.views as collection_edit_views
-import hi.apps.location.edit.views as location_edit_views
+from hi.apps.collection.edit.async_views import CollectionManageItemsView
+from hi.apps.location.edit.async_views import LocationViewManageItemsView
from hi.decorators import edit_required
from hi.enums import ItemType
from hi.views import bad_request_response
@@ -57,10 +57,10 @@ class ItemDetailsCloseView( View ):
def get(self, request, *args, **kwargs ):
if request.view_parameters.view_type.is_location_view:
- return location_edit_views.LocationViewAddRemoveItemView().get( request, *args, **kwargs )
+ return LocationViewManageItemsView().get( request, *args, **kwargs )
elif request.view_parameters.view_type.is_collection:
- return collection_edit_views.CollectionAddRemoveItemView().get( request, *args, **kwargs )
+ return CollectionManageItemsView().get( request, *args, **kwargs )
raise bad_request_response( request, 'Add/remove items not supported for current view type.' )
diff --git a/src/hi/apps/entity/async_views.py b/src/hi/apps/entity/async_views.py
new file mode 100644
index 00000000..1d049473
--- /dev/null
+++ b/src/hi/apps/entity/async_views.py
@@ -0,0 +1,43 @@
+import logging
+
+from django.core.exceptions import BadRequest
+from django.http import Http404
+
+from hi.hi_async_view import HiSideView
+
+from .entity_manager import EntityManager
+from .models import Entity
+
+logger = logging.getLogger(__name__)
+
+
+class EntityDetailsView( HiSideView ):
+
+ def get_template_name( self ) -> str:
+ return 'entity/panes/entity_details.html'
+
+ def should_push_url( self ):
+ return True
+
+ def get_template_context( self, request, *args, **kwargs ):
+
+ entity_id = kwargs.get( 'entity_id' )
+ if not entity_id:
+ raise BadRequest( 'Missing entity id in request.' )
+ try:
+ entity = Entity.objects.get( id = entity_id )
+ except Entity.DoesNotExist:
+ raise Http404()
+
+ current_location_view = None
+ if request.view_parameters.view_type.is_location_view:
+ current_location_view = request.view_parameters.location_view
+
+ entity_detail_data = EntityManager().get_entity_detail_data(
+ entity = entity,
+ current_location_view = current_location_view,
+ is_editing = request.is_editing,
+ )
+ return {
+ 'entity_detail_data': entity_detail_data,
+ }
diff --git a/src/hi/apps/entity/templates/entity/panes/entity_details.html b/src/hi/apps/entity/templates/entity/panes/entity_details.html
index c6b53c5a..76c2dc72 100644
--- a/src/hi/apps/entity/templates/entity/panes/entity_details.html
+++ b/src/hi/apps/entity/templates/entity/panes/entity_details.html
@@ -1,3 +1,7 @@
+{% if request.is_editing %}
+{% include "edit/panes/side_header.html" %}
+{% endif %}
+
<div class="d-flex justify-content-between">
<div class="h5"><small>Entity</small>: {{ entity_detail_data.entity.name }}</div>
<div class="">
diff --git a/src/hi/apps/entity/urls.py b/src/hi/apps/entity/urls.py
index cd29a841..643c0d56 100644
--- a/src/hi/apps/entity/urls.py
+++ b/src/hi/apps/entity/urls.py
@@ -1,12 +1,12 @@
from django.urls import include, re_path
-from . import views
+from . import async_views
urlpatterns = [
re_path( r'^details/(?P<entity_id>\d+)$',
- views.EntityDetailsView.as_view(),
+ async_views.EntityDetailsView.as_view(),
name='entity_details' ),
re_path( r'^edit/', include('hi.apps.entity.edit.urls' )),
diff --git a/src/hi/apps/entity/views.py b/src/hi/apps/entity/views.py
deleted file mode 100644
index afd52cb3..00000000
--- a/src/hi/apps/entity/views.py
+++ /dev/null
@@ -1,49 +0,0 @@
-import logging
-
-from django.template.loader import get_template
-from django.views.generic import View
-
-import hi.apps.common.antinode as antinode
-
-from hi.constants import DIVID
-from hi.views import bad_request_response, page_not_found_response
-
-from .entity_manager import EntityManager
-from .models import Entity
-
-logger = logging.getLogger(__name__)
-
-
-class EntityDetailsView( View ):
-
- def get( self, request, *args, **kwargs ):
- entity_id = kwargs.get( 'entity_id' )
- if not entity_id:
- return bad_request_response( request, message = 'Missing entity id in request.' )
- try:
- entity = Entity.objects.get( id = entity_id )
- except Entity.DoesNotExist:
- return page_not_found_response( request, message = f'No entity with id "{entity_id}".' )
-
- current_location_view = None
- if request.view_parameters.view_type.is_location_view:
- current_location_view = request.view_parameters.location_view
-
- entity_detail_data = EntityManager().get_entity_detail_data(
- entity = entity,
- current_location_view = current_location_view,
- is_editing = request.is_editing,
- )
-
- context = {
- 'entity_detail_data': entity_detail_data,
- }
- template = get_template( 'entity/panes/entity_details.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['EDIT_ITEM']: content,
- },
- )
-
-
diff --git a/src/hi/apps/location/async_views.py b/src/hi/apps/location/async_views.py
new file mode 100644
index 00000000..7de9b484
--- /dev/null
+++ b/src/hi/apps/location/async_views.py
@@ -0,0 +1,126 @@
+import logging
+
+from django.core.exceptions import BadRequest
+from django.http import Http404, HttpResponseRedirect
+from django.urls import reverse
+from django.views.generic import View
+
+from hi.apps.collection.async_views import CollectionDetailsView
+from hi.apps.entity.async_views import EntityDetailsView
+from hi.apps.location.edit.forms import (
+ LocationAttributeFormset,
+ LocationEditForm,
+ LocationSvgFileForm,
+ LocationViewEditForm,
+)
+
+from hi.enums import ItemType
+from hi.hi_async_view import HiSideView
+
+from .models import LocationView
+
+logger = logging.getLogger(__name__)
+
+
+class LocationViewDetailsView( HiSideView ):
+
+ def get_template_name( self ) -> str:
+ return 'location/panes/location_details.html'
+
+ def should_push_url( self ):
+ return True
+
+ def get_template_context( self, request, *args, **kwargs ):
+
+ location_view_id = kwargs.get( 'location_view_id' )
+ if location_view_id:
+ location_view_id = int(location_view_id)
+ default_location_view_id = request.view_parameters.location_view_id
+ if default_location_view_id:
+ default_location_view_id = int(default_location_view_id)
+
+ if not location_view_id and not default_location_view_id:
+ raise BadRequest( 'No location given and no default available.' )
+
+ if location_view_id and ( location_view_id != default_location_view_id ):
+ try:
+ location_view = LocationView.objects.get( id = location_view_id )
+ except LocationView.DoesNotExist:
+ raise Http404()
+ else:
+ location_view = request.view_parameters.location_view
+
+ return {
+ 'location': location_view.location,
+ 'location_edit_form': LocationEditForm( instance = location_view.location ),
+ 'location_attribute_formset': LocationAttributeFormset(
+ instance = location_view.location,
+ form_kwargs = {
+ 'is_editable': True,
+ },
+ ),
+ 'location_svg_file_form': LocationSvgFileForm( ),
+ 'location_view': location_view,
+ 'location_view_edit_form': LocationViewEditForm( instance = location_view ),
+ }
+
+
+class LocationItemDetailsViewNEW( HiSideView ):
+
+ def dispatch( self, request, *args, **kwargs ):
+ try:
+ self._item_type, self._item_id = ItemType.parse_from_dict( kwargs )
+ if self._item_type == ItemType.ENTITY:
+ self._view = EntityDetailsView()
+ self._kwargs = { 'entity_id': self._item_id }
+ elif self._item_type == ItemType.COLLECTION:
+ self._view = CollectionDetailsView()
+ self._kwargs = { 'collection_id': self._item_id }
+ else:
+ raise BadRequest( 'Unsupported item type.' )
+
+ except ValueError:
+ raise BadRequest( 'Bad item id.' )
+
+ return super().dispatch( request, *args, **kwargs )
+
+ def get_template_name( self ) -> str:
+ return self._view.get_template_name()
+
+ def should_push_url( self ):
+ return True
+
+ def get_template_context( self, request, *args, **kwargs ):
+ return self._view.get_template_context( request, **self._kwargs )
+
+
+class LocationItemDetailsView( View ):
+
+ def get(self, request, *args, **kwargs):
+ try:
+ ( item_type, item_id ) = ItemType.parse_from_dict( kwargs )
+ except ValueError:
+ raise BadRequest( request, message = 'Bad item id.' )
+
+ if item_type == ItemType.ENTITY:
+ redirect_url = reverse( 'entity_details', kwargs = { 'entity_id': item_id } )
+ return HttpResponseRedirect( redirect_url )
+ return EntityDetailsView().get(
+ request = request,
+ entity_id = item_id,
+ )
+
+ if item_type == ItemType.COLLECTION:
+ redirect_url = reverse( 'collection_details', kwargs = { 'collection_id': item_id } )
+ return HttpResponseRedirect( redirect_url )
+ return CollectionDetailsView().get(
+ request = request,
+ collection_id = item_id,
+ )
+
+ raise BadRequest( 'Unknown item type "{item_type}".' )
+
+
+
+
+
diff --git a/src/hi/apps/location/edit/async_views.py b/src/hi/apps/location/edit/async_views.py
new file mode 100644
index 00000000..140650e6
--- /dev/null
+++ b/src/hi/apps/location/edit/async_views.py
@@ -0,0 +1,32 @@
+import logging
+
+from django.utils.decorators import method_decorator
+
+from hi.apps.location.location_manager import LocationManager
+
+from hi.decorators import edit_required
+from hi.hi_async_view import HiSideView
+
+logger = logging.getLogger(__name__)
+
+
+@method_decorator( edit_required, name='dispatch' )
+class LocationViewManageItemsView( HiSideView ):
+
+ def get_template_name( self ) -> str:
+ return 'location/edit/panes/location_view_manage_items.html'
+
+ def get_template_context( self, request, *args, **kwargs ):
+
+ location_view = request.view_parameters.location_view
+ location_manager = LocationManager()
+ entity_view_group_list = location_manager.create_entity_view_group_list(
+ location_view = location_view,
+ )
+ collection_view_group = location_manager.create_collection_view_group(
+ location_view = location_view,
+ )
+ return {
+ 'entity_view_group_list': entity_view_group_list,
+ 'collection_view_group': collection_view_group,
+ }
diff --git a/src/hi/apps/location/edit/urls.py b/src/hi/apps/location/edit/urls.py
index daf3e96d..12148b77 100644
--- a/src/hi/apps/location/edit/urls.py
+++ b/src/hi/apps/location/edit/urls.py
@@ -1,5 +1,6 @@
from django.urls import re_path
+from . import async_views
from . import views
@@ -33,9 +34,9 @@ urlpatterns = [
views.LocationViewDeleteView.as_view(),
name='location_edit_location_view_delete' ),
- re_path( r'^location-view/add-remove-item$',
- views.LocationViewAddRemoveItemView.as_view(),
- name='location_edit_location_view_add_remove_item' ),
+ re_path( r'^location-view/manage-item$',
+ async_views.LocationViewManageItemsView.as_view(),
+ name='location_edit_location_view_manage_items' ),
re_path( r'^location-view/entity/toggle/(?P<location_view_id>\d+)/(?P<entity_id>\d+)$',
views.LocationViewEntityToggleView.as_view(),
diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.py
index 4adb9909..d9fe2588 100644
--- a/src/hi/apps/location/edit/views.py
+++ b/src/hi/apps/location/edit/views.py
@@ -183,7 +183,30 @@ class LocationViewAddView( View ):
class LocationViewEditView( View ):
def post( self, request, *args, **kwargs ):
- raise NotImplementedError()
+
+ location_view_id = kwargs.get('location_view_id')
+ try:
+ location_view = LocationView.objects.select_related(
+ 'location' ).get( id = location_view_id )
+ except LocationView.DoesNotExist:
+ return page_not_found_response( request )
+
+ location_view_edit_form = forms.LocationViewEditForm( request.POST, instance = location_view )
+ if not location_view_edit_form.is_valid():
+ context = {
+ 'location_view': location_view,
+ 'location_view_edit_form': location_view_edit_form,
+ }
+ template = get_template( 'location/edit/panes/location_view_edit.html' )
+ content = template.render( context, request = request )
+ return antinode.response(
+ insert_map = {
+ DIVID['LOCATION_VIEW_EDIT_PANE']: content,
+ },
+ )
+
+ location_view_edit_form.save()
+ return antinode.refresh_response()
class LocationViewGeometryView( View ):
@@ -284,36 +307,6 @@ class LocationViewDeleteView( View ):
return antinode.redirect_response( redirect_url )
-@method_decorator( edit_required, name='dispatch' )
-class LocationViewAddRemoveItemView( View ):
-
- def get(self, request, *args, **kwargs):
-
- location_view = request.view_parameters.location_view
- context = self.get_add_remove_template_context( location_view )
- template = get_template( 'location/edit/panes/location_view_add_remove_item.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['EDIT_ITEM']: content,
- },
- )
-
- @classmethod
- def get_add_remove_template_context( self, location_view : LocationView ):
- location_manager = LocationManager()
- entity_view_group_list = location_manager.create_entity_view_group_list(
- location_view = location_view,
- )
- collection_view_group = location_manager.create_collection_view_group(
- location_view = location_view,
- )
- return {
- 'entity_view_group_list': entity_view_group_list,
- 'collection_view_group': collection_view_group,
- }
-
-
@method_decorator( edit_required, name='dispatch' )
class LocationViewEntityToggleView( View ):
diff --git a/src/hi/apps/location/location_detail_data.py b/src/hi/apps/location/location_detail_data.py
deleted file mode 100644
index 465b782f..00000000
--- a/src/hi/apps/location/location_detail_data.py
+++ /dev/null
@@ -1,22 +0,0 @@
-from dataclasses import dataclass
-
-from hi.apps.common.svg_file_form import SvgFileForm
-from hi.apps.location.edit.forms import (
- LocationAttributeFormset,
- LocationEditForm,
- LocationSvgFileForm,
- LocationViewEditForm,
-)
-from .models import Location, LocationView
-
-
-@dataclass
-class LocationDetailData:
-
- location : Location
- location_edit_form : LocationEditForm
- location_attribute_formset : LocationAttributeFormset
- location_svg_file_form : LocationSvgFileForm
-
- location_view : LocationView
- location_view_edit_form : LocationViewEditForm
diff --git a/src/hi/apps/location/location_manager.py b/src/hi/apps/location/location_manager.py
index df3883f7..8297c5b2 100644
--- a/src/hi/apps/location/location_manager.py
+++ b/src/hi/apps/location/location_manager.py
@@ -11,7 +11,6 @@ from hi.apps.collection.models import (
CollectionView,
)
from hi.apps.common.singleton import Singleton
-from hi.apps.common.svg_file_form import SvgFileForm
from hi.apps.common.svg_models import SvgViewBox
from hi.apps.entity.delegation_manager import DelegationManager
from hi.apps.entity.entity_manager import EntityManager
@@ -19,15 +18,8 @@ from hi.apps.entity.models import (
Entity,
EntityView,
)
-from hi.apps.location.edit.forms import (
- LocationAttributeFormset,
- LocationEditForm,
- LocationSvgFileForm,
- LocationViewEditForm,
-)
from .enums import LocationViewType
-from .location_detail_data import LocationDetailData
from .location_view_data import LocationViewData
from .models import (
Location,
@@ -87,22 +79,6 @@ class LocationManager(Singleton):
os.makedirs( directory, exist_ok = True )
return
- def get_location_detail_data( self, location_view : LocationView ) -> LocationDetailData:
- # TODO: Add attributes and other data
- return LocationDetailData(
- location = location_view.location,
- location_edit_form = LocationEditForm( instance = location_view.location ),
- location_attribute_formset = LocationAttributeFormset(
- instance = location_view.location,
- form_kwargs = {
- 'is_editable': True,
- },
- ),
- location_svg_file_form = LocationSvgFileForm( ),
- location_view = location_view,
- location_view_edit_form = LocationViewEditForm( instance = location_view ),
- )
-
def create_location_view( self,
location : Location,
name : str ) -> LocationView:
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_edit.html b/src/hi/apps/location/templates/location/edit/panes/location_edit.html
index b6811012..078f0c7c 100644
--- a/src/hi/apps/location/templates/location/edit/panes/location_edit.html
+++ b/src/hi/apps/location/templates/location/edit/panes/location_edit.html
@@ -1,20 +1,15 @@
<h5>Location</h5>
-<div>{{ location_detail_data.location.name }}</div>
-
-
-<form action="{% url 'location_edit_location_edit' location_id=location_detail_data.location.id %}" method="post" enctype="multipart/form-data"
- class="form" data-async="modal">
+<form action="{% url 'location_edit_location_edit' location_id=location.id %}"
+ method="post" enctype="multipart/form-data"
+ class="form" data-async="#{{ DIVID.LOCATION_EDIT_PANE }}">
{% csrf_token %}
<div>
{% include "location/edit/panes/location_edit_form.html" %}
</div>
-<h5>Attributes</h5>
-TBD
-
<button id="hi-add-location-button" type="submit" class="btn btn-info"
- name="action" value="add">
+ name="action" value="update">
UPDATE
</button>
</form>
@@ -22,10 +17,10 @@ TBD
<h5>Order Index</h5>
-<div>{{ location_detail_data.location.order_id }}</div>
+<div>{{ location.order_id }}</div>
<div>
- <a href="{% url 'location_edit_location_delete' location_id=location_detail_data.location.id %}"
+ <a href="{% url 'location_edit_location_delete' location_id=location.id %}"
id="hi-delete-location-link" class="btn btn-danger btn-control" role="button"
data-async="true">
DELETE LOCATION AND ALL VIEWS
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_edit_form.html b/src/hi/apps/location/templates/location/edit/panes/location_edit_form.html
index 387e641d..5db27e7d 100644
--- a/src/hi/apps/location/templates/location/edit/panes/location_edit_form.html
+++ b/src/hi/apps/location/templates/location/edit/panes/location_edit_form.html
@@ -10,17 +10,17 @@
</div>
{% endif %}
-<p>
+<div>
{{ location_edit_form.name.label }}
{{ location_edit_form.name }}
{{ location_edit_form.name.errors }}
-</p>
+</div>
-<p>
+<div>
{{ location_edit_form.order_id.label }}
{{ location_edit_form.order_id }}
{{ location_edit_form.order_id.errors }}
-</p>
+</div>
<div>
{% include "attribute/panes/attribute_form.html" with attribute_formset=location_attribute_formset %}
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_view_edit.html b/src/hi/apps/location/templates/location/edit/panes/location_view_edit.html
index 945d6680..f937ba50 100644
--- a/src/hi/apps/location/templates/location/edit/panes/location_view_edit.html
+++ b/src/hi/apps/location/templates/location/edit/panes/location_view_edit.html
@@ -1,8 +1,7 @@
<h5>Location View</h5>
-<div>{{ location_detail_data.location_view.name }}</div>
-
-<form action="{% url 'location_edit_location_view_add' %}" method="post"
- class="form" data-async="modal">
+<form action="{% url 'location_edit_location_view_edit' location_view_id=location_view.id %}"
+ method="post"
+ class="form" data-async="#{{ DIVID.LOCATION_VIEW_EDIT_PANE }}">
{% csrf_token %}
<div>
@@ -10,15 +9,15 @@
</div>
<button id="hi-add-location-view-button" type="submit" class="btn btn-info"
- name="action" value="add">
- UPDATE
-</button>
+ name="action" value="update">
+ UPDATE
+ </button>
</form>
<div>
- <a href="{% url 'location_edit_location_view_delete' location_view_id=location_detail_data.location_view.id %}"
+ <a href="{% url 'location_edit_location_view_delete' location_view_id=location_view.id %}"
id="hi-delete-location-view-link" class="btn btn-danger btn-control" role="button"
data-async="true">
DELETE THIS VIEW
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_view_edit_form.html b/src/hi/apps/location/templates/location/edit/panes/location_view_edit_form.html
index ad7a7c30..72f2c4a2 100644
--- a/src/hi/apps/location/templates/location/edit/panes/location_view_edit_form.html
+++ b/src/hi/apps/location/templates/location/edit/panes/location_view_edit_form.html
@@ -15,3 +15,24 @@
{{ location_view_edit_form.name }}
{{ location_view_edit_form.name.errors }}
</div>
+
+<div>
+ {{ location_view_edit_form.order_id.label }}
+ {{ location_view_edit_form.order_id }}
+ {{ location_view_edit_form.order_id.errors }}
+</div>
+<div>
+ {{ location_view_edit_form.svg_view_box_str.label }}
+ {{ location_view_edit_form.svg_view_box_str }}
+ {{ location_view_edit_form.svg_view_box_str.errors }}
+</div>
+<div>
+ {{ location_view_edit_form.svg_rotate.label }}
+ {{ location_view_edit_form.svg_rotate }}
+ {{ location_view_edit_form.svg_rotate.errors }}
+</div>
+<div>
+ {{ location_view_edit_form.location_view_type_str.label }}
+ {{ location_view_edit_form.location_view_type_str }}
+ {{ location_view_edit_form.location_view_type_str.errors }}
+</div>
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_view_add_remove_item.html b/src/hi/apps/location/templates/location/edit/panes/location_view_manage_items.html
similarity index 89%
rename from src/hi/apps/location/templates/location/edit/panes/location_view_add_remove_item.html
rename to src/hi/apps/location/templates/location/edit/panes/location_view_manage_items.html
index 863c0efd..bab236a5 100644
--- a/src/hi/apps/location/templates/location/edit/panes/location_view_add_remove_item.html
+++ b/src/hi/apps/location/templates/location/edit/panes/location_view_manage_items.html
@@ -1,4 +1,8 @@
-<div class="h5">Item Management</div>
+{% if request.is_editing %}
+{% include "edit/panes/side_header.html" %}
+{% endif %}
+
+<div class="h5">Items in View</div>
<div class="">
<a href="{% url 'entity_edit_entity_add' %}"
diff --git a/src/hi/apps/location/templates/location/edit/panes/side_header.html b/src/hi/apps/location/templates/location/edit/panes/side_header.html
deleted file mode 100644
index 43298ccf..00000000
--- a/src/hi/apps/location/templates/location/edit/panes/side_header.html
+++ /dev/null
@@ -1,3 +0,0 @@
-<div>
- <div class="h3">View: {{ location_view.name }} <small>[{{ location_view.location.name }}]</small></div>
-</div>
diff --git a/src/hi/apps/location/templates/location/location_drop_down.html b/src/hi/apps/location/templates/location/location_drop_down.html
index cb80901e..8728c102 100644
--- a/src/hi/apps/location/templates/location/location_drop_down.html
+++ b/src/hi/apps/location/templates/location/location_drop_down.html
@@ -2,7 +2,7 @@
<div class="dropdown">
<button class="btn btn-secondary btn-control dropdown-toggle" type="button"
id="hi-location-menu" data-toggle="dropdown">
- {{ current_location.name }}
+ {{ request.view_parameters.location.name }}
<span class="caret"></span>
</button>
<div class="dropdown-menu"
diff --git a/src/hi/apps/location/templates/location/location_view_buttons.html b/src/hi/apps/location/templates/location/location_view_buttons.html
index 9d8e9146..6ba30da7 100644
--- a/src/hi/apps/location/templates/location/location_view_buttons.html
+++ b/src/hi/apps/location/templates/location/location_view_buttons.html
@@ -7,7 +7,7 @@
<a href="{% url 'location_view' id=location_view.id %}"
class="btn btn-primary btn-control"
role="button"
- data-async="#{{ DIVID.MAIN }}" >
+ data-async="true" >
{{ location_view.name }}
</a>
</div>
diff --git a/src/hi/apps/location/templates/location/panes/location_details.html b/src/hi/apps/location/templates/location/panes/location_details.html
index e42b6d2d..0950b816 100644
--- a/src/hi/apps/location/templates/location/panes/location_details.html
+++ b/src/hi/apps/location/templates/location/panes/location_details.html
@@ -1,3 +1,7 @@
+{% if request.is_editing %}
+{% include "edit/panes/side_header.html" %}
+{% endif %}
+
<div class="d-flex justify-content-between">
<div></div>
<div class="">
@@ -7,5 +11,9 @@
</a>
</div>
</div>
-{% include "location/edit/panes/location_view_edit.html" %}
-{% include "location/edit/panes/location_edit.html" %}
+<div id="{{ DIVID.LOCATION_VIEW_EDIT_PANE }}">
+ {% include "location/edit/panes/location_view_edit.html" %}
+</div>
+<div id="{{ DIVID.LOCATION_EDIT_PANE }}">
+ {% include "location/edit/panes/location_edit.html" %}
+</div>
diff --git a/src/hi/apps/location/urls.py b/src/hi/apps/location/urls.py
index a5159d7b..acd45630 100644
--- a/src/hi/apps/location/urls.py
+++ b/src/hi/apps/location/urls.py
@@ -1,5 +1,6 @@
from django.urls import include, re_path
+from . import async_views
from . import views
@@ -18,11 +19,11 @@ urlpatterns = [
name='location_view_default'),
re_path( r'^item/details/(?P<html_id>[\w\-]+)$',
- views.LocationItemDetailsView.as_view(),
+ async_views.LocationItemDetailsView.as_view(),
name='location_item_details' ),
re_path( r'^details/(?P<location_view_id>\d*)$',
- views.LocationViewDetailsView.as_view(),
+ async_views.LocationViewDetailsView.as_view(),
name='location_details' ),
re_path( r'^edit/', include('hi.apps.location.edit.urls' )),
diff --git a/src/hi/apps/location/views.py b/src/hi/apps/location/views.py
index 6c5011a7..ad489ee0 100644
--- a/src/hi/apps/location/views.py
+++ b/src/hi/apps/location/views.py
@@ -1,18 +1,25 @@
import logging
-from django.http import HttpResponseRedirect
+from django.http import HttpResponseRedirect, Http404
from django.template.loader import get_template
from django.urls import reverse
from django.views.generic import View
-from hi.apps.collection.views import CollectionDetailsView
+from hi.apps.collection.async_views import CollectionDetailsView
import hi.apps.common.antinode as antinode
from hi.apps.common.utils import is_ajax
-from hi.apps.entity.views import EntityDetailsView
-from hi.apps.location.edit.views import LocationViewAddRemoveItemView
+from hi.apps.entity.async_views import EntityDetailsView
+from hi.apps.location.edit.forms import (
+ LocationAttributeFormset,
+ LocationEditForm,
+ LocationSvgFileForm,
+ LocationViewEditForm,
+)
+from hi.apps.location.edit.async_views import LocationViewManageItemsView
from hi.constants import DIVID
from hi.enums import ItemType, ViewType
+from hi.exceptions import ForceRedirectException
from hi.hi_grid_view import HiGridView
from hi.views import bad_request_response, page_not_found_response
@@ -22,31 +29,6 @@ from .models import Location, LocationView
logger = logging.getLogger(__name__)
-class LocationSwitchView( View ):
-
- def get(self, request, *args, **kwargs):
-
- location_id = kwargs.get('location_id')
- try:
- location = Location.objects.get( id = location_id )
- except Location.DoesNotExist:
- return page_not_found_response( request )
-
- location_view = location.views.order_by( 'order_id' ).first()
- if not location_view:
- return bad_request_response( request, message = 'No views defined for this location.' )
-
- 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(
- 'location_view',
- kwargs = { 'id': location_view.id }
- )
- return HttpResponseRedirect( redirect_url )
-
-
class LocationViewDefaultView( View ):
def get(self, request, *args, **kwargs):
@@ -82,17 +64,22 @@ class LocationViewDefaultView( View ):
class LocationViewView( HiGridView ):
- def get(self, request, *args, **kwargs):
+ def get_main_template_name( self ) -> str:
+ return 'location/location_view.html'
+
+ def get_template_context( self, request, *args, **kwargs ):
location_view_id = kwargs.get('id')
try:
location_view = LocationView.objects.select_related(
'location' ).get( id = location_view_id )
except LocationView.DoesNotExist:
- return page_not_found_response( request )
+ raise Http404()
# 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.location_view_id = location_view.id
request.view_parameters.to_session( request )
@@ -102,92 +89,42 @@ class LocationViewView( HiGridView ):
# views are invalidated. Else, the editing state and edit side
# panel will be invalid for the new view.
#
- if view_type_changed and is_ajax( request ):
- sync_url = reverse( 'location_view', kwargs = kwargs )
- return antinode.redirect_response( url = sync_url )
+ 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,
)
- context = {
+ return {
'is_async_request': is_ajax( request ),
'location_view': location_view,
'location_view_data': location_view_data,
}
-
- side_template_name = None
- if request.is_editing:
- context.update(
- LocationViewAddRemoveItemView.get_add_remove_template_context(
- location_view = location_view,
- )
- )
- side_template_name = 'edit/panes/side.html'
-
- return self.hi_grid_response(
- request = request,
- context = context,
- main_template_name = 'location/location_view.html',
- side_template_name = side_template_name,
- push_url_name = 'location_view',
- push_url_kwargs = kwargs,
- )
-class LocationItemDetailsView( HiGridView ):
+class LocationSwitchView( View ):
def get(self, request, *args, **kwargs):
+
+ location_id = kwargs.get('location_id')
try:
- ( item_type, item_id ) = ItemType.parse_from_dict( kwargs )
- except ValueError:
- return bad_request_response( request, message = 'Bad item id.' )
-
- if item_type == ItemType.ENTITY:
- return EntityDetailsView().get(
- request = request,
- entity_id = item_id,
- )
+ location = Location.objects.get( id = location_id )
+ except Location.DoesNotExist:
+ return page_not_found_response( request )
- if item_type == ItemType.COLLECTION:
- return CollectionDetailsView().get(
- request = request,
- collection_id = item_id,
- )
+ location_view = location.views.order_by( 'order_id' ).first()
+ if not location_view:
+ return bad_request_response( request, message = 'No views defined for this location.' )
- return bad_request_response( request, message = 'Unknown item type "{item_type}".' )
+ request.view_parameters.view_type = ViewType.LOCATION_VIEW
+ request.view_parameters.location_view_id = location_view.id
+ request.view_parameters.to_session( request )
-
-class LocationViewDetailsView( View ):
-
- def get( self, request, *args, **kwargs ):
- location_view_id = kwargs.get( 'location_view_id' )
- if location_view_id:
- location_view_id = int(location_view_id)
- default_location_view_id = request.view_parameters.location_view_id
- if default_location_view_id:
- default_location_view_id = int(default_location_view_id)
-
- if not location_view_id and not default_location_view_id:
- return bad_request_response( request, message = 'No location given and no default available.' )
-
- if location_view_id and ( location_view_id != default_location_view_id ):
- try:
- location_view = LocationView.objects.get( id = location_view_id )
- except LocationView.DoesNotExist:
- return page_not_found_response( request )
- else:
- location_view = request.view_parameters.location_view
-
- location_detail_data = LocationManager().get_location_detail_data(
- location_view = location_view,
+ redirect_url = reverse(
+ 'location_view',
+ kwargs = { 'id': location_view.id }
)
- context = {
- 'location_detail_data': location_detail_data,
- }
- template = get_template( 'location/panes/location_details.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['EDIT_ITEM']: content,
- },
- )
+ return HttpResponseRedirect( redirect_url )
diff --git a/src/hi/constants.py b/src/hi/constants.py
index ee14dff5..0cf6e19a 100644
--- a/src/hi/constants.py
+++ b/src/hi/constants.py
@@ -11,8 +11,6 @@ DIVID = {
# Config
'INTEGRATION_TAB': 'hi-config-integration-tab',
- # Editing
- 'EDIT_SIDE': 'hi-edit-side-content',
- 'EDIT_ITEM': 'hi-edit-item',
-
+ 'LOCATION_EDIT_PANE': 'hi-location-edit',
+ 'LOCATION_VIEW_EDIT_PANE': 'hi-location-view-edit',
}
diff --git a/src/hi/exceptions.py b/src/hi/exceptions.py
new file mode 100644
index 00000000..3f7925f3
--- /dev/null
+++ b/src/hi/exceptions.py
@@ -0,0 +1,10 @@
+class ForceRedirectException( Exception ):
+
+ def __init__(self, url, message = f'Force redirect' ):
+ self._url = url
+ super().__init__(message)
+ return
+
+ @property
+ def url(self):
+ return self._url
diff --git a/src/hi/hi_async_view.py b/src/hi/hi_async_view.py
new file mode 100644
index 00000000..1817ee87
--- /dev/null
+++ b/src/hi/hi_async_view.py
@@ -0,0 +1,83 @@
+import logging
+from typing import Dict
+import urllib.parse
+
+from django.template.loader import get_template
+from django.views.generic import View
+
+import hi.apps.common.antinode as antinode
+
+from hi.constants import DIVID
+
+logger = logging.getLogger(__name__)
+
+
+class HiAsyncView( View ):
+
+ def get_target_div_id( self ) -> str:
+ raise NotImplementedError('Subclasses must override this method.')
+
+ def get_template_name( self ) -> str:
+ 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. """
+ raise NotImplementedError('Subclasses must override this method.')
+
+ def get_content( self, request, *args, **kwargs ) -> str:
+ template_name = self.get_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 ):
+ div_id = self.get_target_div_id()
+ content = self.get_content( request, *args, **kwargs )
+ return antinode.response(
+ insert_map = { div_id: content },
+ )
+
+
+class HiSideView( HiAsyncView ):
+
+ def should_push_url( self ):
+ """
+ Subclasses can override this if they want full page refresh to retain
+ the view in the side page.
+ """
+ return False
+
+ def get_target_div_id( self ) -> str:
+ return DIVID['SIDE']
+
+ def get( self, request, *args, **kwargs ):
+ div_id = self.get_target_div_id()
+ content = self.get_content( request, *args, **kwargs )
+ push_url = self.get_push_url( request )
+ return antinode.response(
+ insert_map = { div_id: content },
+ push_url = push_url,
+ )
+
+ def get_push_url( self, request ):
+
+ referrer_url_str = request.META.get('HTTP_REFERER', '')
+ if not referrer_url_str:
+ return None
+
+ side_url = request.path
+ referrer_url = urllib.parse.urlparse( referrer_url_str )
+ referrer_query_params = urllib.parse.parse_qs( referrer_url.query )
+ if self.should_push_url():
+ referrer_query_params['details'] = side_url
+ else:
+ del referrer_query_params['details']
+
+ updated_query_string = urllib.parse.urlencode( referrer_query_params )
+ return f"{referrer_url.path}?{updated_query_string}"
+
+
+class HiModalView( View ):
+
+ def get_modal( self, request, *args, **kwargs ) -> str:
+ raise NotImplementedError('Subclasses must override this method.')
diff --git a/src/hi/hi_grid_view.py b/src/hi/hi_grid_view.py
index ac5ca4da..ebbbb4b4 100644
--- a/src/hi/hi_grid_view.py
+++ b/src/hi/hi_grid_view.py
@@ -1,135 +1,180 @@
import logging
from typing import Dict
+import urllib.parse
-from django.http import HttpRequest
from django.shortcuts import redirect, render
-from django.template import Context
from django.template.loader import get_template
-from django.urls import NoReverseMatch
-from django.urls import reverse
+from django.urls import resolve, reverse
from django.views.generic import View
import hi.apps.common.antinode as antinode
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.collection.models import Collection
from hi.apps.location.models import Location
from hi.constants import DIVID
+from hi.exceptions import ForceRedirectException
+from hi.hi_async_view import HiSideView
logger = logging.getLogger(__name__)
-
+
class HiGridView(View):
- """
- - Helpers for views that use the main "HI Grid View" layout.
- - The HI Grid view layout has the top and bottom fixed rows (usually buttons)
- and main panel and a side panel.
- - Helps to populate areas asynchronously, but can handle sychconous requests too.
- - Good for views that populate all or just some of the tiled areas of the HIU Grid.
- - Main HI Grid layout defined in the template: pages/hi_grid.html
- """
-
+ # ZZZ Add description and how it works
+
HI_GRID_TEMPLATE_NAME = 'pages/home.html'
TOP_TEMPLATE_NAME = 'panes/top_buttons.html'
BOTTOM_TEMPLATE_NAME = 'panes/bottom_buttons.html'
- MAIN_TEMPLATE_NAME = 'panes/main_default.html'
- SIDE_TEMPLATE_NAME = 'panes/side_panel.html'
-
- def hi_grid_response( self,
- request : HttpRequest,
- context : Context,
- top_template_name : str = None,
- bottom_template_name : str = None,
- main_template_name : str = None,
- side_template_name : str = None,
- push_url_name : str = None,
- push_url_kwargs : Dict[ str, str ] = None ):
- if not context:
- context = dict()
+ SIDE_DEFAULT_TEMPLATE_NAME = 'panes/side.html'
+
+ def get_main_template_name( self ) -> str:
+ 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. """
+ raise NotImplementedError('Subclasses must override this method.')
+
+ def get_top_template_name( self ) -> str:
+ return self.TOP_TEMPLATE_NAME
+
+ def get_bottom_template_name( self ) -> str:
+ return self.BOTTOM_TEMPLATE_NAME
+
+ def get_top_template_context( self, request, *args, **kwargs ):
+ if not request.view_parameters.location:
+ return dict()
+ location_view_list = list( request.view_parameters.location.views.order_by( 'order_id' ))
+ return { 'location_view_list': location_view_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):
current_location = request.view_parameters.location
if not current_location:
redirect_url = reverse('start')
if is_ajax( request ):
- antinode.redirect( redirect_url )
+ return 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.
- if not top_template_name:
- top_template_name = self.TOP_TEMPLATE_NAME
- if not bottom_template_name:
- bottom_template_name = self.BOTTOM_TEMPLATE_NAME
- if not main_template_name:
- main_template_name = self.MAIN_TEMPLATE_NAME
- if not side_template_name:
- side_template_name = self.SIDE_TEMPLATE_NAME
- context.update({
- 'top_template_name': top_template_name,
- 'bottom_template_name': bottom_template_name,
- 'main_template_name': main_template_name,
- 'side_template_name': side_template_name,
- })
-
- # This list of views is needed for top buttons
- if ( 'location_view_list' not in context ):
- 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 buttons
- if '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 = {
- DIVID['TOP'] : top_template_name,
- DIVID['BOTTOM'] : bottom_template_name,
- DIVID['MAIN'] : main_template_name,
- DIVID['SIDE'] : side_template_name,
- }
- insert_map = dict()
- for div_id, template_name in div_id_to_template_name.items():
- if not template_name:
- continue
- template = get_template( template_name )
- content = template.render( context, request = request )
- insert_map[div_id] = content,
- continue
-
- push_url = None
- if push_url_name:
- try:
- push_url = reverse( push_url_name, kwargs = push_url_kwargs )
- except NoReverseMatch:
- logger.warning( f'No reverse url for {push_url_name}' )
- pass
+ if is_ajax( request ):
+ return self.get_async_response( request, *args, **kwargs )
+
+ try:
+ context = self.get_template_context( request, *args, **kwargs )
+ except ForceRedirectException as fde:
+ return redirect( fde.url )
+
+ ( side_template_name,
+ side_template_context ) = self.get_side_template_name_and_context( request, *args, **kwargs )
+
+ context.update( side_template_context )
+ context.update({
+ 'top_template_name': self.get_top_template_name(),
+ 'bottom_template_name': self.get_bottom_template_name(),
+ 'main_template_name': self.get_main_template_name(),
+ 'side_template_name': side_template_name,
+ })
+ 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 )
+
+ insert_map = { DIVID['MAIN']: main_content }
+ push_url = self.get_push_url( request )
+
return antinode.response(
insert_map = insert_map,
push_url = push_url,
)
- def side_panel_response( self,
- request : HttpRequest,
- template_name : str,
- context : Dict = None):
- if context is None:
- context = dict()
- template = get_template( template_name )
- content = template.render( context, request = request )
- insert_map = {
- DIVID['EDIT_ITEM']: content,
- }
- return antinode.response(
- insert_map = insert_map,
- )
+ def get_push_url( self, request ):
+ referrer_url = request.META.get('HTTP_REFERER', '')
+ if referrer_url:
+ parsed_url = urllib.parse.urlparse( referrer_url )
+ query_params = urllib.parse.parse_qs( parsed_url.query )
+ new_details_value = query_params.get( 'details', [ None ] )[0]
+ else:
+ new_details_value = None
+
+ if new_details_value:
+ full_url = request.get_full_path()
+ parsed_url = urllib.parse.urlparse( full_url )
+ query_params = urllib.parse.parse_qs(parsed_url.query)
+ query_params['details'] = [ new_details_value ]
+ new_query_string = urllib.parse.urlencode(query_params, doseq=True)
+ push_url = urllib.parse.urlunparse((
+ parsed_url.path,
+ parsed_url.params,
+ new_query_string,
+ parsed_url.fragment
+ ))
+ else:
+ push_url = request.get_full_path()
+
+ return push_url
-
-
+ def get_side_template_name_and_context( self, request, *args, **kwargs ):
+ try:
+ side_url = self.get_side_url_from_request_url( request )
+ side_view, side_kwargs = self.get_side_view_from_url( side_url )
+
+ if not side_view:
+ if request.is_editing:
+ if request.view_parameters.view_type.is_location_view:
+ side_view = LocationViewManageItemsView()
+
+ elif request.view_parameters.view_type.is_collection:
+ side_view = CollectionManageItemsView()
+
+ if not side_view:
+ return ( self.SIDE_DEFAULT_TEMPLATE_NAME, dict() )
+
+ if not isinstance( side_view, HiSideView ):
+ raise ValueError( f'Side URL has view not side class: {side_view.__class__.__name__}' )
+
+ template_name = side_view.get_template_name()
+ template_context = side_view.get_template_context( request, **side_kwargs )
+ return ( template_name, template_context )
+
+ except Exception as e:
+ logger.exception( e )
+ return ( self.SIDE_DEFAULT_TEMPLATE_NAME, dict() )
+
+ def get_side_url_from_request_url( self, request ):
+
+ full_url = request.get_full_path()
+ parsed_url = urllib.parse.urlparse( full_url )
+ query_params = urllib.parse.parse_qs( parsed_url.query )
+ side_url = query_params.get( 'details', [ None ] )[0]
+ return side_url
+
+ def get_side_view_from_url( self, url ):
+ if not url:
+ return ( None, dict() )
+ resolved_match = resolve( url )
+ view_kwargs = resolved_match.kwargs
+ view_class = resolved_match.func.view_class
+ view = view_class()
+ return ( view, view_kwargs )
diff --git a/src/hi/static/js/main.js b/src/hi/static/js/main.js
index c6c3c60f..c7c64c3c 100644
--- a/src/hi/static/js/main.js
+++ b/src/hi/static/js/main.js
@@ -15,7 +15,7 @@
DATA_TYPE_PATH_VALUE: 'svg-path',
API_LOCATION_ITEM_DETAILS_URL: '/location/item/details',
API_LOCATION_DETAILS_URL: '/location/details',
-
+
generateUniqueId: function() {
return _generateUniqueId();
},
diff --git a/src/hi/static/js/svg-location.js b/src/hi/static/js/svg-location.js
index 7bfc231f..57834777 100644
--- a/src/hi/static/js/svg-location.js
+++ b/src/hi/static/js/svg-location.js
@@ -170,7 +170,7 @@
}
function handleKeyDown( event ) {
- if ( $(event.target).is('input[type="text"], textarea') ) {
+ if ( $(event.target).is('input, textarea') ) {
return;
}
if ( gSelectedLocationViewSvg ) {
diff --git a/src/hi/templates/panes/main_default.html b/src/hi/templates/panes/main_default.html
deleted file mode 100644
index e3984e54..00000000
--- a/src/hi/templates/panes/main_default.html
+++ /dev/null
@@ -1,3 +0,0 @@
-<div class="" >
- TBD - DEFAULT MAIN CONTENT
-</div>
diff --git a/src/hi/templates/panes/side_default.html b/src/hi/templates/panes/side.html
similarity index 100%
rename from src/hi/templates/panes/side_default.html
rename to src/hi/templates/panes/side.html
diff --git a/src/hi/templates/panes/side_panel.html b/src/hi/templates/panes/side_panel.html
deleted file mode 100644
index f3045903..00000000
--- a/src/hi/templates/panes/side_panel.html
+++ /dev/null
@@ -1,5 +0,0 @@
-{% if request.is_editing %}
-{% include "edit/panes/side.html" %}
-{% else %}
-{% include "panes/side_default.html" %}
-{% endif %}