Skip to content

Home Information - Your home, your data, organized.

Python

Refactored some views to remove redundancies.

Anthony Cassandra38465ef6de36
diff --git a/src/hi/apps/entity/edit/async_views.py b/src/hi/apps/entity/edit/async_views.py
index 71130b11..d64085be 100644
--- a/src/hi/apps/entity/edit/async_views.py
+++ b/src/hi/apps/entity/edit/async_views.py
@@ -36,6 +36,7 @@ class EntityEditView( View, EntityViewMixin ):
with transaction.atomic():
entity_form.save()
entity_attribute_formset.save()
+
# Recreate to preserve "max" to show new form
entity_attribute_formset = forms.EntityAttributeFormSet(
instance = entity,
@@ -45,23 +46,12 @@ class EntityEditView( View, EntityViewMixin ):
else:
status_code = 400
- entity_attribute_upload_form = forms.EntityAttributeUploadForm(
- request.POST,
- instance = entity,
- )
- context = {
- 'entity': entity,
- 'entity_form': entity_form,
- 'entity_attribute_formset': entity_attribute_formset,
- 'entity_attribute_upload_form': entity_attribute_upload_form,
- }
- template = get_template( 'entity/edit/panes/entity_edit.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['ENTITY_EDIT_PANE']: content,
- },
- status = status_code,
+ return self.entity_edit_response(
+ request = request,
+ entity= entity,
+ entity_form = entity_form,
+ entity_attribute_formset = entity_attribute_formset,
+ status_code = status_code,
)
@@ -84,29 +74,12 @@ class EntityAttributeUploadView( View, EntityViewMixin ):
else:
status_code = 400
- entity_form = forms.EntityForm( instance = entity )
- entity_attribute_formset = forms.EntityAttributeFormSet(
- instance = entity,
- prefix = f'entity-{entity.id}',
- form_kwargs = {
- 'show_as_editable': True,
- },
+ return self.entity_edit_response(
+ request = request,
+ entity= entity,
+ entity_attribute_upload_form = entity_attribute_upload_form,
+ status_code = status_code,
)
-
- context = {
- 'entity': entity,
- 'entity_form': entity_form,
- 'entity_attribute_formset': entity_attribute_formset,
- 'entity_attribute_upload_form': entity_attribute_upload_form,
- }
- template = get_template( 'entity/edit/panes/entity_edit.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['ENTITY_EDIT_PANE']: content,
- },
- status = status_code,
- )
@method_decorator( edit_required, name='dispatch' )
diff --git a/src/hi/apps/entity/templates/entity/edit/panes/entity_edit.html b/src/hi/apps/entity/templates/entity/edit/panes/entity_edit.html
index 6ff7a1b2..addcccb4 100644
--- a/src/hi/apps/entity/templates/entity/edit/panes/entity_edit.html
+++ b/src/hi/apps/entity/templates/entity/edit/panes/entity_edit.html
@@ -1,6 +1,6 @@
<form action="{% url 'entity_edit' entity_id=entity.id %}"
method="post"
- class="form" data-async="#{{ DIVID.ENTITY_EDIT_PANE }}">
+ class="form" data-async="true">
{% csrf_token %}
<div>
@@ -15,7 +15,7 @@
<form action="{% url 'entity_attribute_upload' entity_id=entity.id %}"
method="post"
- class="form" data-async="#{{ DIVID.ENTITY_EDIT_PANE }}">
+ class="form" data-async="true">
{% csrf_token %}
<div>
{% include "attribute/panes/attribute_upload_form.html" with attribute_upload_form=entity_attribute_upload_form %}
diff --git a/src/hi/apps/entity/view_mixin.py b/src/hi/apps/entity/view_mixin.py
index 543e81b2..4f96efd3 100644
--- a/src/hi/apps/entity/view_mixin.py
+++ b/src/hi/apps/entity/view_mixin.py
@@ -1,8 +1,14 @@
from django.core.exceptions import BadRequest
-from django.http import Http404
+from django.http import Http404, HttpRequest
+from django.template.loader import get_template
+
+import hi.apps.common.antinode as antinode
+import hi.apps.entity.edit.forms as forms
from hi.apps.entity.models import Entity
+from hi.constants import DIVID
+
class EntityViewMixin:
@@ -16,4 +22,44 @@ class EntityViewMixin:
return Entity.objects.get( id = entity_id )
except Entity.DoesNotExist:
raise Http404( request )
+
+ def entity_edit_response( self,
+ request : HttpRequest,
+ entity : Entity,
+ entity_form : forms.EntityAttributeFormSet = None,
+ entity_attribute_formset : forms.EntityAttributeFormSet = None,
+ entity_attribute_upload_form : forms.EntityAttributeUploadForm = None,
+ status_code : int = 200 ):
+
+ if not entity_form:
+ entity_form = forms.EntityForm(
+ instance = entity,
+ )
+ if not entity_attribute_formset:
+ entity_attribute_formset = forms.EntityAttributeFormSet(
+ instance = entity,
+ prefix = f'entity-{entity.id}',
+ form_kwargs = {
+ 'show_as_editable': True,
+ },
+ )
+ if not entity_attribute_upload_form:
+ entity_attribute_upload_form = forms.EntityAttributeUploadForm(
+ instance = entity,
+ )
+
+ context = {
+ 'entity': entity,
+ 'entity_form': entity_form,
+ 'entity_attribute_formset': entity_attribute_formset,
+ 'entity_attribute_upload_form': entity_attribute_upload_form,
+ }
+ template = get_template( 'entity/edit/panes/entity_edit.html' )
+ content = template.render( context, request = request )
+ return antinode.response(
+ insert_map = {
+ DIVID['ENTITY_EDIT_PANE']: content,
+ },
+ status = status_code,
+ )
diff --git a/src/hi/apps/location/edit/async_views.py b/src/hi/apps/location/edit/async_views.py
index 35d34526..b80f0606 100644
--- a/src/hi/apps/location/edit/async_views.py
+++ b/src/hi/apps/location/edit/async_views.py
@@ -48,38 +48,33 @@ class LocationEditView( View, LocationViewMixin ):
'show_as_editable': True,
},
)
-
- location_attribute_upload_form = forms.LocationAttributeUploadForm(
- request.POST,
- instance = location,
- )
- context = {
- 'location': location,
- 'location_edit_form': location_edit_form,
- 'location_attribute_formset': location_attribute_formset,
- 'location_attribute_upload_form': location_attribute_upload_form,
- }
- if ( not location_edit_form.is_valid()
- or not location_attribute_formset.is_valid() ):
+ if ( location_edit_form.is_valid()
+ and location_attribute_formset.is_valid() ):
+ with transaction.atomic():
+ location_edit_form.save()
+ location_attribute_formset.save()
+
+ # Location name/oreder can impact many parts of UI. Full refresh is safest in that case.
+ if location_edit_form.has_changed():
+ return antinode.refresh_response()
+
# Recreate to preserve "max" to show new form
location_attribute_formset = forms.LocationAttributeFormSet(
instance = location,
prefix = f'location-{location.id}',
)
- template = get_template( 'location/edit/panes/location_edit.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['LOCATION_EDIT_PANE']: content,
- },
- )
-
- with transaction.atomic():
- location_edit_form.save()
- location_attribute_formset.save()
-
- return antinode.refresh_response()
+ status_code = 200
+ else:
+ status_code = 400
+
+ return self.location_edit_response(
+ request = request,
+ location = location,
+ location_edit_form = location_edit_form,
+ location_attribute_formset = location_attribute_formset,
+ status_code = status_code,
+ )
@method_decorator( edit_required, name='dispatch' )
@@ -101,33 +96,13 @@ class LocationAttributeUploadView( View, LocationViewMixin ):
else:
status_code = 400
- location_edit_form = forms.LocationEditForm(
- instance = location,
- )
- location_attribute_formset = forms.LocationAttributeFormSet(
- instance = location,
- prefix = f'location-{location.id}',
- form_kwargs = {
- 'show_as_editable': True,
- },
+ return self.location_edit_response(
+ request = request,
+ location = location,
+ location_attribute_upload_form = location_attribute_upload_form,
+ status_code = status_code,
)
-
- context = {
- 'location': location,
- 'location_edit_form': location_edit_form,
- 'location_attribute_formset': location_attribute_formset,
- 'location_attribute_upload_form': location_attribute_upload_form,
- }
-
- template = get_template( 'location/edit/panes/location_edit.html' )
- content = template.render( context, request = request )
- return antinode.response(
- insert_map = {
- DIVID['LOCATION_EDIT_PANE']: content,
- },
- status = status_code,
- )
-
+
@method_decorator( edit_required, name='dispatch' )
class LocationViewManageItemsView( HiSideView ):
diff --git a/src/hi/apps/location/edit/forms.py b/src/hi/apps/location/edit/forms.py
index 90b141d0..1bcaeec0 100644
--- a/src/hi/apps/location/edit/forms.py
+++ b/src/hi/apps/location/edit/forms.py
@@ -64,7 +64,8 @@ class LocationEditForm( forms.ModelForm ):
'name',
'order_id',
)
-
+
+
class LocationAttributeForm( AttributeForm ):
class Meta( AttributeForm.Meta ):
diff --git a/src/hi/apps/location/view_mixin.py b/src/hi/apps/location/view_mixin.py
index 2cbe7571..7e2f3854 100644
--- a/src/hi/apps/location/view_mixin.py
+++ b/src/hi/apps/location/view_mixin.py
@@ -1,9 +1,14 @@
from django.core.exceptions import BadRequest
-from django.http import Http404
+from django.http import Http404, HttpRequest
+from django.template.loader import get_template
+import hi.apps.common.antinode as antinode
+import hi.apps.location.edit.forms as forms
from hi.apps.location.location_manager import LocationManager
from hi.apps.location.models import Location, LocationView
+from hi.constants import DIVID
+
class LocationViewMixin:
@@ -35,3 +40,43 @@ class LocationViewMixin:
except LocationView.DoesNotExist:
raise Http404( request )
+ def location_edit_response( self,
+ request : HttpRequest,
+ location : Location,
+ location_edit_form : forms.LocationEditForm = None,
+ location_attribute_formset : forms.LocationAttributeFormSet = None,
+ location_attribute_upload_form : forms.LocationAttributeUploadForm = None,
+ status_code : int = 200 ):
+
+ if not location_edit_form:
+ location_edit_form = forms.LocationEditForm(
+ instance = location,
+ )
+ if not location_attribute_formset:
+ location_attribute_formset = forms.LocationAttributeFormSet(
+ instance = location,
+ prefix = f'location-{location.id}',
+ form_kwargs = {
+ 'show_as_editable': True,
+ },
+ )
+ if not location_attribute_upload_form:
+ location_attribute_upload_form = forms.LocationAttributeUploadForm(
+ request.POST,
+ instance = location,
+ )
+
+ context = {
+ 'location': location,
+ 'location_edit_form': location_edit_form,
+ 'location_attribute_formset': location_attribute_formset,
+ 'location_attribute_upload_form': location_attribute_upload_form,
+ }
+
+ template = get_template( 'location/edit/panes/location_edit.html' )
+ content = template.render( context, request = request )
+ return antinode.response(
+ insert_map = {
+ DIVID['LOCATION_EDIT_PANE']: content,
+ },
+ )