Skip to content

Home Information - Your home, your data, organized.

Python

Added new editing forms and refactored forms into ModelForms.

Anthony Cassandraa9bfa9a9d685
diff --git a/src/hi/apps/collection/collection_detail_data.py b/src/hi/apps/collection/collection_detail_data.py
index 74fc3591..5aeb522d 100644
--- a/src/hi/apps/collection/collection_detail_data.py
+++ b/src/hi/apps/collection/collection_detail_data.py
@@ -1,6 +1,6 @@
from dataclasses import dataclass
-from hi.apps.collection.edit.forms import CollectionPositionForm
+from hi.apps.collection.edit.forms import CollectionForm, CollectionPositionForm
from .models import Collection
@@ -9,5 +9,6 @@ from .models import Collection
class CollectionDetailData:
collection : Collection
+ collection_form : CollectionForm
collection_position_form : CollectionPositionForm
diff --git a/src/hi/apps/collection/collection_manager.py b/src/hi/apps/collection/collection_manager.py
index ce70e6df..0fae358e 100644
--- a/src/hi/apps/collection/collection_manager.py
+++ b/src/hi/apps/collection/collection_manager.py
@@ -4,7 +4,7 @@ from typing import List
from django.db import transaction
from django.http import HttpRequest
-from hi.apps.collection.edit.forms import CollectionPositionForm
+from hi.apps.collection.edit.forms import CollectionForm, CollectionPositionForm
from hi.apps.common.singleton import Singleton
from hi.apps.entity.models import Entity
from hi.apps.location.models import Location, LocationView
@@ -246,6 +246,7 @@ class CollectionManager(Singleton):
# TODO: Add attributes and other data
return CollectionDetailData(
collection = collection,
+ collection_form = CollectionForm( instance = collection ),
collection_position_form = collection_position_form,
)
diff --git a/src/hi/apps/collection/edit/async_views.py b/src/hi/apps/collection/edit/async_views.py
index 7110db34..ae488355 100644
--- a/src/hi/apps/collection/edit/async_views.py
+++ b/src/hi/apps/collection/edit/async_views.py
@@ -22,6 +22,35 @@ from . import forms
logger = logging.getLogger(__name__)
+class CollectionEditView( View ):
+
+ def post( self, request, *args, **kwargs ):
+
+ collection_id = kwargs.get('collection_id')
+ try:
+ collection = Collection.objects.get( id = collection_id )
+ except Collection.DoesNotExist:
+ raise Http404( request )
+
+ collection_form = forms.CollectionForm( request.POST, instance = collection )
+ if collection_form.is_valid():
+ collection_form.save()
+ return antinode.refresh_response() # Collection name shows on bottom of screen
+
+ # On error, show form errors
+ context = {
+ 'collection': collection,
+ 'collection_form': collection_form,
+ }
+ template = get_template( 'collection/edit/panes/collection_edit.html' )
+ content = template.render( context, request = request )
+ return antinode.response(
+ insert_map = {
+ DIVID['COLLECTION_EDIT_PANE']: content,
+ },
+ )
+
+
@method_decorator( edit_required, name='dispatch' )
class CollectionReorder( View ):
@@ -49,10 +78,11 @@ class CollectionPositionEditView( View ):
location = request.view_parameters.location
try:
collection_position = CollectionPosition.objects.get(
- id = collection_id,
+ collection_id = collection_id,
location = location,
)
- except Collection.DoesNotExist:
+ except CollectionPosition.DoesNotExist:
+ logger.warning( f'Not collection position found for {collection_id} at {location}' )
raise Http404( request )
collection_position_form = forms.CollectionPositionForm(
diff --git a/src/hi/apps/collection/edit/forms.py b/src/hi/apps/collection/edit/forms.py
index 47f2daa1..4a5bd4a7 100644
--- a/src/hi/apps/collection/edit/forms.py
+++ b/src/hi/apps/collection/edit/forms.py
@@ -1,38 +1,28 @@
from django import forms
from hi.apps.collection.enums import CollectionType
-from hi.apps.collection.models import CollectionPosition
+from hi.apps.collection.models import Collection, CollectionPosition
from hi.apps.location.edit.forms import LocationItemPositionForm
-class CollectionForm(forms.Form):
+class CollectionForm( forms.ModelForm ):
- name = forms.CharField(
- label = 'Name',
- max_length = 64,
- required = False,
- )
-
- collection_type = forms.ChoiceField(
+ class Meta:
+ model = Collection
+ fields = (
+ 'name',
+ 'collection_type_str',
+ )
+
+ collection_type_str = 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
-
+
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 1ebd6e37..ffa00aba 100644
--- a/src/hi/apps/collection/edit/urls.py
+++ b/src/hi/apps/collection/edit/urls.py
@@ -10,6 +10,10 @@ urlpatterns = [
views.CollectionAddView.as_view(),
name='collection_edit_collection_add' ),
+ re_path( r'^collection/edit/(?P<collection_id>\d+)$',
+ async_views.CollectionEditView.as_view(),
+ name='collection_edit'),
+
re_path( r'^collection/delete/(?P<collection_id>\d+)$',
views.CollectionDeleteView.as_view(),
name='collection_edit_collection_delete' ),
diff --git a/src/hi/apps/collection/edit/views.py b/src/hi/apps/collection/edit/views.py
index 2280b1f1..711f32b3 100644
--- a/src/hi/apps/collection/edit/views.py
+++ b/src/hi/apps/collection/edit/views.py
@@ -7,7 +7,6 @@ from django.urls import reverse
from django.utils.decorators import method_decorator
from hi.apps.collection.collection_manager import CollectionManager
-from hi.apps.collection.enums import CollectionType
from hi.apps.collection.models import Collection
from hi.decorators import edit_required
@@ -39,17 +38,9 @@ class CollectionAddView( HiModalView ):
}
return self.modal_response( request, context )
- cleaned_data = collection_form.clean()
- collection_type = CollectionType.from_name_safe( cleaned_data.get('collection_type') )
- name = cleaned_data.get('name')
-
try:
with transaction.atomic():
- collection = CollectionManager().create_collection(
- request = request,
- collection_type = collection_type,
- name = name,
- )
+ collection = collection_form.save()
if ( request.view_parameters.view_type.is_location_view
and request.view_parameters.location_view_id ):
CollectionManager().create_collection_view_by_id(
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 350f9ca8..70989c1b 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
@@ -1 +1,14 @@
-COLLECTION EDIT PANE
+<form action="{% url 'collection_edit' collection_id=collection.id %}"
+ method="post"
+ class="form" data-async="#{{ DIVID.COLLECTION_EDIT_PANE }}">
+ {% csrf_token %}
+
+ <div>
+ {% include "collection/edit/panes/collection_form.html" %}
+ </div>
+
+ <button id="hi-add-collection-edit-button" type="submit" class="btn btn-info"
+ name="action" value="update">
+ UPDATE
+ </button>
+</form>
diff --git a/src/hi/apps/collection/templates/collection/edit/panes/collection_edit_form.html b/src/hi/apps/collection/templates/collection/edit/panes/collection_edit_form.html
new file mode 100644
index 00000000..11cf312c
--- /dev/null
+++ b/src/hi/apps/collection/templates/collection/edit/panes/collection_edit_form.html
@@ -0,0 +1,22 @@
+{% 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>
+ {{ collection_form.name.label }}
+ {{ collection_form.name }}
+ {{ collection_form.name.errors }}
+</div>
+<div>
+ {{ collection_form.collection_type_str.label }}
+ {{ collection_form.collection_type_str }}
+ {{ collection_form.collection_type_str.errors }}
+</div>
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
index a315550b..c1f56476 100644
--- a/src/hi/apps/collection/templates/collection/edit/panes/collection_form.html
+++ b/src/hi/apps/collection/templates/collection/edit/panes/collection_form.html
@@ -13,11 +13,11 @@
<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 class="input-group-text">{{ collection_form.collection_type_str.label_tag }}</div>
</div>
- {{ collection_form.collection_type }}
+ {{ collection_form.collection_type_str }}
</div>
- {{ ollection_form.collection_type.errors }}
+ {{ collection_form.collection_type_str.errors }}
</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 74c90352..75d67b32 100644
--- a/src/hi/apps/collection/templates/collection/panes/collection_details.html
+++ b/src/hi/apps/collection/templates/collection/panes/collection_details.html
@@ -13,7 +13,7 @@
</div>
<div id="{{ DIVID.COLLECTION_EDIT_PANE }}">
- {% include "collection/edit/panes/collection_edit.html" %}
+ {% include "collection/edit/panes/collection_edit.html" with collection=collection_detail_data.collection collection_form=collection_detail_data.collection_form %}
</div>
{% if collection_detail_data.collection_position_form %}
diff --git a/src/hi/apps/entity/edit/async_views.py b/src/hi/apps/entity/edit/async_views.py
index 67b5caa3..1611f674 100644
--- a/src/hi/apps/entity/edit/async_views.py
+++ b/src/hi/apps/entity/edit/async_views.py
@@ -17,6 +17,33 @@ from . import forms
logger = logging.getLogger(__name__)
+class EntityEditView( View ):
+
+ def post( self, request, *args, **kwargs ):
+
+ entity_id = kwargs.get('entity_id')
+ try:
+ entity = Entity.objects.get( id = entity_id )
+ except Entity.DoesNotExist:
+ raise Http404( request )
+
+ entity_form = forms.EntityForm( request.POST, instance = entity )
+ if entity_form.is_valid():
+ entity_form.save()
+
+ context = {
+ 'entity': entity,
+ 'entity_form': entity_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,
+ },
+ )
+
+
@method_decorator( edit_required, name='dispatch' )
class EntityPositionEditView( View ):
@@ -26,10 +53,10 @@ class EntityPositionEditView( View ):
location = request.view_parameters.location
try:
entity_position = EntityPosition.objects.get(
- id = entity_id,
+ entity_id = entity_id,
location = location,
)
- except Entity.DoesNotExist:
+ except EntityPosition.DoesNotExist:
raise Http404( request )
entity_position_form = forms.EntityPositionForm(
diff --git a/src/hi/apps/entity/edit/forms.py b/src/hi/apps/entity/edit/forms.py
index da50d702..fd98260a 100644
--- a/src/hi/apps/entity/edit/forms.py
+++ b/src/hi/apps/entity/edit/forms.py
@@ -1,36 +1,26 @@
from django import forms
from hi.apps.entity.enums import EntityType
-from hi.apps.entity.models import EntityPosition
+from hi.apps.entity.models import Entity, EntityPosition
from hi.apps.location.edit.forms import LocationItemPositionForm
-class EntityForm(forms.Form):
+class EntityForm( forms.ModelForm ):
- name = forms.CharField(
- label = 'Name',
- max_length = 64,
- required = False,
- )
-
- entity_type = forms.ChoiceField(
+ class Meta:
+ model = Entity
+ fields = (
+ 'name',
+ 'entity_type_str',
+ )
+
+ entity_type_str = forms.ChoiceField(
label = 'Entity Type',
choices = EntityType.choices,
initial = EntityType.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:
- entity_type_str = cleaned_data.get( 'entity_type' )
- entity_type = EntityType.from_name_safe( entity_type_str )
- cleaned_data['name'] = entity_type.label
-
- return cleaned_data
class EntityPositionForm( LocationItemPositionForm ):
diff --git a/src/hi/apps/entity/edit/urls.py b/src/hi/apps/entity/edit/urls.py
index 86d81b75..2abd32b9 100644
--- a/src/hi/apps/entity/edit/urls.py
+++ b/src/hi/apps/entity/edit/urls.py
@@ -10,6 +10,10 @@ urlpatterns = [
views.EntityAddView.as_view(),
name='entity_edit_entity_add' ),
+ re_path( r'^entity/edit/(?P<entity_id>\d+)$',
+ async_views.EntityEditView.as_view(),
+ name='entity_edit'),
+
re_path( r'^entity/delete/(?P<entity_id>\d+)$',
views.EntityDeleteView.as_view(),
name='entity_edit_entity_delete' ),
diff --git a/src/hi/apps/entity/edit/views.py b/src/hi/apps/entity/edit/views.py
index 0ab6601c..9dd7503a 100644
--- a/src/hi/apps/entity/edit/views.py
+++ b/src/hi/apps/entity/edit/views.py
@@ -8,8 +8,6 @@ from django.utils.decorators import method_decorator
from hi.apps.collection.collection_manager import CollectionManager
import hi.apps.common.antinode as antinode
-from hi.apps.entity.entity_manager import EntityManager
-from hi.apps.entity.enums import EntityType
from hi.apps.entity.models import Entity
from hi.apps.location.location_manager import LocationManager
@@ -45,17 +43,9 @@ class EntityAddView( HiModalView ):
context = context,
)
- cleaned_data = entity_form.clean()
- entity_type = EntityType.from_name_safe( cleaned_data.get('entity_type') )
- name = cleaned_data.get('name')
-
try:
with transaction.atomic():
- entity = EntityManager().create_entity(
- name = name,
- entity_type = entity_type,
- can_user_delete = True,
- )
+ entity = entity_form.save()
if ( request.view_parameters.view_type.is_location_view
and request.view_parameters.location_view_id ):
LocationManager().add_entity_to_view_by_id(
diff --git a/src/hi/apps/entity/entity_detail_data.py b/src/hi/apps/entity/entity_detail_data.py
index 3e9a4189..430f7dd2 100644
--- a/src/hi/apps/entity/entity_detail_data.py
+++ b/src/hi/apps/entity/entity_detail_data.py
@@ -1,6 +1,6 @@
from dataclasses import dataclass
-from hi.apps.entity.edit.forms import EntityPositionForm
+from hi.apps.entity.edit.forms import EntityForm, EntityPositionForm
from .models import Entity
@@ -9,5 +9,6 @@ from .models import Entity
class EntityDetailData:
entity : Entity
+ entity_form : EntityForm
entity_position_form : EntityPositionForm
diff --git a/src/hi/apps/entity/entity_manager.py b/src/hi/apps/entity/entity_manager.py
index 2c2a8ada..cc62afba 100644
--- a/src/hi/apps/entity/entity_manager.py
+++ b/src/hi/apps/entity/entity_manager.py
@@ -3,7 +3,7 @@ from decimal import Decimal
from django.db import transaction
from hi.apps.common.singleton import Singleton
-from hi.apps.entity.edit.forms import EntityPositionForm
+from hi.apps.entity.edit.forms import EntityForm, EntityPositionForm
from hi.apps.location.models import Location, LocationView
from hi.apps.location.svg_item_factory import SvgItemFactory
@@ -190,6 +190,7 @@ class EntityManager(Singleton):
# TODO: Add attributes and other data
return EntityDetailData(
entity = entity,
+ entity_form = EntityForm( instance = entity ),
entity_position_form = entity_position_form,
)
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 4403faf3..14023882 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 +1,14 @@
-ENTITY EDIT PANE
+<form action="{% url 'entity_edit' entity_id=entity.id %}"
+ method="post"
+ class="form" data-async="#{{ DIVID.ENTITY_EDIT_PANE }}">
+ {% csrf_token %}
+
+ <div>
+ {% include "entity/edit/panes/entity_form.html" %}
+ </div>
+
+ <button id="hi-add-entity-edit-button" type="submit" class="btn btn-info"
+ name="action" value="update">
+ UPDATE
+ </button>
+</form>
diff --git a/src/hi/apps/entity/templates/entity/edit/panes/entity_form.html b/src/hi/apps/entity/templates/entity/edit/panes/entity_form.html
index 4fb4a634..0dd3e8a2 100644
--- a/src/hi/apps/entity/templates/entity/edit/panes/entity_form.html
+++ b/src/hi/apps/entity/templates/entity/edit/panes/entity_form.html
@@ -13,11 +13,11 @@
<div>
<div class="input-group mb-2 mr-sm-2">
<div class="input-group-prepend">
- <div class="input-group-text">{{ entity_form.entity_type.label_tag }}</div>
+ <div class="input-group-text">{{ entity_form.entity_type_str.label_tag }}</div>
</div>
- {{ entity_form.entity_type }}
+ {{ entity_form.entity_type_str }}
</div>
- {{ ollection_form.entity_type.errors }}
+ {{ entity_form.entity_type_str.errors }}
</div>
<div>
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 8bf98f87..8475b961 100644
--- a/src/hi/apps/entity/templates/entity/panes/entity_details.html
+++ b/src/hi/apps/entity/templates/entity/panes/entity_details.html
@@ -13,10 +13,11 @@
</div>
<div id="{{ DIVID.ENTITY_EDIT_PANE }}">
- {% include "entity/edit/panes/entity_edit.html" %}
+ {% include "entity/edit/panes/entity_edit.html" with entity=entity_detail_data.entity entity_form=entity_detail_data.entity_form %}
</div>
{% if entity_detail_data.entity_position_form %}
+<h5>Geometry</h5>
<div id="{{ DIVID.ENTITY_POSITION_EDIT_PANE }}">
{% include "entity/edit/panes/entity_position_edit.html" with entity=entity_detail_data.entity entity_position_form=entity_detail_data.entity_position_form %}
</div>
diff --git a/src/hi/apps/location/edit/async_views.py b/src/hi/apps/location/edit/async_views.py
index 3854ebbc..31ffdd36 100644
--- a/src/hi/apps/location/edit/async_views.py
+++ b/src/hi/apps/location/edit/async_views.py
@@ -16,7 +16,6 @@ from hi.apps.entity.entity_manager import EntityManager
from hi.apps.entity.models import Entity
from hi.apps.location.location_manager import LocationManager
from hi.apps.location.models import LocationView
-from hi.apps.location.svg_item_factory import SvgItemFactory
from hi.constants import DIVID
from hi.decorators import edit_required
diff --git a/src/hi/apps/location/edit/forms.py b/src/hi/apps/location/edit/forms.py
index 1602f284..8111ba2c 100644
--- a/src/hi/apps/location/edit/forms.py
+++ b/src/hi/apps/location/edit/forms.py
@@ -7,11 +7,10 @@ from django.conf import settings
from hi.apps.attribute.forms import AttributeForm
from hi.apps.common.svg_forms import SvgDecimalFormField, SvgFileForm
+from hi.apps.location.enums import LocationViewType
from hi.apps.location.models import (
Location,
LocationAttribute,
- LocationItemModelMixin,
- LocationItemPositionModel,
LocationView,
)
@@ -115,6 +114,14 @@ class LocationViewEditForm( forms.ModelForm ):
svg_rotate = SvgDecimalFormField()
+ location_view_type_str = forms.ChoiceField(
+ label = 'View Type',
+ choices = LocationViewType.choices,
+ initial = LocationViewType.default_value(),
+ required = True,
+ widget = forms.Select( attrs = { 'class' : 'custom-select' } ),
+ )
+
class LocationViewGeometryForm( forms.ModelForm ):
class Meta:
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 b39d59d6..d3434f72 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
@@ -2,12 +2,6 @@
{{ hidden }}
{% endfor %}
-
-
-<div>Errors: {{ location_view_edit_form.errors }}</div>
-
-
-
{% if location_view_edit_form.non_field_errors %}
<div class="non-field-errors">
{% for err in location_view_edit_form.non_field_errors %}
@@ -38,7 +32,11 @@
{{ 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 }}
+ <div class="input-group mb-2 mr-sm-2">
+ <div class="input-group-prepend">
+ <div class="input-group-text">{{ location_view_edit_form.location_view_type_str.label_tag }}</div>
+ </div>
+ {{ location_view_edit_form.location_view_type_str }}
+ </div>
{{ location_view_edit_form.location_view_type_str.errors }}
</div>