Home Information - Your home, your data, organized.
Python
Added on-click attribute file uploads.
Anthony Cassandra18b4d0d03c1d
diff --git a/src/hi/apps/attribute/forms.py b/src/hi/apps/attribute/forms.pyindex 69ee8819..1f476aa5 100644--- a/src/hi/apps/attribute/forms.py+++ b/src/hi/apps/attribute/forms.py@@ -6,10 +6,35 @@ from hi.apps.common.utils import is_blankfrom .enums import AttributeType, AttributeValueType-class AttributeForm(forms.ModelForm):-+class AttributeForm( forms.ModelForm ):+ """+ Abstract mode form class the corresponds to the abstract model calss+ Attribute. When subclassing the Attribute model, you likely also want+ a subclass of this model form. Subclassing this fomr looks something+ like this:++ class MyAttributeForm( AttributeForm ):+ class Meta( AttributeForm.Meta ):+ model = MyAttribute+ """class Meta:- abstract = True+ fields = (+ 'name',+ 'value',+ 'file_value',+ 'value_type_str',+ )++ value_type_str = forms.ChoiceField(+ label = 'Value Type',+ choices = AttributeValueType.choices,+ initial = AttributeValueType.default_value(),+ required = True,+ widget = forms.Select( attrs = {+ 'class' : 'custom-select',+ 'onchange': 'Hi.changeAttributeValueType( this );',+ } ),+ )@propertydef show_as_editable(self):@@ -32,15 +57,19 @@ class AttributeForm(forms.ModelForm):def clean(self):cleaned_data = super().clean()-- if not self.instance.is_editable:++ form_is_bound = bool( self.instance.pk )+ if form_is_bound and not self.instance.is_editable:raise ValidationError( 'This attribute is not editable.' )-+value_type = AttributeValueType.from_name( cleaned_data.get('value_type_str') )name = cleaned_data.get('name')value = cleaned_data.get('value')file_value = cleaned_data.get('file_value')+ if form_is_bound and ( self.instance.value_type != value_type ):+ raise ValidationError( 'Changing an attribute value type is not supported.' )+if is_blank( name ):self.add_error( 'name', 'A name is required' )@@ -58,32 +87,40 @@ class AttributeForm(forms.ModelForm):instance = super().save( commit = False )if not instance.pk:- instance.attribute_type_str = AttributeType.CUSTOM+ instance.attribute_type_str = str(AttributeType.CUSTOM)instance.is_editable = True- instance.iis_required = False+ instance.is_required = Falseif commit:instance.save()return instance-class GeneralAttributeForm( AttributeForm ):+class AttributeUploadForm( forms.ModelForm ):class Meta:fields = (- 'name',- 'value','file_value',- 'value_type_str',)++ def clean(self):+ cleaned_data = super().clean()- value_type_str = forms.ChoiceField(- label = 'Value Type',- choices = AttributeValueType.choices,- initial = AttributeValueType.default_value(),- required = True,- widget = forms.Select( attrs = {- 'class' : 'custom-select',- 'onchange': 'Hi.changeAttributeValueType( this );',- } ),- )+ file_value = cleaned_data.get('file_value')+ if not file_value:+ self.add_error( 'file_value', 'A file is required.')++ return cleaned_data++ def save( self, commit = True ):+ instance = super().save( commit = False )++ instance.attribute_type_str = str(AttributeType.CUSTOM)+ instance.value_type_str = str( AttributeValueType.FILE )+ instance.name = instance.file_value.name+ instance.is_editable = True+ instance.is_required = False++ if commit:+ instance.save()+ return instancediff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_upload_form.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_upload_form.htmlnew file mode 100644index 00000000..296571f1--- /dev/null+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_upload_form.html@@ -0,0 +1,9 @@+<label for="{{ attribute_upload_form.file_value.id_for_label }}"+ class="btn btn-info">+ UPLOAD+</label>+<input type="file" name="{{ attribute_upload_form.file_value.html_name }}"+ id="{{ attribute_upload_form.file_value.id_for_label }}"+ style="display: none;"+ onchange="Hi.submitForm( this );">+diff --git a/src/hi/apps/entity/edit/async_views.py b/src/hi/apps/entity/edit/async_views.pyindex 0698ec3b..71130b11 100644--- a/src/hi/apps/entity/edit/async_views.py+++ b/src/hi/apps/entity/edit/async_views.py@@ -7,7 +7,7 @@ from django.utils.decorators import method_decoratorfrom django.views.generic import Viewimport hi.apps.common.antinode as antinode-from hi.apps.entity.models import EntityPosition+from hi.apps.entity.models import EntityAttribute, EntityPositionfrom hi.apps.entity.view_mixin import EntityViewMixinfrom hi.apps.location.svg_item_factory import SvgItemFactoryfrom hi.apps.location.location_manager import LocationManager@@ -34,7 +34,7 @@ class EntityEditView( View, EntityViewMixin ):)if entity_form.is_valid() and entity_attribute_formset.is_valid():with transaction.atomic():- entity_form.save()+ entity_form.save()entity_attribute_formset.save()# Recreate to preserve "max" to show new formentity_attribute_formset = forms.EntityAttributeFormSet(@@ -44,11 +44,16 @@ class EntityEditView( View, EntityViewMixin ):status_code = 200else: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 )@@ -60,6 +65,50 @@ class EntityEditView( View, EntityViewMixin ):)+@method_decorator( edit_required, name='dispatch' )+class EntityAttributeUploadView( View, EntityViewMixin ):++ def post( self, request, *args, **kwargs ):+ entity = self.get_entity( request, *args, **kwargs )+ entity_attribute = EntityAttribute( entity = entity )+ entity_attribute_upload_form = forms.EntityAttributeUploadForm(+ request.POST,+ request.FILES,+ instance = entity_attribute,+ )++ if entity_attribute_upload_form.is_valid():+ with transaction.atomic():+ entity_attribute_upload_form.save()+ status_code = 200+ 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,+ },+ )++ 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' )class EntityPositionEditView( View, EntityViewMixin ):diff --git a/src/hi/apps/entity/edit/forms.py b/src/hi/apps/entity/edit/forms.pyindex f3c1081e..6cfc3720 100644--- a/src/hi/apps/entity/edit/forms.py+++ b/src/hi/apps/entity/edit/forms.py@@ -1,6 +1,6 @@from django import forms-from hi.apps.attribute.forms import GeneralAttributeForm+from hi.apps.attribute.forms import AttributeForm, AttributeUploadFormfrom hi.apps.entity.enums import EntityTypefrom hi.apps.entity.models import Entity, EntityAttribute, EntityPositionfrom hi.apps.location.edit.forms import LocationItemPositionForm@@ -24,8 +24,8 @@ class EntityForm( forms.ModelForm ):)-class EntityAttributeForm( GeneralAttributeForm ):- class Meta( GeneralAttributeForm.Meta ):+class EntityAttributeForm( AttributeForm ):+ class Meta( AttributeForm.Meta ):model = EntityAttribute@@ -40,6 +40,11 @@ EntityAttributeFormSet = forms.inlineformset_factory()+class EntityAttributeUploadForm( AttributeUploadForm ):+ class Meta( AttributeUploadForm.Meta ):+ model = EntityAttribute++class EntityPositionForm( LocationItemPositionForm ):class Meta( LocationItemPositionForm.Meta ):model = EntityPositiondiff --git a/src/hi/apps/entity/edit/urls.py b/src/hi/apps/entity/edit/urls.pyindex 2abd32b9..3bfb877e 100644--- a/src/hi/apps/entity/edit/urls.py+++ b/src/hi/apps/entity/edit/urls.py@@ -14,6 +14,10 @@ urlpatterns = [async_views.EntityEditView.as_view(),name='entity_edit'),+ re_path( r'^entity/attribute/upload/(?P<entity_id>\d+)$',+ async_views.EntityAttributeUploadView.as_view(),+ name='entity_attribute_upload'),+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/entity_detail_data.py b/src/hi/apps/entity/entity_detail_data.pyindex 488a073a..d4602e48 100644--- a/src/hi/apps/entity/entity_detail_data.py+++ b/src/hi/apps/entity/entity_detail_data.py@@ -1,6 +1,11 @@from dataclasses import dataclass-from hi.apps.entity.edit.forms import EntityAttributeFormSet, EntityForm, EntityPositionForm+from hi.apps.entity.edit.forms import (+ EntityAttributeFormSet,+ EntityForm,+ EntityPositionForm,+ EntityAttributeUploadForm,+)from .models import Entity@@ -8,8 +13,9 @@ from .models import Entity@dataclassclass EntityDetailData:- entity : Entity- entity_form : EntityForm- entity_attribute_formset : EntityAttributeFormSet- entity_position_form : EntityPositionForm+ entity : Entity+ entity_form : EntityForm+ entity_attribute_formset : EntityAttributeFormSet+ entity_attribute_upload_form : EntityAttributeUploadForm+ entity_position_form : EntityPositionFormdiff --git a/src/hi/apps/entity/entity_manager.py b/src/hi/apps/entity/entity_manager.pyindex c185d1cd..1f56c248 100644--- a/src/hi/apps/entity/entity_manager.py+++ b/src/hi/apps/entity/entity_manager.py@@ -4,7 +4,12 @@ from typing import Listfrom django.db import transactionfrom hi.apps.common.singleton import Singleton-from hi.apps.entity.edit.forms import EntityAttributeFormSet, EntityForm, EntityPositionForm+from hi.apps.entity.edit.forms import (+ EntityAttributeFormSet,+ EntityForm,+ EntityPositionForm,+ EntityAttributeUploadForm,+)from hi.apps.location.models import Location, LocationViewfrom hi.apps.location.svg_item_factory import SvgItemFactory@@ -236,6 +241,7 @@ class EntityManager(Singleton):'show_as_editable': True,},),+ entity_attribute_upload_form = EntityAttributeUploadForm(),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.htmlindex 14023882..6ff7a1b2 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@@ -12,3 +12,13 @@UPDATE</button></form>++<form action="{% url 'entity_attribute_upload' entity_id=entity.id %}"+ method="post"+ class="form" data-async="#{{ DIVID.ENTITY_EDIT_PANE }}">+ {% csrf_token %}+ <div>+ {% include "attribute/panes/attribute_upload_form.html" with attribute_upload_form=entity_attribute_upload_form %}+ </div>+</form>+diff --git a/src/hi/apps/entity/templates/entity/panes/entity_details.html b/src/hi/apps/entity/templates/entity/panes/entity_details.htmlindex e07720bc..3e9f7c6b 100644--- a/src/hi/apps/entity/templates/entity/panes/entity_details.html+++ b/src/hi/apps/entity/templates/entity/panes/entity_details.html@@ -13,7 +13,7 @@</div><div id="{{ DIVID.ENTITY_EDIT_PANE }}">- {% include "entity/edit/panes/entity_edit.html" with entity=entity_detail_data.entity entity_form=entity_detail_data.entity_form entity_attribute_formset=entity_detail_data.entity_attribute_formset %}+ {% include "entity/edit/panes/entity_edit.html" with entity=entity_detail_data.entity entity_form=entity_detail_data.entity_form entity_attribute_formset=entity_detail_data.entity_attribute_formset entity_attribute_upload_form=entity_detail_data.entity_attribute_upload_form %}</div>{% if entity_detail_data.entity_position_form %}diff --git a/src/hi/apps/location/async_views.py b/src/hi/apps/location/async_views.pyindex b5defa6d..dd7e086c 100644--- a/src/hi/apps/location/async_views.py+++ b/src/hi/apps/location/async_views.py@@ -9,6 +9,7 @@ from hi.apps.collection.async_views import CollectionDetailsViewfrom hi.apps.entity.async_views import EntityDetailsViewfrom hi.apps.location.edit.forms import (LocationAttributeFormSet,+ LocationAttributeUploadForm,LocationEditForm,LocationViewEditForm,)@@ -42,6 +43,7 @@ class LocationViewDetailsView( HiSideView, LocationViewMixin ):'show_as_editable': True,},),+ 'location_attribute_upload_form': LocationAttributeUploadForm(),'location_view': location_view,'location_view_edit_form': LocationViewEditForm( instance = location_view ),}diff --git a/src/hi/apps/location/edit/async_views.py b/src/hi/apps/location/edit/async_views.pyindex 552055ed..35d34526 100644--- a/src/hi/apps/location/edit/async_views.py+++ b/src/hi/apps/location/edit/async_views.py@@ -16,6 +16,7 @@ from hi.apps.entity.edit.async_views import EntityPositionEditViewfrom hi.apps.entity.entity_manager import EntityManagerfrom hi.apps.entity.models import Entityfrom hi.apps.location.location_manager import LocationManager+from hi.apps.location.models import LocationAttributefrom hi.apps.location.view_mixin import LocationViewMixinfrom hi.constants import DIVID@@ -43,12 +44,20 @@ class LocationEditView( View, LocationViewMixin ):request.FILES,instance = location,prefix = f'location-{location.id}',+ form_kwargs = {+ '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()@@ -73,6 +82,53 @@ class LocationEditView( View, LocationViewMixin ):return antinode.refresh_response()+@method_decorator( edit_required, name='dispatch' )+class LocationAttributeUploadView( View, LocationViewMixin ):++ def post( self, request, *args, **kwargs ):+ location = self.get_location( request, *args, **kwargs )+ location_attribute = LocationAttribute( location = location )+ location_attribute_upload_form = forms.LocationAttributeUploadForm(+ request.POST,+ request.FILES,+ instance = location_attribute,+ )++ if location_attribute_upload_form.is_valid():+ with transaction.atomic():+ location_attribute_upload_form.save()+ status_code = 200+ 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,+ },+ )++ 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.pyindex 335b31de..90b141d0 100644--- a/src/hi/apps/location/edit/forms.py+++ b/src/hi/apps/location/edit/forms.py@@ -4,7 +4,7 @@ import osfrom django import formsfrom django.conf import settings-from hi.apps.attribute.forms import GeneralAttributeForm+from hi.apps.attribute.forms import AttributeForm, AttributeUploadFormfrom hi.apps.common.svg_forms import SvgDecimalFormField, SvgFileFormfrom hi.apps.location.enums import LocationViewType@@ -66,8 +66,8 @@ class LocationEditForm( forms.ModelForm ):)-class LocationAttributeForm( GeneralAttributeForm ):- class Meta( GeneralAttributeForm.Meta ):+class LocationAttributeForm( AttributeForm ):+ class Meta( AttributeForm.Meta ):model = LocationAttribute@@ -82,6 +82,11 @@ LocationAttributeFormSet = forms.inlineformset_factory()+class LocationAttributeUploadForm( AttributeUploadForm ):+ class Meta( AttributeUploadForm.Meta ):+ model = LocationAttribute++class LocationViewAddForm(forms.Form):# N.B. When adding a LocationView, we use limited field options to keepdiff --git a/src/hi/apps/location/edit/urls.py b/src/hi/apps/location/edit/urls.pyindex bae784ac..b6aa648a 100644--- a/src/hi/apps/location/edit/urls.py+++ b/src/hi/apps/location/edit/urls.py@@ -14,6 +14,10 @@ urlpatterns = [async_views.LocationEditView.as_view(),name='location_edit_location_edit'),+ re_path( r'^location/attribute/upload/(?P<location_id>\d+)$',+ async_views.LocationAttributeUploadView.as_view(),+ name='location_attribute_upload'),+re_path( r'^location/svg/replace/(?P<location_id>\d+)$',views.LocationSvgReplaceView.as_view(),name='location_edit_svg_replace'),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.htmlindex f672e7ec..c6d1eeb5 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@@ -14,6 +14,14 @@</button></form>+<form action="{% url 'location_attribute_upload' location_id=location.id %}"+ method="post"+ class="form" data-async="#{{ DIVID.LOCATION_EDIT_PANE }}">+ {% csrf_token %}+ <div>+ {% include "attribute/panes/attribute_upload_form.html" with attribute_upload_form=location_attribute_upload_form %}+ </div>+</form><div><a href="{% url 'location_edit_svg_replace' location_id=location.id %}"diff --git a/src/hi/apps/location/templates/location/panes/location_details.html b/src/hi/apps/location/templates/location/panes/location_details.htmlindex 0950b816..c07d9448 100644--- a/src/hi/apps/location/templates/location/panes/location_details.html+++ b/src/hi/apps/location/templates/location/panes/location_details.html@@ -11,9 +11,9 @@</a></div></div>-<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>+<div id="{{ DIVID.LOCATION_VIEW_EDIT_PANE }}">+ {% include "location/edit/panes/location_view_edit.html" %}+</div>diff --git a/src/hi/static/css/main.css b/src/hi/static/css/main.cssindex 65fe5cfa..fe94800a 100644--- a/src/hi/static/css/main.css+++ b/src/hi/static/css/main.css@@ -193,5 +193,3 @@ svg[action-state="scale"] {svg .highlighted {filter: drop-shadow(0 0 10px red);}--diff --git a/src/hi/static/js/main.js b/src/hi/static/js/main.jsindex 01c158eb..8e49e324 100644--- a/src/hi/static/js/main.js+++ b/src/hi/static/js/main.js@@ -21,6 +21,9 @@generateUniqueId: function() {return _generateUniqueId();},+ submitForm: function( formElement ) {+ return _submitForm( formElement );+ },togglePasswordField: function( toggleCheckbox ) {return _togglePasswordField( toggleCheckbox );},@@ -69,6 +72,12 @@return 'id-' + Date.now() + '-' + Math.floor(Math.random() * 1000);}+ function _submitForm( formElement ) {+ let form = $(formElement).closest('form');+ if ( Hi.DEBUG ) { console.debug( 'Submitting form:', formElement, form ); }+ $(form).submit();+ }+function _togglePasswordField( toggleCheckbox ) {let passwordField = $(toggleCheckbox).closest(Hi.FORM_FIELD_CONTAINER_SELECTOR).find('input[type="password"], input[type="text"]');