summaryrefslogtreecommitdiff
path: root/archaeological_warehouse
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_warehouse')
-rw-r--r--archaeological_warehouse/migrations/0124_warehouse_cached_town.py31
-rw-r--r--archaeological_warehouse/migrations/0125_migrate_cached_town.py29
-rw-r--r--archaeological_warehouse/models.py60
-rw-r--r--archaeological_warehouse/tests.py43
4 files changed, 147 insertions, 16 deletions
diff --git a/archaeological_warehouse/migrations/0124_warehouse_cached_town.py b/archaeological_warehouse/migrations/0124_warehouse_cached_town.py
new file mode 100644
index 000000000..669bcd3af
--- /dev/null
+++ b/archaeological_warehouse/migrations/0124_warehouse_cached_town.py
@@ -0,0 +1,31 @@
+# Generated by Django 2.2.24 on 2025-10-24 15:21
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('archaeological_warehouse', '0123_warehouse_container_ishtar_users'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='historicalwarehouse',
+ name='cached_town',
+ field=models.TextField(blank=True, default='', verbose_name='Cached town'),
+ ),
+ migrations.AddField(
+ model_name='warehouse',
+ name='cached_town',
+ field=models.TextField(blank=True, default='', verbose_name='Cached town'),
+ ),
+ migrations.RemoveField(
+ model_name='historicalwarehouse',
+ name='cached_town_label',
+ ),
+ migrations.RemoveField(
+ model_name='warehouse',
+ name='cached_town_label',
+ ),
+ ]
diff --git a/archaeological_warehouse/migrations/0125_migrate_cached_town.py b/archaeological_warehouse/migrations/0125_migrate_cached_town.py
new file mode 100644
index 000000000..49f914185
--- /dev/null
+++ b/archaeological_warehouse/migrations/0125_migrate_cached_town.py
@@ -0,0 +1,29 @@
+# Generated by Django 2.2.24 on 2025-10-24 12:59
+
+from django.db import migrations
+
+
+def _generate_cached_town(Town, town):
+ if town.precise_town_id:
+ return Town.objects.get(id=town.precise_town_id).name
+ return town.town or ""
+
+
+def migrate_cached_towns(apps, __):
+ Town = apps.get_model("ishtar_common", "Town")
+ Warehouse = apps.get_model("archaeological_warehouse", "Warehouse")
+ for warehouse in Warehouse.objects.all():
+ Warehouse.objects.filter(pk=warehouse.pk).update(
+ cached_town=_generate_cached_town(Town, warehouse),
+ )
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('archaeological_warehouse', '0124_warehouse_cached_town'),
+ ]
+
+ operations = [
+ migrations.RunPython(migrate_cached_towns)
+ ]
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py
index 993ab3362..2cdbee13c 100644
--- a/archaeological_warehouse/models.py
+++ b/archaeological_warehouse/models.py
@@ -293,10 +293,10 @@ class Warehouse(
SHOW_URL = "show-warehouse"
DELETE_URL = "delete-warehouse"
DOWN_MODEL_UPDATE = ["containers", "responsibilities"]
- TABLE_COLS = ["name", "warehouse_type__label", "cached_town_label"]
+ TABLE_COLS = ["name", "warehouse_type__label", "cached_town"]
COL_LABELS = {
"warehouse_type__label": _("Type"),
- "cached_town_label": _("Town"),
+ "cached_town": _("Town"),
}
INITIAL_VALUES = ["name", "slug"]
NEW_QUERY_ENGINE = True
@@ -339,7 +339,7 @@ class Warehouse(
DEFAULT_SEARCH_FORM = ("archaeological_warehouse.forms", "WarehouseSelect")
GEO_LABEL = "name"
- CACHED_LABELS = ["cached_town_label"]
+ CACHED_LABELS = ["cached_town"]
QA_EDIT = QuickAction(
url="warehouse-qa-bulk-update",
@@ -432,12 +432,6 @@ class Warehouse(
default=0,
help_text=_("Automatically generated"),
)
- cached_town_label = models.TextField(
- _("Cached town label"),
- blank=True,
- default="",
- help_text=_("Generated automatically - do not edit"),
- )
SUB_ADDRESSES = ["organization", "person_in_charge"]
class Meta:
@@ -649,13 +643,6 @@ class Warehouse(
wdiv.delete()
merge_model_objects(self, item, keep_old=keep_old)
- def _generate_cached_town_label(self):
- if self.precise_town:
- return str(self.precise_town)
- if self.town:
- return self.town
- return ""
-
def check_cascade_update(self):
# arbitrary check if a "location" or a "responsibility" container are impacted by a change
# arbitrary because check every formula is not efficient
@@ -2084,9 +2071,46 @@ class Container(
q.update(cached_division="")
return ((self.__class__, q.values_list("id", flat=True)),)
+ def _update_childs_divisions(self):
+ Find = apps.get_model("archaeological_finds", "Find")
+ q = Container.objects.filter(parent_id=self.id)
+ for child in q.all():
+ self.update_weight()
+ cached_division = child._generate_cached_division()
+ cached_label = child._generate_cached_label()
+ cached_weight = child._generate_cached_weight()
+ Container.objects.filter(pk=child.pk).update(
+ cached_division=cached_division,
+ cached_label=cached_label,
+ cached_weight=cached_weight
+ )
+
+ # update location of associated find
+ full_location = Container.objects.get(pk=child.pk).generate_full_location()
+ for find in child.finds_ref.all():
+ value = find.update_ref_full_location(full_location, return_value=True)
+ if value is not False:
+ Find.objects.filter(pk=find.pk).update(
+ container_ref_fisrt_full_location=value
+ )
+ for find in child.finds.all():
+ value = find.update_current_full_location(full_location, return_value=True)
+ if value is not False:
+ Find.objects.filter(pk=find.pk).update(
+ container_fisrt_full_location=value
+ )
+
+ # TODO update last treatment?
+ # warning - if a new treatment is created via move container
+ # it will be created after this is triggered... not easy to manage...
+
+ child._update_childs_divisions()
+
def save(self, *args, **kwargs):
+ self._cached_location, self._cached_parent = None, None
if self.pk:
logger.debug(f"[ishtar] archaeological_warehouse.models.Container.save - {self.pk} - {self.cached_label}")
+ self._cached_location, self._cached_parent = Container.objects.filter(pk=self.pk).values_list("location_id", "parent_id").all()[0]
self.pre_save()
super().save(*args, **kwargs)
self.update_find_location()
@@ -2097,6 +2121,10 @@ class Container(
updated += self._calculate_weight()
+ # update cached_label for childs
+ if self._cached_location != self.location_id or self._cached_parent != self.parent_id:
+ self._update_childs_divisions()
+
if not self.index and not self.container_type.stationary:
self.skip_history_when_saving = True
q = (
diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py
index 4290e2c42..382efb769 100644
--- a/archaeological_warehouse/tests.py
+++ b/archaeological_warehouse/tests.py
@@ -880,10 +880,33 @@ class ContainerTest(FindInit, TestCase):
def test_change_location(self):
find = self.create_finds()[0][0]
find = Find.objects.get(pk=find.pk)
+
+ main_container_1 = models.Container.objects.create(
+ reference="Test - main container",
+ responsible=self.main_warehouse,
+ location=self.main_warehouse,
+ container_type=models.ContainerType.objects.all()[1],
+ )
+ main_container_2 = models.Container.objects.create(
+ reference="Test - main container 2",
+ responsible=self.main_warehouse,
+ location=self.main_warehouse,
+ container_type=models.ContainerType.objects.all()[1],
+ )
+ top_container_1 = models.Container.objects.create(
+ reference="Test - top container",
+ responsible=self.main_warehouse,
+ location=self.main_warehouse,
+ container_type=models.ContainerType.objects.all()[1],
+ parent=main_container_1
+ )
+
+ # simple localisation
container = models.Container.objects.create(
reference="Test",
responsible=self.main_warehouse,
location=self.main_warehouse,
+ parent=top_container_1,
container_type=models.ContainerType.objects.all()[0],
)
container.save()
@@ -914,6 +937,26 @@ class ContainerTest(FindInit, TestCase):
division__warehouse=self.main_warehouse
).count()
)
+
+ # test cascade update
+ top_container_1.parent = main_container_2
+ top_container_1.save()
+ container = models.Container.objects.get(pk=container.pk)
+ container_2 = models.Container.objects.get(pk=container_2.pk)
+ find = Find.objects.get(pk=find.pk)
+ self.assertTrue(
+ find.container_fisrt_full_location.startswith(
+ main_container_2.generate_full_location()
+ )
+ )
+ self.assertTrue(
+ find.container_ref_fisrt_full_location.startswith(
+ main_container_2.generate_full_location()
+ )
+ )
+ self.assertEqual(find.container_fisrt_full_location, container.generate_full_location())
+ self.assertEqual(find.container_ref_fisrt_full_location, container_2.generate_full_location())
+
# changing location remove irrelevant localisation
other_warehouse = models.Warehouse.objects.create(
name="Other", warehouse_type=models.WarehouseType.objects.all()[0]