summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2023-01-20 11:46:57 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2023-01-20 11:46:57 +0100
commitaf04b1ecfc22f8c104f6f960c26086c4284e9777 (patch)
tree5232bf428003effb1dcad3b23c3d49a976c39894
parent931092752a4db520becea7815e0d1e205b0a921e (diff)
downloadIshtar-af04b1ecfc22f8c104f6f960c26086c4284e9777.tar.bz2
Ishtar-af04b1ecfc22f8c104f6f960c26086c4284e9777.zip
Warehouse: new slug field to prevent ID change when name is changed
-rw-r--r--archaeological_warehouse/forms.py4
-rw-r--r--archaeological_warehouse/migrations/0115_auto_20230120_1133.py (renamed from archaeological_warehouse/migrations/0115_auto_20230119_1911.py)30
-rw-r--r--archaeological_warehouse/models.py17
-rw-r--r--archaeological_warehouse/tests.py6
-rw-r--r--ishtar_common/migrations/0223_auto_20230120_1124.py18
-rw-r--r--ishtar_common/models.py2
-rw-r--r--ishtar_common/models_common.py2
-rw-r--r--ishtar_common/utils.py3
8 files changed, 71 insertions, 11 deletions
diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py
index 828d651f1..a95289669 100644
--- a/archaeological_warehouse/forms.py
+++ b/archaeological_warehouse/forms.py
@@ -23,6 +23,7 @@ import datetime
from django import forms
from django.db.models import Max
from django.conf import settings
+from django.core.validators import validate_slug
from django.forms.formsets import formset_factory
from ishtar_common.utils import ugettext_lazy as _
@@ -201,6 +202,9 @@ class WarehouseForm(CustomForm, ManageOldType, forms.Form):
}
name = forms.CharField(label=_("Name"), max_length=200, validators=[name_validator])
+ slug = forms.CharField(label=_("Textual ID"), max_length=200,
+ validators=[validate_slug], required=False,
+ help_text=_("Auto filled if kept empty."))
warehouse_type = forms.ChoiceField(label=_("Warehouse type"), choices=[])
organization = forms.IntegerField(
label=_("Organization"),
diff --git a/archaeological_warehouse/migrations/0115_auto_20230119_1911.py b/archaeological_warehouse/migrations/0115_auto_20230120_1133.py
index cf678f2c2..8462d4bb8 100644
--- a/archaeological_warehouse/migrations/0115_auto_20230119_1911.py
+++ b/archaeological_warehouse/migrations/0115_auto_20230120_1133.py
@@ -1,9 +1,25 @@
-# Generated by Django 2.2.24 on 2023-01-19 19:11
+# Generated by Django 2.2.24 on 2023-01-20 11:33
from django.db import migrations, models
import django.db.models.deletion
+def copy_external_id_to_slug(apps, schema):
+ Warehouse = apps.get_model("archaeological_warehouse", "Warehouse")
+ for w in Warehouse.objects.all():
+ w.skip_history_when_saving = True
+ w.slug = w.external_id
+ w.save()
+
+
+def update_profile(apps, schema):
+ IshtarSiteProfile = apps.get_model("ishtar_common", "IshtarSiteProfile")
+ for p in IshtarSiteProfile.objects.filter(
+ warehouse_external_id="{name|slug}").all():
+ p.warehouse_external_id = "{slug}"
+ p.save()
+
+
class Migration(migrations.Migration):
dependencies = [
@@ -17,10 +33,20 @@ class Migration(migrations.Migration):
field=models.TextField(blank=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached town label'),
),
migrations.AddField(
+ model_name='historicalwarehouse',
+ name='slug',
+ field=models.SlugField(blank=True, default='', max_length=200, verbose_name='Textual ID'),
+ ),
+ migrations.AddField(
model_name='warehouse',
name='cached_town_label',
field=models.TextField(blank=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached town label'),
),
+ migrations.AddField(
+ model_name='warehouse',
+ name='slug',
+ field=models.SlugField(blank=True, default='', max_length=200, verbose_name='Textual ID'),
+ ),
migrations.AlterField(
model_name='container',
name='geodata',
@@ -46,4 +72,6 @@ class Migration(migrations.Migration):
name='main_geodata',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='main_related_items_archaeological_warehouse_warehouse', to='ishtar_common.GeoVectorData', verbose_name='Main geodata'),
),
+ migrations.RunPython(copy_external_id_to_slug),
+ migrations.RunPython(update_profile),
]
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py
index 3f4913d4b..5d96e6863 100644
--- a/archaeological_warehouse/models.py
+++ b/archaeological_warehouse/models.py
@@ -320,6 +320,7 @@ class Warehouse(
}
GEO_LABEL = "name"
DOWN_MODEL_UPDATE = ["containers"]
+ FORCE_CASCADE_UPDATE = True
CACHED_LABELS = ["cached_town_label"]
QA_EDIT = QuickAction(
@@ -346,6 +347,7 @@ class Warehouse(
uuid = models.UUIDField(default=uuid.uuid4)
name = models.CharField(_("Name"), max_length=200)
+ slug = models.SlugField(_("Textual ID"), blank=True, default="", max_length=200)
warehouse_type = models.ForeignKey(
WarehouseType, verbose_name=_("Warehouse type"), on_delete=models.PROTECT
)
@@ -658,15 +660,20 @@ class Warehouse(
self.update_search_vector()
super(Warehouse, self).save(*args, **kwargs)
- self.skip_history_when_saving = True
+ updated = False
+ if not self.slug and self.name:
+ self.slug = slugify(self.name)[:200]
+ updated = True
if not self.external_id or self.auto_external_id:
external_id = get_generated_id("warehouse_external_id", self)
if external_id != self.external_id:
- self.auto_external_id = True
self.external_id = external_id
- self._cached_label_checked = False
- self.save()
- return
+ updated = True
+ if updated:
+ self.skip_history_when_saving = True
+ self._cached_label_checked = False
+ self.force_update = True
+ self.save()
m2m_changed.connect(document_attached_changed, sender=Warehouse.documents.through)
diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py
index e67d03542..e211e3a2d 100644
--- a/archaeological_warehouse/tests.py
+++ b/archaeological_warehouse/tests.py
@@ -731,8 +731,10 @@ class ContainerTest(FindInit, TestCase):
container.save()
container = models.Container.objects.get(pk=container.pk)
self.assertIn(self.main_warehouse.name, container.cached_location)
- self.main_warehouse.name = "New name"
- self.main_warehouse.save()
+ warehouse = models.Warehouse.objects.get(pk=self.main_warehouse.pk)
+ warehouse.name = "New name"
+ warehouse.slug = "new-name"
+ warehouse.save()
self.assertEqual(models.Container.objects.filter(need_update=True).count(), 1)
self.assertEqual(
models.Container.objects.filter(pk=container.pk, need_update=True).count(),
diff --git a/ishtar_common/migrations/0223_auto_20230120_1124.py b/ishtar_common/migrations/0223_auto_20230120_1124.py
new file mode 100644
index 000000000..a4fe554de
--- /dev/null
+++ b/ishtar_common/migrations/0223_auto_20230120_1124.py
@@ -0,0 +1,18 @@
+# Generated by Django 2.2.24 on 2023-01-20 11:24
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('ishtar_common', '0222_auto_20230111_1857'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='ishtarsiteprofile',
+ name='warehouse_external_id',
+ field=models.TextField(default='{slug}', help_text='Formula to manage warehouse external ID. Change this with care. With incorrect formula, the application might be unusable and import of external data can be destructive.', verbose_name='Warehouse external id'),
+ ),
+ ]
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index fc2487d7b..5dc325e4e 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -1349,7 +1349,7 @@ class IshtarSiteProfile(models.Model, Cached):
)
warehouse_external_id = models.TextField(
_("Warehouse external id"),
- default="{name|slug}",
+ default="{slug}",
help_text=_(
"Formula to manage warehouse external ID. "
"Change this with care. With incorrect formula, the "
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py
index 586549542..b239813f3 100644
--- a/ishtar_common/models_common.py
+++ b/ishtar_common/models_common.py
@@ -1241,7 +1241,7 @@ class CascasdeUpdate:
continue
for item in getattr(self, down_model).all():
cached_label_changed(item.__class__, instance=item)
- if hasattr(item, "point_2d"):
+ if hasattr(item, "main_geodata"):
post_save_geo(item.__class__, instance=item)
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index 1219dd454..750154e0c 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -577,7 +577,8 @@ def _cached_label_changed(sender, **kwargs):
if hasattr(instance, "_cascade_change") and instance._cascade_change:
instance.skip_history_when_saving = True
instance.__class__.objects.filter(pk=instance.pk).update(**dict(changed))
- if (changed or not cached_labels) and hasattr(instance, "cascade_update"):
+ if (getattr(instance, "FORCE_CASCADE_UPDATE", False) or changed
+ or not cached_labels) and hasattr(instance, "cascade_update"):
instance.cascade_update()
updated = False
if force_update or hasattr(instance, "update_search_vector"):