Skip to content

Home Information - Your home, your data, organized.

Python

Attribute basic features now work. Redid file uploads.

Anthony Cassandrabeac20ffa1ff
diff --git a/src/hi/apps/attribute/enums.py b/src/hi/apps/attribute/enums.py
index 383660e3..a19373af 100644
--- a/src/hi/apps/attribute/enums.py
+++ b/src/hi/apps/attribute/enums.py
@@ -9,18 +9,23 @@ class AttributeType(LabeledEnum):
class AttributeValueType(LabeledEnum):
- def __init__( self,
- label : str,
- description : str,
- is_file : bool = False ):
- super().__init__( label, description )
- self.is_file = is_file
- return
-
TEXT = ('Text', '' )
- FILE = ('File', '', True ) # relative filename of MEDIA_ROOT
+ FILE = ('File', '' ) # relative filename of MEDIA_ROOT
SECRET = ('Secret', '' )
@classmethod
def default(cls):
return cls.TEXT
+
+ @property
+ def is_file(self):
+ return bool( self == AttributeValueType.FILE )
+
+ @property
+ def is_text(self):
+ return bool( self == AttributeValueType.TEXT )
+
+ @property
+ def is_secret(self):
+ return bool( self == AttributeValueType.SECRET )
+
diff --git a/src/hi/apps/attribute/forms.py b/src/hi/apps/attribute/forms.py
index 1f476aa5..3a7a031f 100644
--- a/src/hi/apps/attribute/forms.py
+++ b/src/hi/apps/attribute/forms.py
@@ -1,3 +1,5 @@
+import mimetypes
+
from django.core.exceptions import ValidationError
from django import forms
@@ -21,19 +23,11 @@ class AttributeForm( forms.ModelForm ):
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 );',
- } ),
+ secret = forms.BooleanField(
+ required = False,
+ label = 'Mark as Secret',
)
@property
@@ -62,25 +56,18 @@ class AttributeForm( forms.ModelForm ):
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' )
-
- if value_type == AttributeValueType.FILE:
- if not file_value:
- self.add_error( 'file_value', 'A file is required.')
-
- 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' )
+ if self.cleaned_data.get('secret'):
+ stripped_value = value.strip()
+ value_lines = stripped_value.splitlines()
+ if len( value_lines) > 1:
+ self.add_error( 'value', 'Secret attributes are limited ot a single line.')
+
return cleaned_data
def save( self, commit = True ):
@@ -90,7 +77,12 @@ class AttributeForm( forms.ModelForm ):
instance.attribute_type_str = str(AttributeType.CUSTOM)
instance.is_editable = True
instance.is_required = False
-
+
+ if self.cleaned_data.get('secret'):
+ instance.value_type_str = str( AttributeValueType.SECRET )
+ else:
+ instance.value_type_str = str(AttributeValueType.TEXT)
+
if commit:
instance.save()
return instance
@@ -114,13 +106,16 @@ class AttributeUploadForm( forms.ModelForm ):
def save( self, commit = True ):
instance = super().save( commit = False )
+
+ mime_type_tuple = mimetypes.guess_type( instance.file_value.name )
- instance.attribute_type_str = str(AttributeType.CUSTOM)
- instance.value_type_str = str( AttributeValueType.FILE )
instance.name = instance.file_value.name
+ instance.file_mime_type = mime_type_tuple[0]
+ instance.value_type_str = str( AttributeValueType.FILE )
+ instance.attribute_type_str = str(AttributeType.CUSTOM)
instance.is_editable = True
instance.is_required = False
-
+
if commit:
instance.save()
return instance
diff --git a/src/hi/apps/attribute/models.py b/src/hi/apps/attribute/models.py
index c3faa6cb..fb66e632 100644
--- a/src/hi/apps/attribute/models.py
+++ b/src/hi/apps/attribute/models.py
@@ -26,11 +26,17 @@ class AttributeModel(models.Model):
)
value = models.TextField(
'Value',
+ blank = True, null = True,
)
file_value = models.FileField(
upload_to = 'attributes/', # Subclasses override via get_upload_to()
blank = True, null = True,
)
+ file_mime_type = models.CharField(
+ 'Mime Type',
+ max_length = 128,
+ null = True, blank = True,
+ )
value_type_str = models.CharField(
'Value Type',
max_length = 32,
diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_form.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_form.html
index e35fe162..9b6ffb70 100644
--- a/src/hi/apps/attribute/templates/attribute/panes/attribute_form.html
+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_form.html
@@ -14,7 +14,13 @@
{% endif %}
{% if attribute_form.instance.pk %}
- {% include "attribute/panes/attribute_form_existing.html" %}
+
+ {% if attribute_form.instance.value_type.is_file %}
+ {% include "attribute/panes/attribute_form_file.html" %}
+ {% else %}
+ {% include "attribute/panes/attribute_form_text.html" %}
+ {% endif %}
+
{% else %}
{% include "attribute/panes/attribute_form_new.html" %}
{% endif %}
diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_file.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_file.html
new file mode 100644
index 00000000..ad95949a
--- /dev/null
+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_file.html
@@ -0,0 +1,23 @@
+<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>
+ File: <a href="{{ attribute_form.instance.file_value.url }}"
+ {% if attribute_form.instance.file_mime_type %}type="{{ attribute_form.instance.file_mime_type }}"{% endif %}
+ target="_blank">
+ {{ attribute_form.instance.name }}
+ </a>
+ </div>
+
+ <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>
+
+ {% if attribute_form.show_as_editable and attribute_form.instance.is_editable %}
+ <div class="">
+ {{ attribute_form.DELETE }} Delete
+ </div>
+ {% endif %}
+</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 ec650a38..8158b6a3 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
@@ -1,67 +1,27 @@
<div class="h5">Add</div>
-<div>
- {{ attribute_form.value_type_str.label }}
- {{ attribute_form.value_type_str }}
- {{ attribute_form.value_type_str.errors }}
-</div>
-
-<div class=""
- {% if attribute_form.value_type_str.value != 'file' %}style="display: none;"{% endif %}
- value-type="file">
+<div class="input-group" >
<label for="{{ attribute_form.name.id_for_label }}">{{ attribute_form.name.label }}</label>
<input type="text"
id="{{ attribute_form.name.id_for_label }}"
name="{{ attribute_form.name.html_name }}"
value="{{ attribute_form.name.value|default_if_none:'' }}">
- <div>
- Choose file:
- {{ attribute_form.file_value }}
- {{ attribute_form.file_value.errors }}
- </div>
+ <div class="form-error text-danger" >{{ attribute_form.name.errors }}</div>
</div>
+<div class="input-group">
+ <label for="{{ attribute_form.value.id_for_label }}">{{ attribute_form.value.label }}</label>
+ <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 class=""
- {% if attribute_form.value_type_str.value != 'secret' %}style="display: none;"{% endif %}
- value-type="secret">
- <div class="input-group" >
- <div>
- <label for="{{ attribute_form.name.id_for_label }}">{{ attribute_form.name.label }}</label>
- <input type="text"
- id="{{ attribute_form.name.id_for_label }}"
- name="{{ attribute_form.name.html_name }}"
- value="{{ attribute_form.name.value|default_if_none:'' }}">
- <input type="checkbox" onclick="Hi.togglePasswordField(this);">Show
- <div class="form-error text-danger" >{{ attribute_form.name.errors }}</div>
- </div>
- <div>
- <label for="{{ attribute_form.value.id_for_label }}">{{ attribute_form.value.label }}</label>
- <input type="password"
- id="{{ attribute_form.value.id_for_label }}"
- name="{{ attribute_form.value.html_name }}"
- value="{{ attribute_form.value.value|default_if_none:'' }}">
- <div class="form-error text-danger" >{{ attribute_form.value.errors }}</div>
- </div>
- </div>
</div>
-<div class=""
- {% if attribute_form.value_type_str.value != 'text' %}style="display: none;"{% endif %}
- value-type="text">
- <div class="input-group" >
- <label for="{{ attribute_form.name.id_for_label }}">{{ attribute_form.name.label }}</label>
- <input type="text"
- id="{{ attribute_form.name.id_for_label }}"
- name="{{ attribute_form.name.html_name }}"
- value="{{ attribute_form.name.value|default_if_none:'' }}">
- <div class="form-error text-danger" >{{ attribute_form.name.errors }}</div>
- </div>
- <div class="input-group">
- <label for="{{ attribute_form.value.id_for_label }}">{{ attribute_form.value.label }}</label>
- <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>
+<span>
+ <input type="checkbox" name="{{ attribute_form.secret.html_name }}"
+ id="{{ attribute_form.secret.id_for_label }}"
+ {% if attribute_form.secret.value %}checked="on"{% endif %} >
+ <label for="{{ attribute_form.secret.id_for_label }}">Mark as secret</label>
+</span>
+ {{ attribute_form.secret.errors }}
diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_existing.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_text.html
similarity index 57%
rename from src/hi/apps/attribute/templates/attribute/panes/attribute_form_existing.html
rename to src/hi/apps/attribute/templates/attribute/panes/attribute_form_text.html
index 30aad278..d1f44e37 100644
--- a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_existing.html
+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_text.html
@@ -1,6 +1,3 @@
-<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 }}" />
@@ -8,10 +5,23 @@
<div class="input-group-prepend" >
<span class="input-group-text" >{{ attribute_form.instance.name }}</span>
</div>
+ {% if attribute_form.instance.value_type.is_secret %}
+ <input type="password"
+ id="{{ attribute_form.value.id_for_label }}"
+ name="{{ attribute_form.value.html_name }}"
+ value="{{ attribute_form.value.value|default_if_none:'' }}">
+ <span>
+ <input type="checkbox" name="{{ attribute_form.value.html_name }}-show"
+ id="{{ attribute_form.value.id_for_label }}-show"
+ onclick="Hi.togglePasswordField( this );">
+ <label for="{{ attribute_form.value.id_for_label }}-show">Show</label>
+ </span>
+ {% else %}
<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>
+ {% endif %}
<div class="form-error text-danger" >{{ attribute_form.value.errors }}</div>
</div>
diff --git a/src/hi/apps/entity/migrations/0003_entityattribute_file_mime_type_and_more.py b/src/hi/apps/entity/migrations/0003_entityattribute_file_mime_type_and_more.py
new file mode 100644
index 00000000..03e0fc4b
--- /dev/null
+++ b/src/hi/apps/entity/migrations/0003_entityattribute_file_mime_type_and_more.py
@@ -0,0 +1,30 @@
+# Generated by Django 4.2.15 on 2024-10-18 02:09
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("entity", "0002_entityattribute_file_value_and_more"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="entityattribute",
+ name="file_mime_type",
+ field=models.CharField(
+ blank=True, max_length=128, null=True, verbose_name="Mime Type"
+ ),
+ ),
+ migrations.AlterField(
+ model_name="entityattribute",
+ name="file_value",
+ field=models.FileField(blank=True, null=True, upload_to="attributes/"),
+ ),
+ migrations.AlterField(
+ model_name="entityattribute",
+ name="value",
+ field=models.TextField(blank=True, null=True, verbose_name="Value"),
+ ),
+ ]
diff --git a/src/hi/apps/location/migrations/0004_locationattribute_file_mime_type_and_more.py b/src/hi/apps/location/migrations/0004_locationattribute_file_mime_type_and_more.py
new file mode 100644
index 00000000..798da96f
--- /dev/null
+++ b/src/hi/apps/location/migrations/0004_locationattribute_file_mime_type_and_more.py
@@ -0,0 +1,30 @@
+# Generated by Django 4.2.15 on 2024-10-18 02:09
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("location", "0003_locationattribute_file_value"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="locationattribute",
+ name="file_mime_type",
+ field=models.CharField(
+ blank=True, max_length=128, null=True, verbose_name="Mime Type"
+ ),
+ ),
+ migrations.AlterField(
+ model_name="locationattribute",
+ name="file_value",
+ field=models.FileField(blank=True, null=True, upload_to="attributes/"),
+ ),
+ migrations.AlterField(
+ model_name="locationattribute",
+ name="value",
+ field=models.TextField(blank=True, null=True, verbose_name="Value"),
+ ),
+ ]
diff --git a/src/hi/integrations/core/migrations/0003_integrationattribute_file_mime_type_and_more.py b/src/hi/integrations/core/migrations/0003_integrationattribute_file_mime_type_and_more.py
new file mode 100644
index 00000000..a0234749
--- /dev/null
+++ b/src/hi/integrations/core/migrations/0003_integrationattribute_file_mime_type_and_more.py
@@ -0,0 +1,30 @@
+# Generated by Django 4.2.15 on 2024-10-18 02:09
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("core", "0002_integrationattribute_file_value"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="integrationattribute",
+ name="file_mime_type",
+ field=models.CharField(
+ blank=True, max_length=128, null=True, verbose_name="Mime Type"
+ ),
+ ),
+ migrations.AlterField(
+ model_name="integrationattribute",
+ name="file_value",
+ field=models.FileField(blank=True, null=True, upload_to="attributes/"),
+ ),
+ migrations.AlterField(
+ model_name="integrationattribute",
+ name="value",
+ field=models.TextField(blank=True, null=True, verbose_name="Value"),
+ ),
+ ]
diff --git a/src/hi/static/js/main.js b/src/hi/static/js/main.js
index 8e49e324..f7b7281c 100644
--- a/src/hi/static/js/main.js
+++ b/src/hi/static/js/main.js
@@ -27,9 +27,6 @@
togglePasswordField: function( toggleCheckbox ) {
return _togglePasswordField( toggleCheckbox );
},
- changeAttributeValueType: function( selectElement ) {
- return _changeAttributeValueType( selectElement );
- },
getScreenCenterPoint: function( element ) {
return _getScreenCenterPoint( element );
},
@@ -83,18 +80,13 @@
let passwordField = $(toggleCheckbox).closest(Hi.FORM_FIELD_CONTAINER_SELECTOR).find('input[type="password"], input[type="text"]');
if ( toggleCheckbox.checked ) {
passwordField.attr('type', 'text');
+ $('label[for="' + $(toggleCheckbox).attr('id') + '"]').text('Hide');
} else {
passwordField.attr('type', 'password');
+ $('label[for="' + $(toggleCheckbox).attr('id') + '"]').text('Show');
}
}
- function _changeAttributeValueType( selectElement ) {
- const value_type = $(selectElement).val();
- $(selectElement).closest(Hi.ATTRIBUTE_CONTAINER_SELECTOR).find('[value-type]').hide();
- $(selectElement).closest(Hi.ATTRIBUTE_CONTAINER_SELECTOR).find(`[value-type="${value_type}"`).show();
-
- }
-
function _getScreenCenterPoint( element ) {
try {
let rect = $(element)[0].getBoundingClientRect();