Skip to content

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.py
index 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_blank
from .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 );',
+ } ),
+ )
@property
def 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 = False
if 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 instance
diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_upload_form.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_upload_form.html
new file mode 100644
index 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.py
index 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_decorator
from django.views.generic import View
import hi.apps.common.antinode as antinode
-from hi.apps.entity.models import EntityPosition
+from hi.apps.entity.models import EntityAttribute, EntityPosition
from hi.apps.entity.view_mixin import EntityViewMixin
from hi.apps.location.svg_item_factory import SvgItemFactory
from 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 form
entity_attribute_formset = forms.EntityAttributeFormSet(
@@ -44,11 +44,16 @@ class EntityEditView( View, EntityViewMixin ):
status_code = 200
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 )
@@ -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.py
index 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, AttributeUploadForm
from hi.apps.entity.enums import EntityType
from hi.apps.entity.models import Entity, EntityAttribute, EntityPosition
from 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 = EntityPosition
diff --git a/src/hi/apps/entity/edit/urls.py b/src/hi/apps/entity/edit/urls.py
index 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.py
index 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
@dataclass
class 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 : EntityPositionForm
diff --git a/src/hi/apps/entity/entity_manager.py b/src/hi/apps/entity/entity_manager.py
index 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 List
from django.db import transaction
from 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, LocationView
from 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.html
index 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.html
index 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.py
index 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 CollectionDetailsView
from hi.apps.entity.async_views import EntityDetailsView
from 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.py
index 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 EntityPositionEditView
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 LocationAttribute
from hi.apps.location.view_mixin import LocationViewMixin
from 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.py
index 335b31de..90b141d0 100644
--- a/src/hi/apps/location/edit/forms.py
+++ b/src/hi/apps/location/edit/forms.py
@@ -4,7 +4,7 @@ import os
from django import forms
from django.conf import settings
-from hi.apps.attribute.forms import GeneralAttributeForm
+from hi.apps.attribute.forms import AttributeForm, AttributeUploadForm
from hi.apps.common.svg_forms import SvgDecimalFormField, SvgFileForm
from 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 keep
diff --git a/src/hi/apps/location/edit/urls.py b/src/hi/apps/location/edit/urls.py
index 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.html
index 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.html
index 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.css
index 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.js
index 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"]');