Skip to content

Home Information - Your home, your data, organized.

Python

Added user choice about adding new collection to location view.

Anthony Cassandraf9b562caa546
diff --git a/src/hi/apps/collection/edit/forms.py b/src/hi/apps/collection/edit/forms.py
index 484528ee..77b07605 100644
--- a/src/hi/apps/collection/edit/forms.py
+++ b/src/hi/apps/collection/edit/forms.py
@@ -5,7 +5,7 @@ from hi.apps.collection.models import Collection, CollectionPosition
from hi.apps.location.edit.forms import LocationItemPositionForm
-class CollectionForm( forms.ModelForm ):
+class CollectionBaseForm( forms.ModelForm ):
class Meta:
model = Collection
@@ -32,7 +32,26 @@ class CollectionForm( forms.ModelForm ):
widget = forms.Select( attrs = { 'class' : 'custom-select' } ),
)
-
+
+class CollectionEditForm( CollectionBaseForm ):
+ pass
+
+
+class CollectionAddForm( CollectionBaseForm ):
+
+ include_in_location_view = forms.BooleanField(
+ label = 'Include in Current Location View?',
+ required = False,
+ initial = False,
+ )
+
+ def __init__(self, *args, **kwargs):
+ include_in_location_view = kwargs.pop( 'include_in_location_view', False )
+ super().__init__(*args, **kwargs)
+ self.fields['include_in_location_view'].initial = include_in_location_view
+ return
+
+
class CollectionPositionForm( LocationItemPositionForm ):
class Meta( LocationItemPositionForm.Meta ):
model = CollectionPosition
diff --git a/src/hi/apps/collection/edit/urls.py b/src/hi/apps/collection/edit/urls.py
index 1369b6c3..77e27781 100644
--- a/src/hi/apps/collection/edit/urls.py
+++ b/src/hi/apps/collection/edit/urls.py
@@ -7,7 +7,7 @@ urlpatterns = [
re_path( r'^collection/add$',
views.CollectionAddView.as_view(),
- name='collection_edit_collection_add' ),
+ name='collection_add' ),
re_path( r'^collection/edit/(?P<collection_id>\d+)$',
views.CollectionEditView.as_view(),
diff --git a/src/hi/apps/collection/edit/views.py b/src/hi/apps/collection/edit/views.py
index e8e0d75f..5fb32af3 100644
--- a/src/hi/apps/collection/edit/views.py
+++ b/src/hi/apps/collection/edit/views.py
@@ -36,22 +36,31 @@ class CollectionAddView( HiModalView ):
return 'collection/edit/modals/collection_add.html'
def get( self, request, *args, **kwargs ):
+
+ include_in_location_view = False
+ if request.view_parameters.view_type == ViewType.LOCATION_VIEW:
+ include_in_location_view = True
+
context = {
- 'collection_form': forms.CollectionForm(),
+ 'collection_add_form': forms.CollectionAddForm(
+ include_in_location_view = include_in_location_view,
+ ),
}
return self.modal_response( request, context )
def post( self, request, *args, **kwargs ):
- collection_form = forms.CollectionForm( request.POST )
- if not collection_form.is_valid():
+
+ collection_add_form = forms.CollectionAddForm( request.POST )
+ if not collection_add_form.is_valid():
context = {
- 'collection_form': collection_form,
+ 'collection_add_form': collection_add_form,
}
return self.modal_response( request, context )
+ cleaneds_data = collection_add_form.clean()
with transaction.atomic():
- collection = collection_form.save()
- if request.view_parameters.view_type == ViewType.LOCATION_VIEW:
+ collection = collection_add_form.save()
+ if cleaneds_data['include_in_location_view']:
self._add_to_location_view(
request = request,
collection = collection,
@@ -114,16 +123,16 @@ class CollectionEditView( View, CollectionViewMixin ):
def post( self, request, *args, **kwargs ):
collection = self.get_collection( request, *args, **kwargs )
- collection_form = forms.CollectionForm( request.POST, instance = collection )
- if collection_form.is_valid():
- collection_form.save()
+ collection_edit_form = forms.CollectionEditForm( request.POST, instance = collection )
+ if collection_edit_form.is_valid():
+ collection_edit_form.save()
# Change can impact other parts of UI. e.g., Collection name shows on bottom of screen
return antinode.refresh_response()
# On error, show form errors
collection_edit_data = CollectionEditData(
collection = collection,
- collection_form = collection_form,
+ collection_edit_form = collection_edit_form,
)
return self.collection_edit_response(
request = request,
diff --git a/src/hi/apps/collection/templates/collection/edit/modals/collection_add.html b/src/hi/apps/collection/templates/collection/edit/modals/collection_add.html
index 533deb9b..263fe306 100644
--- a/src/hi/apps/collection/templates/collection/edit/modals/collection_add.html
+++ b/src/hi/apps/collection/templates/collection/edit/modals/collection_add.html
@@ -1,7 +1,7 @@
{% extends "modals/action_with_cancel.html" %}
{% block preamble %}
-<form action="{% url 'collection_edit_collection_add' %}" method="post"
+<form action="{% url 'collection_add' %}" method="post"
class="form" data-async="modal">
{% csrf_token %}
{% endblock %}
@@ -12,7 +12,14 @@ New Collection
{% block body %}
<div>
-{% include "collection/edit/panes/collection_form.html" %}
+{% include "collection/edit/panes/collection_form.html" with collection_form=collection_add_form %}
+<p>
+ <div class="form-check">
+ {{ collection_add_form.include_in_location_view }}
+ {{ collection_add_form.include_in_location_view.label }}
+ </div>
+</p>
+
</div>
{% endblock %}
diff --git a/src/hi/apps/collection/templates/collection/edit/panes/collection_edit.html b/src/hi/apps/collection/templates/collection/edit/panes/collection_edit.html
index b6bd4463..3078b90a 100644
--- a/src/hi/apps/collection/templates/collection/edit/panes/collection_edit.html
+++ b/src/hi/apps/collection/templates/collection/edit/panes/collection_edit.html
@@ -4,7 +4,7 @@
{% csrf_token %}
<div>
- {% include "collection/edit/panes/collection_form.html" %}
+ {% include "collection/edit/panes/collection_form.html" with collection_form=collection_edit_form %}
</div>
<div class="text-center" >
<button id="hi-add-collection-edit-button" type="submit" class="btn btn-primary"
diff --git a/src/hi/apps/collection/templates/collection/panes/collection_buttons.html b/src/hi/apps/collection/templates/collection/panes/collection_buttons.html
index e98756e3..2e2c43f6 100644
--- a/src/hi/apps/collection/templates/collection/panes/collection_buttons.html
+++ b/src/hi/apps/collection/templates/collection/panes/collection_buttons.html
@@ -15,7 +15,7 @@
{% endfor %}
{% if request.view_parameters.is_editing %}
<div class="ml-3">
- <a href="{% url 'collection_edit_collection_add' %}"
+ <a href="{% url 'collection_add' %}"
class="btn btn-success btn-control" role="button"
data-async="modal" >
ADD
diff --git a/src/hi/apps/collection/transient_models.py b/src/hi/apps/collection/transient_models.py
index 3ba69628..2f6104ec 100644
--- a/src/hi/apps/collection/transient_models.py
+++ b/src/hi/apps/collection/transient_models.py
@@ -1,7 +1,7 @@
from dataclasses import dataclass, field
from typing import List
-from hi.apps.collection.edit.forms import CollectionForm, CollectionPositionForm
+from hi.apps.collection.edit.forms import CollectionEditForm, CollectionPositionForm
from hi.apps.entity.enums import EntityGroupType
from hi.apps.entity.models import Entity
from hi.apps.monitor.transient_models import EntityStatusData
@@ -54,13 +54,13 @@ class EntityCollectionGroup:
class CollectionEditData:
""" All the data needed to render the Collection edit pane (subset of all collection details). """
- collection : Collection
- collection_form : CollectionForm = None
+ collection : Collection
+ collection_edit_form : CollectionEditForm = None
def __post_init__(self):
- if not self.collection_form:
- self.collection_form = CollectionForm(
+ if not self.collection_edit_form:
+ self.collection_edit_form = CollectionEditForm(
instance = self.collection,
)
return
@@ -68,7 +68,7 @@ class CollectionEditData:
def to_template_context(self):
return {
'collection': self.collection,
- 'collection_form': self.collection_form,
+ 'collection_edit_form': self.collection_edit_form,
}