Skip to content

Home Information - Your home, your data, organized.

Python

End of day saving of Attribute work. WIP

Anthony Cassandrab3ad2c2d88b0
diff --git a/src/hi/apps/attribute/forms.py b/src/hi/apps/attribute/forms.py
index 8b5f6e9a..69ee8819 100644
--- a/src/hi/apps/attribute/forms.py
+++ b/src/hi/apps/attribute/forms.py
@@ -1,7 +1,9 @@
-from django.core.files.storage import default_storage
+from django.core.exceptions import ValidationError
from django import forms
-from .enums import AttributeValueType
+from hi.apps.common.utils import is_blank
+
+from .enums import AttributeType, AttributeValueType
class AttributeForm(forms.ModelForm):
@@ -30,19 +32,40 @@ class AttributeForm(forms.ModelForm):
def clean(self):
cleaned_data = super().clean()
+
+ if 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')
- value_type = self.instance.value_type
+ file_value = cleaned_data.get('file_value')
- if value_type in { AttributeValueType.TEXT }:
- if not isinstance(value, str):
- self.add_error('value', 'Value must be a string.')
+ if is_blank( name ):
+ self.add_error( 'name', 'A name is required' )
+
+ if value_type == AttributeValueType.FILE:
+ if not file_value:
+ self.add_error( 'file_value', 'A file is required.')
- elif value_type in { AttributeValueType.FILE }:
- if not default_storage.exists( value ):
- self.add_error('value', f'{value_type} file does not exist.')
+ elif value_type in { AttributeValueType.TEXT, AttributeValueType.SECRET }:
+ if self.instance.is_required and is_blank( value ):
+ self.add_error( 'value', 'A value is required' )
return cleaned_data
+ def save( self, commit = True ):
+ instance = super().save( commit = False )
+
+ if not instance.pk:
+ instance.attribute_type_str = AttributeType.CUSTOM
+ instance.is_editable = True
+ instance.iis_required = False
+
+ if commit:
+ instance.save()
+ return instance
+
class GeneralAttributeForm( AttributeForm ):
diff --git a/src/hi/apps/attribute/models.py b/src/hi/apps/attribute/models.py
index aec0779f..c3faa6cb 100644
--- a/src/hi/apps/attribute/models.py
+++ b/src/hi/apps/attribute/models.py
@@ -1,3 +1,6 @@
+import logging
+
+from django.core.files.storage import default_storage
from django.db import models
from hi.apps.common.file_utils import generate_unique_filename
@@ -9,6 +12,8 @@ from .enums import (
AttributeType,
)
+logger = logging.getLogger(__name__)
+
class AttributeModel(models.Model):
@@ -107,3 +112,19 @@ class AttributeModel(models.Model):
super().save(*args, **kwargs)
return
+ def delete( self, *args, **kwargs ):
+ """ Deleting file from MEDIA_ROOT on best effort basis. Ignore if fails. """
+
+ if self.file_value:
+ try:
+ if default_storage.exists( self.file_value ):
+ default_storage.delete( self.file_value )
+ logger.debug( f'Deleted Attribute file: {self.file_value}' )
+ else:
+ logger.warn( f'Attribute file not found: {self.file_value}' )
+ except Exception as e:
+ # Log the error or handle it accordingly
+ logger.warn( f'Error deleting Attribute file {self.file_value}: {e}' )
+
+ super().delete( *args, **kwargs )
+ return
diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_existing.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_existing.html
index 6b594113..30aad278 100644
--- a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_existing.html
+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_existing.html
@@ -1,17 +1,21 @@
+<input type="hidden" id="{{ attribute_form.value_type_str.id_for_label }}"
+ name="{{ attribute_form.value_type_str.html_name }}"
+ value="{{ attribute_form.instance.value_type_str }}" />
+<input type="hidden" id="{{ attribute_form.name.id_for_label }}"
+ name="{{ attribute_form.name.html_name }}"
+ value="{{ attribute_form.instance.name }}" />
<div class="input-group" >
<div class="input-group-prepend" >
<span class="input-group-text" >{{ attribute_form.instance.name }}</span>
</div>
- {{ attribute_form.value }}
+ <textarea rows="2" cols="40"
+ id="{{ attribute_form.value.id_for_label }}"
+ name="{{ attribute_form.value.html_name }}"
+ >{{ attribute_form.value.value|default_if_none:'' }}</textarea>
+ <div class="form-error text-danger" >{{ attribute_form.value.errors }}</div>
</div>
-<div class="form-error text-danger" >{{ attribute_form.value.errors }}</div>
-<div>
- {{ attribute_form.value_type_str.label }}
- {{ attribute_form.value_type_str }}
- {{ attribute_form.value_type_str.errors }}
-</div>
-{% if attribute_form.show_as_editable and instance.is_editable %}
+{% if attribute_form.show_as_editable and attribute_form.instance.is_editable %}
<div class="">
{{ attribute_form.DELETE }} Delete
</div>
diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_new.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_new.html
index 67deecad..ec650a38 100644
--- a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_new.html
+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_new.html
@@ -61,8 +61,7 @@
<textarea rows="2" cols="40"
id="{{ attribute_form.value.id_for_label }}"
name="{{ attribute_form.value.html_name }}"
- value="{{ attribute_form.value.value|default_if_none:'' }}">
- </textarea>
+ >{{ attribute_form.value.value|default_if_none:'' }}</textarea>
<div class="form-error text-danger" >{{ attribute_form.value.errors }}</div>
</div>
</div>
diff --git a/src/hi/apps/common/utils.py b/src/hi/apps/common/utils.py
index bbc5c42e..34ef3870 100644
--- a/src/hi/apps/common/utils.py
+++ b/src/hi/apps/common/utils.py
@@ -14,6 +14,14 @@ def is_ajax( request : HttpRequest ):
return bool( request.headers.get('x-requested-with') == 'XMLHttpRequest' )
+def is_blank( obj ):
+ if obj is None:
+ return True
+ if not isinstance( obj, str ):
+ return False
+ return bool( obj.strip() == '' )
+
+
def get_long_display_name( user_obj ):
if not user_obj:
return settings.USER_DISPLAY_NAME_DEFAULT
diff --git a/src/hi/apps/entity/edit/async_views.py b/src/hi/apps/entity/edit/async_views.py
index 405cdac2..0698ec3b 100644
--- a/src/hi/apps/entity/edit/async_views.py
+++ b/src/hi/apps/entity/edit/async_views.py
@@ -41,7 +41,10 @@ class EntityEditView( View, EntityViewMixin ):
instance = entity,
prefix = f'entity-{entity.id}',
)
-
+ status_code = 200
+ else:
+ status_code = 400
+
context = {
'entity': entity,
'entity_form': entity_form,
@@ -53,6 +56,7 @@ class EntityEditView( View, EntityViewMixin ):
insert_map = {
DIVID['ENTITY_EDIT_PANE']: content,
},
+ status = status_code,
)
diff --git a/src/hi/apps/entity/templates/entity/panes/entity_attributes.html b/src/hi/apps/entity/templates/entity/panes/entity_attributes.html
deleted file mode 100644
index b539898c..00000000
--- a/src/hi/apps/entity/templates/entity/panes/entity_attributes.html
+++ /dev/null
@@ -1 +0,0 @@
-TBD - ATTRIBUTES
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 ad575766..e07720bc 100644
--- a/src/hi/apps/entity/templates/entity/panes/entity_details.html
+++ b/src/hi/apps/entity/templates/entity/panes/entity_details.html
@@ -23,11 +23,6 @@
</div>
{% endif %}
-<h5>Attributes</h5>
-<div id="hi-edit-entity-attributes">
- {% include "entity/panes/entity_attributes.html" %}
-</div>
-
<h5>Views</h5>
<div id="hi-edit-entity-views">
{% include "entity/panes/entity_views.html" %}
diff --git a/src/hi/constants.py b/src/hi/constants.py
index 1ca2b74e..2a70406b 100644
--- a/src/hi/constants.py
+++ b/src/hi/constants.py
@@ -1,5 +1,5 @@
-# HTML div ids that are commonly referenced
+# HTML div ids (classes and attributes) that app logic depends on.
DIVID = {
# Main HI Grid Structure