Skip to content

Home Information - Your home, your data, organized.

Python

Added collection type dropdown to new collection form.

Anthony Cassandra09442485bdb6
diff --git a/src/hi/apps/collection/edit/forms.py b/src/hi/apps/collection/edit/forms.py
new file mode 100644
index 00000000..5b39b7f6
--- /dev/null
+++ b/src/hi/apps/collection/edit/forms.py
@@ -0,0 +1,31 @@
+from django import forms
+
+from hi.apps.collection.enums import CollectionType
+
+
+class CollectionForm(forms.Form):
+
+ name = forms.CharField(
+ label = 'Name',
+ max_length = 64,
+ required = False,
+ )
+
+ collection_type = forms.ChoiceField(
+ label = 'Collection Type',
+ choices = CollectionType.choices,
+ initial = CollectionType.default_value(),
+ required = True,
+ widget = forms.Select( attrs = { 'class' : 'custom-select' } ),
+ )
+
+ def clean(self):
+ cleaned_data = super().clean()
+
+ name = cleaned_data.get( 'name', '' )
+ if not name:
+ collection_type_str = cleaned_data.get( 'collection_type' )
+ collection_type = CollectionType.from_name_safe( collection_type_str )
+ cleaned_data['name'] = collection_type.label
+
+ return cleaned_data
diff --git a/src/hi/apps/collection/edit/helpers.py b/src/hi/apps/collection/edit/helpers.py
index 59971fd4..6e375037 100644
--- a/src/hi/apps/collection/edit/helpers.py
+++ b/src/hi/apps/collection/edit/helpers.py
@@ -21,13 +21,14 @@ class CollectionEditHelpers:
@classmethod
def create_collection( cls,
- request : HttpRequest,
- name : str ) -> Collection:
+ request : HttpRequest,
+ collection_type : CollectionType,
+ name : str ) -> Collection:
last_collection = Collection.objects.all().order_by( '-order_id' ).first()
return Collection.objects.create(
name = name,
- collection_type_str = CollectionType.default(),
+ collection_type_str = str(collection_type),
order_id = last_collection.order_id + 1,
)
diff --git a/src/hi/apps/collection/edit/views.py b/src/hi/apps/collection/edit/views.py
index 9b01185f..ba4a0551 100644
--- a/src/hi/apps/collection/edit/views.py
+++ b/src/hi/apps/collection/edit/views.py
@@ -10,7 +10,6 @@ import hi.apps.common.antinode as antinode
from hi.apps.collection.collection_manager import CollectionManager
from hi.apps.collection.enums import CollectionType
from hi.apps.collection.models import Collection, CollectionPosition
-from hi.apps.edit.forms import NameForm
from hi.apps.entity.models import Entity
from hi.apps.location.forms import SvgPositionForm
from hi.decorators import edit_required
@@ -19,6 +18,7 @@ from hi.views import bad_request_response, page_not_found_response
from hi.constants import DIVID
+from . import forms
from .helpers import CollectionEditHelpers
logger = logging.getLogger(__name__)
@@ -29,7 +29,7 @@ class CollectionAddView( View ):
def get( self, request, *args, **kwargs ):
context = {
- 'name_form': NameForm(),
+ 'collection_form': forms.CollectionForm(),
}
return antinode.modal_from_template(
request = request,
@@ -38,10 +38,10 @@ class CollectionAddView( View ):
)
def post( self, request, *args, **kwargs ):
- name_form = NameForm( request.POST )
+ name_form = forms.CollectionForm( request.POST )
if not name_form.is_valid():
context = {
- 'name_form': name_form,
+ 'collection_form': name_form,
}
return antinode.modal_from_template(
request = request,
@@ -49,10 +49,15 @@ class CollectionAddView( View ):
context = context,
)
+ cleaned_data = name_form.clean()
+ collection_type = CollectionType.from_name_safe( cleaned_data.get('collection_type') )
+ name = cleaned_data.get('name')
+
try:
collection = CollectionEditHelpers.create_collection(
request = request,
- name = name_form.cleaned_data.get('name'),
+ collection_type = collection_type,
+ name = name,
)
except ValueError as e:
return bad_request_response( request, message = str(e) )
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 b0199133..533deb9b 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
@@ -12,7 +12,7 @@ New Collection
{% block body %}
<div>
-{% include "edit/panes/name_form.html" %}
+{% include "collection/edit/panes/collection_form.html" %}
</div>
{% endblock %}
diff --git a/src/hi/apps/collection/templates/collection/edit/panes/collection_form.html b/src/hi/apps/collection/templates/collection/edit/panes/collection_form.html
new file mode 100644
index 00000000..a315550b
--- /dev/null
+++ b/src/hi/apps/collection/templates/collection/edit/panes/collection_form.html
@@ -0,0 +1,28 @@
+{% for hidden in collection_form.hidden_fields %}
+{{ hidden }}
+{% endfor %}
+
+{% if collection_form.non_field_errors %}
+<div class="non-field-errors">
+ {% for err in collection_form.non_field_errors %}
+ <p class="form-error text-danger">ERROR: {{ err }}</p>
+ {% endfor %}
+</div>
+{% endif %}
+
+<div>
+ <div class="input-group mb-2 mr-sm-2">
+ <div class="input-group-prepend">
+ <div class="input-group-text">{{ collection_form.collection_type.label_tag }}</div>
+ </div>
+ {{ collection_form.collection_type }}
+ </div>
+ {{ ollection_form.collection_type.errors }}
+</div>
+
+<div>
+ {{ collection_form.name.label }}
+ {{ collection_form.name }}
+ {{ collection_form.name.errors }}
+</div>
+
diff --git a/src/hi/apps/edit/forms.py b/src/hi/apps/edit/forms.py
index 0f396ee9..e79048bf 100644
--- a/src/hi/apps/edit/forms.py
+++ b/src/hi/apps/edit/forms.py
@@ -1,6 +1 @@
from django import forms
-
-
-class NameForm(forms.Form):
-
- name = forms.CharField()
diff --git a/src/hi/apps/edit/templates/edit/panes/name_form.html b/src/hi/apps/edit/templates/edit/panes/name_form.html
deleted file mode 100644
index 7e03a7ad..00000000
--- a/src/hi/apps/edit/templates/edit/panes/name_form.html
+++ /dev/null
@@ -1,17 +0,0 @@
-{% for hidden in name_form.hidden_fields %}
-{{ hidden }}
-{% endfor %}
-
-{% if name_form.non_field_errors %}
-<div class="non-field-errors">
- {% for err in name_form.non_field_errors %}
- <p class="form-error text-danger">ERROR: {{ err }}</p>
- {% endfor %}
-</div>
-{% endif %}
-
-<div>
- {{ name_form.name.label }}
- {{ name_form.name }}
- {{ name_form.name.errors }}
-</div>
diff --git a/src/hi/apps/location/edit/forms.py b/src/hi/apps/location/edit/forms.py
new file mode 100644
index 00000000..09c5a89a
--- /dev/null
+++ b/src/hi/apps/location/edit/forms.py
@@ -0,0 +1,6 @@
+from django import forms
+
+
+class LocationViewForm(forms.Form):
+
+ name = forms.CharField()
diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.py
index 00efce99..97cfe13e 100644
--- a/src/hi/apps/location/edit/views.py
+++ b/src/hi/apps/location/edit/views.py
@@ -8,7 +8,6 @@ from django.views.generic import View
from hi.apps.collection.models import Collection
import hi.apps.common.antinode as antinode
-from hi.apps.edit.forms import NameForm
from hi.apps.entity.models import Entity
from hi.apps.location.location_view_manager import LocationViewManager
from hi.apps.location.models import LocationView
@@ -18,6 +17,7 @@ from hi.views import bad_request_response
from hi.constants import DIVID
+from . import forms
from .helpers import LocationEditHelpers
@@ -29,7 +29,7 @@ class LocationViewAddView( View ):
def get( self, request, *args, **kwargs ):
context = {
- 'name_form': NameForm(),
+ 'location_view_form': forms.LocationViewForm(),
}
return antinode.modal_from_template(
request = request,
@@ -38,10 +38,10 @@ class LocationViewAddView( View ):
)
def post( self, request, *args, **kwargs ):
- name_form = NameForm( request.POST )
+ name_form = forms.LocationViewForm( request.POST )
if not name_form.is_valid():
context = {
- 'name_form': name_form,
+ 'location_view_form': name_form,
}
return antinode.modal_from_template(
request = request,
diff --git a/src/hi/apps/location/templates/location/edit/modals/location_view_add.html b/src/hi/apps/location/templates/location/edit/modals/location_view_add.html
index adafb45f..b9598698 100644
--- a/src/hi/apps/location/templates/location/edit/modals/location_view_add.html
+++ b/src/hi/apps/location/templates/location/edit/modals/location_view_add.html
@@ -12,7 +12,7 @@ New Location View
{% block body %}
<div>
-{% include "edit/panes/name_form.html" %}
+{% include "location/edit/panes/location_view_form.html" %}
</div>
{% endblock %}
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_view_form.html b/src/hi/apps/location/templates/location/edit/panes/location_view_form.html
new file mode 100644
index 00000000..3861faa7
--- /dev/null
+++ b/src/hi/apps/location/templates/location/edit/panes/location_view_form.html
@@ -0,0 +1,17 @@
+{% for hidden in location_view_form.hidden_fields %}
+{{ hidden }}
+{% endfor %}
+
+{% if location_view_form.non_field_errors %}
+<div class="non-field-errors">
+ {% for err in location_view_form.non_field_errors %}
+ <p class="form-error text-danger">ERROR: {{ err }}</p>
+ {% endfor %}
+</div>
+{% endif %}
+
+<div>
+ {{ location_view_form.name.label }}
+ {{ location_view_form.name }}
+ {{ location_view_form.name.errors }}
+</div>