Skip to content

Home Information - Your home, your data, organized.

Python

Attribute Order Improvements (#246)

Thiago Trautwein Santosd38e299bb939
diff --git a/src/hi/apps/attribute/forms.py b/src/hi/apps/attribute/forms.py
index 90097d56..eaff8d15 100644
--- a/src/hi/apps/attribute/forms.py
+++ b/src/hi/apps/attribute/forms.py
@@ -41,6 +41,7 @@ class AttributeForm( forms.ModelForm ):
fields = (
'name',
'value',
+ 'order_id',
)
widgets = {
'name': forms.TextInput( attrs={'class': 'form-control'} ),
@@ -53,6 +54,12 @@ class AttributeForm( forms.ModelForm ):
widget = forms.CheckboxInput(attrs={'class': 'custom-control-input'}),
)
+ order_id = forms.IntegerField(
+ required = False,
+ label = 'Ordering Index',
+ widget = forms.HiddenInput(),
+ )
+
@property
def show_as_editable(self):
return self._show_as_editable
diff --git a/src/hi/apps/attribute/models.py b/src/hi/apps/attribute/models.py
index 83f248d2..a28bfba2 100644
--- a/src/hi/apps/attribute/models.py
+++ b/src/hi/apps/attribute/models.py
@@ -49,6 +49,7 @@ class AttributeModel(models.Model):
class Meta:
abstract = True
+ ordering = ['order_id', 'id']
name = models.CharField(
'Name',
@@ -103,6 +104,10 @@ class AttributeModel(models.Model):
auto_now=True,
blank = True,
)
+ order_id = models.PositiveIntegerField(
+ 'Ordering Index',
+ default = 0,
+ )
def get_upload_to(self):
raise NotImplementedError('Subclasses should override this method.' )
diff --git a/src/hi/apps/attribute/templates/attribute/components/regular_attribute_card.html b/src/hi/apps/attribute/templates/attribute/components/regular_attribute_card.html
index bc2f9080..a8b1039b 100644
--- a/src/hi/apps/attribute/templates/attribute/components/regular_attribute_card.html
+++ b/src/hi/apps/attribute/templates/attribute/components/regular_attribute_card.html
@@ -1,6 +1,8 @@
{% load icons %}
{% load common_tags %}
{% load attribute_extras %}
+{% load static %}
+
{% comment %}
V2 Attribute Card Component
Individual attribute attribute card with value editing and actions
@@ -9,7 +11,6 @@ Individual attribute attribute card with value editing and actions
<div class="{{ DIVID.ATTR_V2_ATTRIBUTE_CARD_CLASS }}{% if not attribute_form.instance.pk %} {{ DIVID.ATTR_V2_NEW_ATTRIBUTE_CLASS }}{% endif %}"
data-attribute-id="{{ attribute_form.instance.pk }}"
data-url-detect="true"
- data-order-index="{{ forloop.counter }}"
data-value-type="{{ attribute_form.instance.value_type.name|default:'text' }}"
data-is-secret="{{ attribute_form.instance.value_type.is_secret|yesno:'true,false' }}"
data-can-delete="{{ attribute_form.instance.attribute_type.can_delete|yesno:'true,false' }}"
@@ -18,9 +19,14 @@ Individual attribute attribute card with value editing and actions
<!-- Hidden form fields -->
{{ attribute_form.id }}
+
{% if attribute_form.DELETE %}
{{ attribute_form.DELETE.as_hidden }}
{% endif %}
+
+ {% if attribute_form.order_id %}
+ {{ attribute_form.order_id.as_hidden }}
+ {% endif %}
{% if attribute_form.instance and not attribute_form.instance.is_editable %}
<div class="text-muted text-center"><small>SYSTEM INFO</small></div>
@@ -59,17 +65,13 @@ Individual attribute attribute card with value editing and actions
<!-- Reorder Controls (disabled until Issue #80, hidden if allow_reordering is False) -->
{% if attribute_form.instance.pk and attribute_form.allow_reordering %}
<button type="button"
- class="btn btn-sm btn-outline-secondary"
- disabled
- title="Reordering available in future update"
- data-reorder-action="up">
+ class="btn btn-sm btn-outline-secondary"
+ onclick="Hi.attr.reorderAttributeCard(this, 'up')">
{% icon "chevron-up" size="sm" %}
</button>
<button type="button"
- class="btn btn-sm btn-outline-secondary"
- disabled
- title="Reordering available in future update"
- data-reorder-action="down">
+ class="btn btn-sm btn-outline-secondary"
+ onclick="Hi.attr.reorderAttributeCard(this, 'down')">
{% icon "chevron-down" size="sm" %}
</button>
{% endif %}
diff --git a/src/hi/apps/attribute/templates/attribute/components/regular_attribute_list.html b/src/hi/apps/attribute/templates/attribute/components/regular_attribute_list.html
index 671f46d9..b47c2a06 100644
--- a/src/hi/apps/attribute/templates/attribute/components/regular_attribute_list.html
+++ b/src/hi/apps/attribute/templates/attribute/components/regular_attribute_list.html
@@ -1,3 +1,4 @@
+{% load static %}
{% if regular_attributes_formset %}
{{ regular_attributes_formset.management_form }}
<div class="attr-v2-attribute-section">
diff --git a/src/hi/apps/config/migrations/0005_subsystemattribute_order_id.py b/src/hi/apps/config/migrations/0005_subsystemattribute_order_id.py
new file mode 100644
index 00000000..ced5de87
--- /dev/null
+++ b/src/hi/apps/config/migrations/0005_subsystemattribute_order_id.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.2.25 on 2026-01-09 11:13
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("config", "0004_subsystemattributehistory"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="subsystemattribute",
+ name="order_id",
+ field=models.PositiveIntegerField(default=0, verbose_name="Ordering Index"),
+ ),
+ ]
diff --git a/src/hi/apps/entity/migrations/0009_entityattribute_order_id.py b/src/hi/apps/entity/migrations/0009_entityattribute_order_id.py
new file mode 100644
index 00000000..f1b245d4
--- /dev/null
+++ b/src/hi/apps/entity/migrations/0009_entityattribute_order_id.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.2.25 on 2026-01-09 11:13
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("entity", "0008_fix_multvalued_typo"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="entityattribute",
+ name="order_id",
+ field=models.PositiveIntegerField(default=0, verbose_name="Ordering Index"),
+ ),
+ ]
diff --git a/src/hi/apps/entity/models.py b/src/hi/apps/entity/models.py
index 9f720988..0d4e284f 100644
--- a/src/hi/apps/entity/models.py
+++ b/src/hi/apps/entity/models.py
@@ -115,6 +115,7 @@ class EntityAttribute( AttributeModel ):
indexes = [
models.Index( fields=[ 'name', 'value' ] ),
]
+ ordering = ['order_id', 'id']
def get_upload_to(self):
return 'entity/attributes/'
diff --git a/src/hi/apps/location/migrations/0004_locationattribute_order_id.py b/src/hi/apps/location/migrations/0004_locationattribute_order_id.py
new file mode 100644
index 00000000..5dd1bc99
--- /dev/null
+++ b/src/hi/apps/location/migrations/0004_locationattribute_order_id.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.2.25 on 2026-01-09 11:13
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("location", "0003_alter_location_order_id_alter_locationview_order_id"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="locationattribute",
+ name="order_id",
+ field=models.PositiveIntegerField(default=0, verbose_name="Ordering Index"),
+ ),
+ ]
diff --git a/src/hi/integrations/migrations/0003_integrationattribute_order_id.py b/src/hi/integrations/migrations/0003_integrationattribute_order_id.py
new file mode 100644
index 00000000..236d2104
--- /dev/null
+++ b/src/hi/integrations/migrations/0003_integrationattribute_order_id.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.2.25 on 2026-01-09 11:13
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("integrations", "0002_integrationattributehistory"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="integrationattribute",
+ name="order_id",
+ field=models.PositiveIntegerField(default=0, verbose_name="Ordering Index"),
+ ),
+ ]
diff --git a/src/hi/static/js/attr-dirty-tracking.js b/src/hi/static/js/attr-dirty-tracking.js
index a3f63482..9de37ba7 100644
--- a/src/hi/static/js/attr-dirty-tracking.js
+++ b/src/hi/static/js/attr-dirty-tracking.js
@@ -87,6 +87,12 @@
if (field.type === 'hidden' && field.name.includes('_')) return;
this.captureFieldValue(field);
});
+
+ // Capture order fields so reorders can be tracked as dirty
+ const orderFields = form.querySelectorAll(`${Hi.ATTR_V2_ATTRIBUTE_CARD_SELECTOR} input[type="hidden"][name$="-order_id"]`);
+ orderFields.forEach(field => {
+ this.captureFieldValue(field);
+ });
// File title input fields
const fileTitleFields = form.querySelectorAll(Hi.ATTR_V2_FILE_TITLE_INPUT_SELECTOR);
@@ -371,6 +377,17 @@
}
});
},
+
+ // Evaluate order field changes after reordering
+ handleOrderFieldChanges: function() {
+ const form = this.container.querySelector(this.config.formSelector);
+ if (!form) return;
+
+ const orderFields = form.querySelectorAll(`${Hi.ATTR_V2_ATTRIBUTE_CARD_SELECTOR} input[type="hidden"][name$="-order_id"]`);
+ orderFields.forEach(field => {
+ this.handleFieldChange(field);
+ });
+ },
// Handle successful form submission
handleFormSuccess: function(e) {
diff --git a/src/hi/static/js/attr.js b/src/hi/static/js/attr.js
index 1f51423d..c39f11b3 100644
--- a/src/hi/static/js/attr.js
+++ b/src/hi/static/js/attr.js
@@ -115,7 +115,11 @@
toggleExpandedView: function(button) {
return _toggleExpandedView(button);
},
-
+
+ reorderAttributeCard: function(button, direction) {
+ return _reorderAttributeCard(button, direction);
+ },
+
// Initialization
init: function() {
_initializeAllContainers();
@@ -141,6 +145,7 @@
const $container = $form.closest(Hi.ATTR_V2_CONTAINER_SELECTOR);
if ($container.length > 0) {
_syncTextareaValuesToHiddenFields($container);
+ _updateOrderIndexes($container);
}
const formData = new FormData($form[0]);
@@ -1164,6 +1169,42 @@
displayField.off('input.overflow');
}
}
+
+ function _reorderAttributeCard(button, direction) {
+ const card = button.closest(`[${Hi.DATA_ATTRIBUTE_ID_ATTR}]`);
+ const parent = card?.parentElement;
+
+ if (!parent) return;
+
+ switch (direction) {
+ case "up":
+ const prev = card.previousElementSibling;
+ if (prev) parent.insertBefore(card, prev);
+ break;
+
+ case "down":
+ const next = card.nextElementSibling;
+ if (next) parent.insertBefore(next, card);
+ break;
+
+ default:
+ console.error(`Invalid direction: ${direction}`);
+ return;
+ }
+
+ const $container = $(card).closest(Hi.ATTR_V2_CONTAINER_SELECTOR);
+ if ($container.length > 0) {
+ _updateOrderIndexes($container);
+
+ if (window.Hi.attr.dirtyTracking) {
+ const containerId = $container.attr('id');
+ if (containerId) {
+ const instance = window.Hi.attr.dirtyTracking.getInstance(containerId);
+ instance.handleOrderFieldChanges();
+ }
+ }
+ }
+ }
// Sync textarea values to hidden fields before form submission
function _syncTextareaValuesToHiddenFields($container) {
@@ -1184,6 +1225,31 @@
}
});
}
-
+
+ function _updateOrderIndexes($container) {
+ let order = 1;
+
+ const $cards = $container.find(Hi.ATTR_V2_ATTRIBUTE_CARD_SELECTOR);
+
+ $cards.each(function() {
+ const $card = $(this);
+ const attrId = $card.attr(Hi.DATA_ATTRIBUTE_ID_ATTR);
+
+ const nameVal = ($card.find('input[name$="-name"]').val() || '').trim();
+ const valueVal = ($card.find('textarea[name$="-value"], input[name$="-value"]').val() || '').trim();
+
+ const isNew = attrId === 'None';
+ const isFilled = (nameVal.length > 0 || valueVal.length > 0);
+
+ if (isNew && !isFilled) return;
+
+ const $orderField = $card.find('input[type="hidden"][name$="-order_id"]');
+ if ($orderField.length > 0) {
+ $orderField.val(String(order));
+ }
+
+ order += 1;
+ });
+ }
})();