diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-01-27 17:31:00 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-01-27 17:31:00 +0100 |
commit | 466398d76996c5219491807e28709ec17706e8d3 (patch) | |
tree | dc2627a86507fe03bf42b21bcc5fe21183573cf4 | |
parent | fe841330a8811a6a5b828c93520aae2727661dcb (diff) | |
download | Ishtar-466398d76996c5219491807e28709ec17706e8d3.tar.bz2 Ishtar-466398d76996c5219491807e28709ec17706e8d3.zip |
Add custom cached_label configuration for each main item
-rw-r--r-- | CHANGES.md | 5 | ||||
-rw-r--r-- | archaeological_context_records/migrations/0111_auto_20230127_1503.py | 54 | ||||
-rw-r--r-- | archaeological_context_records/models.py | 25 | ||||
-rw-r--r-- | archaeological_context_records/tests.py | 26 | ||||
-rw-r--r-- | archaeological_files/migrations/0110_auto_20230127_1503.py | 24 | ||||
-rw-r--r-- | archaeological_files/models.py | 10 | ||||
-rw-r--r-- | archaeological_finds/migrations/0111_auto_20230127_1504.py | 160 | ||||
-rw-r--r-- | archaeological_finds/models_finds.py | 49 | ||||
-rw-r--r-- | archaeological_finds/models_treatments.py | 16 | ||||
-rw-r--r-- | archaeological_operations/migrations/0111_auto_20230127_1503.py | 54 | ||||
-rw-r--r-- | archaeological_operations/models.py | 28 | ||||
-rw-r--r-- | archaeological_warehouse/migrations/0116_auto_20230127_1504.py | 28 | ||||
-rw-r--r-- | archaeological_warehouse/models.py | 6 | ||||
-rw-r--r-- | ishtar_common/admin.py | 11 | ||||
-rw-r--r-- | ishtar_common/migrations/0224_auto_20230127_1503.py | 68 | ||||
-rw-r--r-- | ishtar_common/models.py | 62 | ||||
-rw-r--r-- | ishtar_common/models_common.py | 20 | ||||
-rw-r--r-- | ishtar_common/utils.py | 12 |
18 files changed, 606 insertions, 52 deletions
diff --git a/CHANGES.md b/CHANGES.md index fbb33b9b0..d8b1f9649 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,11 +1,14 @@ --- title: Ishtar changelog -date: 2023-01-23 +date: 2023-01-27 --- Ishtar changelog ================ +### Features/improvements ### +- add custom cached_label configuration for each main item + v4.0.42 - 2023-01-25 -------------------- diff --git a/archaeological_context_records/migrations/0111_auto_20230127_1503.py b/archaeological_context_records/migrations/0111_auto_20230127_1503.py new file mode 100644 index 000000000..b1d36254c --- /dev/null +++ b/archaeological_context_records/migrations/0111_auto_20230127_1503.py @@ -0,0 +1,54 @@ +# Generated by Django 2.2.24 on 2023-01-27 15:03 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_context_records', '0110_auto_20221017_1435'), + ] + + operations = [ + migrations.AlterField( + model_name='contextrecord', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AlterField( + model_name='contextrecord', + name='excavation_technics', + field=models.ManyToManyField(blank=True, related_name='context_records', to='archaeological_context_records.ExcavationTechnicType', verbose_name='Excavation techniques'), + ), + migrations.AlterField( + model_name='contextrecord', + name='geodata', + field=models.ManyToManyField(blank=True, related_name='related_items_archaeological_context_records_contextrecord', to='ishtar_common.GeoVectorData', verbose_name='Geodata'), + ), + migrations.AlterField( + model_name='contextrecord', + name='main_geodata', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='main_related_items_archaeological_context_records_contextrecord', to='ishtar_common.GeoVectorData', verbose_name='Main geodata'), + ), + migrations.AlterField( + model_name='dating', + name='period', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='archaeological_operations.Period', verbose_name='Chronological period'), + ), + migrations.AlterField( + model_name='dating', + name='precise_dating', + field=models.TextField(blank=True, default='', verbose_name='Precise on dating'), + ), + migrations.AlterField( + model_name='historicalcontextrecord', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AlterField( + model_name='historicalcontextrecord', + name='main_geodata', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.GeoVectorData', verbose_name='Main geodata'), + ), + ] diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 074bc3358..bfdda4bfe 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -838,9 +838,6 @@ class ContextRecord( blank=True, null=True, ) - cached_label = models.TextField( - _("Cached name"), blank=True, default="", db_index=True - ) cached_periods = models.TextField( _("Cached periods label"), blank=True, @@ -997,6 +994,25 @@ class ContextRecord( def cached_label_bulk_update( cls, operation_id=None, parcel_id=None, transaction_id=None ): + profile = get_current_profile() + if profile.contextrecord_cached_label: + # no bulk possible + q = cls.objects + if operation_id: + q = q.filter(operation_id=operation_id) + elif parcel_id: + q = q.filter(parcel_id=parcel_id) + else: + return + for cr in q.all(): + cr.skip_history_when_saving = True + cr._no_move = True + cr.save() + cls._meta.get_field("base_finds").related_model.cached_label_bulk_update( + operation_id=operation_id, parcel_id=parcel_id, + transaction_id=transaction_id + ) + return transaction_id, is_recursion = cls.bulk_recursion( transaction_id, [operation_id, parcel_id] ) @@ -1186,6 +1202,9 @@ class ContextRecord( ) def _generate_cached_label(self): + label = self._profile_generate_cached_label() + if label: + return label return self.full_label() def _generate_cached_periods(self): diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index f0b12e0d6..28aa2208b 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -27,7 +27,7 @@ from django.conf import settings from django.contrib.auth.models import Permission from django.core.exceptions import ValidationError, ImproperlyConfigured from django.core.files.uploadedfile import SimpleUploadedFile -from django.test import tag +from django.template.defaultfilters import slugify from django.test.client import Client from django.urls import reverse from django.utils.translation import pgettext_lazy @@ -438,6 +438,30 @@ class ContextRecordTest(ContextRecordInit, TestCase): self.assertIsNotNone(cr.cached_label) self.assertIn(profile.operation_prefix.lower() + "patriarch", cr.search_vector) + def test_custom_cached_label(self): + profile, created = IshtarSiteProfile.objects.get_or_create( + slug="default", active=True + ) + profile.contextrecord_cached_label = \ + "OA{operation__code_patriarche}-{label|slug:0>7}" + profile.save() + cr1 = self.context_records[0] + cr1 = models.ContextRecord.objects.get(pk=cr1.pk) + cr1.save() + cr1 = models.ContextRecord.objects.get(pk=cr1.pk) + code_patriarche = cr1.operation.code_patriarche + label = slugify(cr1.label) + # label = "CR 1" - code patriarche = "1" + # -> OA1-000cr-1 + expected_lbl = f"OA{code_patriarche}-{label:0>7}" + self.assertEqual(cr1.cached_label, expected_lbl) + # test cascade update + cr1.operation.code_patriarche = "666" + cr1.operation.save() + cr1 = models.ContextRecord.objects.get(pk=cr1.pk) + expected_lbl = f"OA666-{label:0>7}" + self.assertEqual(cr1.cached_label, expected_lbl) + def test_upstream_cache_update(self): cr = self.create_context_record()[0] cr_pk = cr.pk diff --git a/archaeological_files/migrations/0110_auto_20230127_1503.py b/archaeological_files/migrations/0110_auto_20230127_1503.py new file mode 100644 index 000000000..6747a4117 --- /dev/null +++ b/archaeological_files/migrations/0110_auto_20230127_1503.py @@ -0,0 +1,24 @@ +# Generated by Django 2.2.24 on 2023-01-27 15:03 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_files', '0109_auto_20220711_1024'), + ] + + operations = [ + migrations.AlterField( + model_name='equipmentservicecost', + name='price_agreement', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='equipment_service_costs', to='archaeological_files.PriceAgreement', verbose_name='Price agreement'), + ), + migrations.AlterField( + model_name='job', + name='price_agreement', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='jobs', to='archaeological_files.PriceAgreement', verbose_name='Price agreement'), + ), + ] diff --git a/archaeological_files/models.py b/archaeological_files/models.py index bb31c1c24..529e3e0e6 100644 --- a/archaeological_files/models.py +++ b/archaeological_files/models.py @@ -719,13 +719,6 @@ class File( Document, related_name="files", verbose_name=_("Documents"), blank=True ) - cached_label = models.TextField( - _("Cached name"), - blank=True, - default="", - db_index=True, - help_text=_("Generated automatically - do not edit"), - ) imported_line = models.TextField(_("Imported line"), blank=True, default="") history = HistoricalRecords(bases=[HistoryModel]) @@ -1009,6 +1002,9 @@ class File( return self.external_id or "" def _generate_cached_label(self): + label = self._profile_generate_cached_label() + if label: + return label items = [self.get_town_label(), self.reference] items += [ str(getattr(self, k)) diff --git a/archaeological_finds/migrations/0111_auto_20230127_1504.py b/archaeological_finds/migrations/0111_auto_20230127_1504.py new file mode 100644 index 000000000..e1d833e03 --- /dev/null +++ b/archaeological_finds/migrations/0111_auto_20230127_1504.py @@ -0,0 +1,160 @@ +# Generated by Django 2.2.24 on 2023-01-27 15:04 + +from django.db import migrations, models +import django.db.models.deletion +import ishtar_common.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_finds', '0110_auto_20220707_1633'), + ] + + operations = [ + migrations.AddField( + model_name='basefind', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AddField( + model_name='historicalbasefind', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AddField( + model_name='historicaltreatment', + name='complete_identifier', + field=models.TextField(blank=True, default='', verbose_name='Complete identifier'), + ), + migrations.AddField( + model_name='historicaltreatment', + name='custom_index', + field=models.IntegerField(blank=True, null=True, verbose_name='Custom index'), + ), + migrations.AddField( + model_name='historicaltreatment', + name='qrcode', + field=models.TextField(blank=True, max_length=255, null=True), + ), + migrations.AddField( + model_name='historicaltreatmentfile', + name='complete_identifier', + field=models.TextField(blank=True, default='', verbose_name='Complete identifier'), + ), + migrations.AddField( + model_name='historicaltreatmentfile', + name='custom_index', + field=models.IntegerField(blank=True, null=True, verbose_name='Custom index'), + ), + migrations.AddField( + model_name='historicaltreatmentfile', + name='qrcode', + field=models.TextField(blank=True, max_length=255, null=True), + ), + migrations.AddField( + model_name='treatment', + name='complete_identifier', + field=models.TextField(blank=True, default='', verbose_name='Complete identifier'), + ), + migrations.AddField( + model_name='treatment', + name='custom_index', + field=models.IntegerField(blank=True, null=True, verbose_name='Custom index'), + ), + migrations.AddField( + model_name='treatment', + name='qrcode', + field=models.ImageField(blank=True, max_length=255, null=True, upload_to=ishtar_common.utils.get_image_path), + ), + migrations.AddField( + model_name='treatmentfile', + name='complete_identifier', + field=models.TextField(blank=True, default='', verbose_name='Complete identifier'), + ), + migrations.AddField( + model_name='treatmentfile', + name='custom_index', + field=models.IntegerField(blank=True, null=True, verbose_name='Custom index'), + ), + migrations.AddField( + model_name='treatmentfile', + name='qrcode', + field=models.ImageField(blank=True, max_length=255, null=True, upload_to=ishtar_common.utils.get_image_path), + ), + migrations.AlterField( + model_name='basefind', + name='discovery_date', + field=models.DateField(blank=True, null=True, verbose_name='Discovery date (exact or beginning)'), + ), + migrations.AlterField( + model_name='basefind', + name='discovery_date_taq', + field=models.DateField(blank=True, null=True, verbose_name='Discovery date (end)'), + ), + migrations.AlterField( + model_name='basefind', + name='geodata', + field=models.ManyToManyField(blank=True, related_name='related_items_archaeological_finds_basefind', to='ishtar_common.GeoVectorData', verbose_name='Geodata'), + ), + migrations.AlterField( + model_name='basefind', + name='main_geodata', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='main_related_items_archaeological_finds_basefind', to='ishtar_common.GeoVectorData', verbose_name='Main geodata'), + ), + migrations.AlterField( + model_name='find', + name='find_number', + field=models.IntegerField(blank=True, null=True, verbose_name='Number of remains'), + ), + migrations.AlterField( + model_name='find', + name='museum_id', + field=models.TextField(blank=True, default='', verbose_name='Museum inventory number'), + ), + migrations.AlterField( + model_name='historicalbasefind', + name='discovery_date', + field=models.DateField(blank=True, null=True, verbose_name='Discovery date (exact or beginning)'), + ), + migrations.AlterField( + model_name='historicalbasefind', + name='discovery_date_taq', + field=models.DateField(blank=True, null=True, verbose_name='Discovery date (end)'), + ), + migrations.AlterField( + model_name='historicalbasefind', + name='main_geodata', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.GeoVectorData', verbose_name='Main geodata'), + ), + migrations.AlterField( + model_name='historicalfind', + name='find_number', + field=models.IntegerField(blank=True, null=True, verbose_name='Number of remains'), + ), + migrations.AlterField( + model_name='historicalfind', + name='museum_id', + field=models.TextField(blank=True, default='', verbose_name='Museum inventory number'), + ), + migrations.AlterField( + model_name='historicaltreatment', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AlterField( + model_name='historicaltreatmentfile', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AlterField( + model_name='treatment', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AlterField( + model_name='treatmentfile', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + ] diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index 8da525281..a8565f14e 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -726,6 +726,28 @@ class BaseFind( context_record_id=None, transaction_id=None, ): + profile = get_current_profile() + if profile.basefind_cached_label: + # no bulk possible + q = cls.objects + if context_record_id: + q = q.filter(context_record_id=context_record_id) + elif operation_id: + q = q.filter(context_record__operation_id=operation_id) + elif parcel_id: + q = q.filter(context_record__parcel_id=parcel_id) + else: + return + for bf in q.all(): + bf.skip_history_when_saving = True + bf._no_move = True + bf.save() + Find.cached_label_bulk_update( + context_record_id=context_record_id, + operation_id=operation_id, parcel_id=parcel_id, + transaction_id=transaction_id + ) + return transaction_id, is_recursion = cls.bulk_recursion( transaction_id, [operation_id, parcel_id, context_record_id] ) @@ -2001,13 +2023,6 @@ class Find( blank=True, help_text=_("Related treatments when no new find is created"), ) - cached_label = models.TextField( - _("Cached name"), - blank=True, - default="", - db_index=True, - help_text=_("Generated automatically - do not edit"), - ) cached_periods = models.TextField( _("Cached periods label"), blank=True, @@ -2676,6 +2691,9 @@ class Find( return cls._return_get_owns(owns, values, get_short_menu_class) def _generate_cached_label(self): + label = self._profile_generate_cached_label() + if label: + return label self.cached_label_bulk_update(find_id=self.pk) return Find.objects.filter(pk=self.pk).values("cached_label")[0]["cached_label"] @@ -2697,6 +2715,23 @@ class Find( find_id=None, transaction_id=None, ): + profile = get_current_profile() + if profile.find_cached_label: + # no bulk possible + q = cls.objects + if context_record_id: + q = q.filter(base_finds__context_record_id=context_record_id) + elif operation_id: + q = q.filter(base_finds__context_record__operation_id=operation_id) + elif parcel_id: + q = q.filter(base_finds__context_record__parcel_id=parcel_id) + else: + return + for f in q.all(): + f.skip_history_when_saving = True + f._no_move = True + f.save() + return transaction_id, is_recursion = cls.bulk_recursion( transaction_id, [operation_id, parcel_id, context_record_id, find_id] ) diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py index d52516ff2..509d67f70 100644 --- a/archaeological_finds/models_treatments.py +++ b/archaeological_finds/models_treatments.py @@ -51,7 +51,7 @@ from ishtar_common.models import ( SearchVectorConfig, DocumentItem, ) -from ishtar_common.models_common import HistoricalRecords +from ishtar_common.models_common import CompleteIdentifierItem, HistoricalRecords from ishtar_common.utils import ( cached_label_changed, get_current_year, @@ -89,6 +89,7 @@ class Treatment( ValueGetter, DocumentItem, BaseHistorizedItem, + CompleteIdentifierItem, ImageModel, OwnPerms, ShortMenuItem, @@ -279,9 +280,6 @@ class Treatment( blank=True, null=True, ) - cached_label = models.TextField( - _("Cached name"), blank=True, default="", db_index=True - ) history = HistoricalRecords(bases=[HistoryModel]) class Meta: @@ -349,6 +347,9 @@ class Treatment( ) def _generate_cached_label(self): + label = self._profile_generate_cached_label() + if label: + return label items = [ str(getattr(self, k)) for k in ["year", "index", "other_reference", "label"] @@ -1008,6 +1009,7 @@ class TreatmentFile( ClosedItem, DocumentItem, BaseHistorizedItem, + CompleteIdentifierItem, OwnPerms, ValueGetter, ShortMenuItem, @@ -1165,9 +1167,6 @@ class TreatmentFile( on_delete=models.SET_NULL, related_name="treatment_files", ) - cached_label = models.TextField( - _("Cached name"), blank=True, default="", db_index=True - ) history = HistoricalRecords() class Meta: @@ -1258,6 +1257,9 @@ class TreatmentFile( return cls._return_get_owns(owns, values, get_short_menu_class) def _generate_cached_label(self): + label = self._profile_generate_cached_label() + if label: + return label items = [ str(getattr(self, k)) for k in ["year", "index", "internal_reference", "name"] diff --git a/archaeological_operations/migrations/0111_auto_20230127_1503.py b/archaeological_operations/migrations/0111_auto_20230127_1503.py new file mode 100644 index 000000000..075422d9f --- /dev/null +++ b/archaeological_operations/migrations/0111_auto_20230127_1503.py @@ -0,0 +1,54 @@ +# Generated by Django 2.2.24 on 2023-01-27 15:03 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_operations', '0110_auto_20220917_1818'), + ] + + operations = [ + migrations.AlterField( + model_name='archaeologicalsite', + name='geodata', + field=models.ManyToManyField(blank=True, related_name='related_items_archaeological_operations_archaeologicalsite', to='ishtar_common.GeoVectorData', verbose_name='Geodata'), + ), + migrations.AlterField( + model_name='archaeologicalsite', + name='main_geodata', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='main_related_items_archaeological_operations_archaeologicalsite', to='ishtar_common.GeoVectorData', verbose_name='Main geodata'), + ), + migrations.AlterField( + model_name='historicalarchaeologicalsite', + name='main_geodata', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.GeoVectorData', verbose_name='Main geodata'), + ), + migrations.AlterField( + model_name='historicaloperation', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AlterField( + model_name='historicaloperation', + name='main_geodata', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.GeoVectorData', verbose_name='Main geodata'), + ), + migrations.AlterField( + model_name='operation', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AlterField( + model_name='operation', + name='geodata', + field=models.ManyToManyField(blank=True, related_name='related_items_archaeological_operations_operation', to='ishtar_common.GeoVectorData', verbose_name='Geodata'), + ), + migrations.AlterField( + model_name='operation', + name='main_geodata', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='main_related_items_archaeological_operations_operation', to='ishtar_common.GeoVectorData', verbose_name='Main geodata'), + ), + ] diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 191918237..d5110df9c 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -36,7 +36,7 @@ from django.db.models import Q, Count, Sum, Max, Avg from django.db.models.signals import post_save, m2m_changed, post_delete from django.forms import ValidationError from django.urls import reverse -from ishtar_common.utils import ugettext_lazy as _, pgettext_lazy +from ishtar_common.utils import ugettext_lazy as _, pgettext_lazy, get_generated_id from ishtar_common.models import ( Area, @@ -532,13 +532,6 @@ class ArchaeologicalSite( blank=True, null=True, ) - cached_label = models.TextField( - _("Cached name"), - blank=True, - default="", - db_index=True, - help_text=_("Generated automatically - do not edit"), - ) cached_towns_label = models.TextField( _("Cached town label"), blank=True, @@ -574,7 +567,7 @@ class ArchaeologicalSite( ] def __str__(self): - return self.cached_label or self._generate_cached_label() or "" + return self.cached_label or "" @property def short_class_name(self): @@ -707,6 +700,9 @@ class ArchaeologicalSite( return cls._return_get_owns(owns, values, get_short_menu_class) def _generate_cached_label(self): + label = self._profile_generate_cached_label() + if label: + return label name = self.reference if self.name: name += " %s %s" % (settings.JOINT, self.name) @@ -1424,14 +1420,6 @@ class Operation( blank=True, null=True, ) - cached_label = models.CharField( - _("Cached name"), - max_length=500, - help_text=_("Generated automatically - do not edit"), - null=True, - blank=True, - db_index=True, - ) archaeological_sites = models.ManyToManyField( ArchaeologicalSite, verbose_name=_("Archaeological sites"), @@ -1724,6 +1712,9 @@ class Operation( # return None def _generate_cached_label(self): + label = self._profile_generate_cached_label() + if label: + return label items = [self.get_town_label(), self.get_reference(full=True)] if self.common_name: items.append(self.common_name) @@ -3090,6 +3081,9 @@ class Parcel(LightHistorizedItem): """ def _generate_cached_label(self): + label = get_generated_id("parcel_cached_label", self) + if label: + return label if self.public_domain: return "DP" return "{}{}".format(self.section or "", self.parcel_number or "") diff --git a/archaeological_warehouse/migrations/0116_auto_20230127_1504.py b/archaeological_warehouse/migrations/0116_auto_20230127_1504.py new file mode 100644 index 000000000..1134ef921 --- /dev/null +++ b/archaeological_warehouse/migrations/0116_auto_20230127_1504.py @@ -0,0 +1,28 @@ +# Generated by Django 2.2.24 on 2023-01-27 15:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_warehouse', '0115_auto_20230120_1133'), + ] + + operations = [ + migrations.AddField( + model_name='historicalwarehouse', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AddField( + model_name='warehouse', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AlterField( + model_name='container', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + ] diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 4fcb1b1d7..224fe8b21 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -1123,9 +1123,6 @@ class Container( ) reference = models.TextField(_("Container ref.")) comment = models.TextField(_("Comment"), blank=True, default="") - cached_label = models.TextField( - _("Localisation"), blank=True, default="", db_index=True - ) cached_location = models.TextField( _("Cached location"), blank=True, default="", db_index=True ) @@ -1309,6 +1306,9 @@ class Container( context.pop(k) def _generate_cached_label(self): + label = self._profile_generate_cached_label() + if label: + return label return self.precise_location def _generate_cached_location(self): diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py index 780a65496..714b4c445 100644 --- a/ishtar_common/admin.py +++ b/ishtar_common/admin.py @@ -575,33 +575,42 @@ class IshtarSiteProfileAdmin(admin.ModelAdmin): "operation_region_code", "operation_complete_identifier", "operation_custom_index", + "operation_cached_label", "site_complete_identifier", "site_custom_index", + "site_cached_label", "file_external_id", "file_complete_identifier", "file_custom_index", + "file_cached_label", "parcel_external_id", + "parcel_cached_label", "context_record_external_id", "contextrecord_complete_identifier", "contextrecord_custom_index", + "contextrecord_cached_label", "base_find_external_id", "basefind_complete_identifier", "basefind_custom_index", + "basefind_cached_label", "find_external_id", "find_complete_identifier", "find_use_index", "find_index", "find_custom_index", + "find_cached_label", "container_external_id", "container_complete_identifier", "container_custom_index", + "container_cached_label", "warehouse_external_id", "warehouse_complete_identifier", "warehouse_custom_index", + "warehouse_cached_label", "document_external_id", "document_complete_identifier", "document_custom_index", - + "document_cached_label", ), }), ) diff --git a/ishtar_common/migrations/0224_auto_20230127_1503.py b/ishtar_common/migrations/0224_auto_20230127_1503.py new file mode 100644 index 000000000..736e2a88d --- /dev/null +++ b/ishtar_common/migrations/0224_auto_20230127_1503.py @@ -0,0 +1,68 @@ +# Generated by Django 2.2.24 on 2023-01-27 15:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0223_auto_20230120_1124'), + ] + + operations = [ + migrations.AddField( + model_name='document', + name='cached_label', + field=models.TextField(blank=True, db_index=True, default='', help_text='Generated automatically - do not edit', verbose_name='Cached name'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='basefind_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='Base find cached label'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='container_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='Container cached label'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='contextrecord_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='Context record cached label'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='document_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='Document cached label'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='file_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='File cached label'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='find_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='Find cached label'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='operation_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='Operation cached label'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='parcel_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='Parcel cached label'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='site_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='Site cached label'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='warehouse_cached_label', + field=models.TextField(blank=True, default='', help_text='Formula to manage cached label. If not set a default is provided', verbose_name='Warehouse cached label'), + ), + ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index b4a563d7f..8dd1fc3fe 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1196,11 +1196,17 @@ class IshtarSiteProfile(models.Model, Cached): "Separate keys with a semicolon." ), ) + operation_cached_label = models.TextField( + _("Operation cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) site_complete_identifier = models.TextField( _("Archaeological site complete identifier"), default="", blank=True, - help_text=_("Formula to manage archaeological site complete" " identifier."), + help_text=_("Formula to manage archaeological site complete identifier."), ) site_custom_index = models.TextField( _("Archaeological site custom index key"), @@ -1211,6 +1217,12 @@ class IshtarSiteProfile(models.Model, Cached): "index. Separate keys with a semicolon." ), ) + site_cached_label = models.TextField( + _("Site cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) file_external_id = models.TextField( _("File external id"), default="{year}-{numeric_reference}", @@ -1236,6 +1248,12 @@ class IshtarSiteProfile(models.Model, Cached): "index. Separate keys with a semicolon." ), ) + file_cached_label = models.TextField( + _("File cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) parcel_external_id = models.TextField( _("Parcel external id"), default="{associated_file__external_id}{operation__code_patriarche}-" @@ -1247,6 +1265,12 @@ class IshtarSiteProfile(models.Model, Cached): "data can be destructive." ), ) + parcel_cached_label = models.TextField( + _("Parcel cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) context_record_external_id = models.TextField( _("Context record external id"), default="{parcel__external_id}-{label}", @@ -1272,6 +1296,12 @@ class IshtarSiteProfile(models.Model, Cached): "Separate keys with a semicolon." ), ) + contextrecord_cached_label = models.TextField( + _("Context record cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) base_find_external_id = models.TextField( _("Base find external id"), default="{context_record__external_id}-{label}", @@ -1297,6 +1327,12 @@ class IshtarSiteProfile(models.Model, Cached): "Separate keys with a semicolon." ), ) + basefind_cached_label = models.TextField( + _("Base find cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) find_external_id = models.TextField( _("Find external id"), default="{get_first_base_find__context_record__external_id}-{label}", @@ -1322,6 +1358,12 @@ class IshtarSiteProfile(models.Model, Cached): "Separate keys with a semicolon." ), ) + find_cached_label = models.TextField( + _("Find cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) container_external_id = models.TextField( _("Container external id"), default="{parent_external_id}-{container_type__txt_idx}-" "{reference}", @@ -1347,6 +1389,12 @@ class IshtarSiteProfile(models.Model, Cached): "Separate keys with a semicolon." ), ) + container_cached_label = models.TextField( + _("Container cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) warehouse_external_id = models.TextField( _("Warehouse external id"), default="{slug}", @@ -1372,6 +1420,12 @@ class IshtarSiteProfile(models.Model, Cached): "Separate keys with a semicolon." ), ) + warehouse_cached_label = models.TextField( + _("Warehouse cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) document_external_id = models.TextField( _("Document external id"), default="{index}", @@ -1397,6 +1451,12 @@ class IshtarSiteProfile(models.Model, Cached): "Separate keys with a semicolon." ), ) + document_cached_label = models.TextField( + _("Document cached label"), + default="", + blank=True, + help_text=_("Formula to manage cached label. If not set a default is provided"), + ) person_raw_name = models.TextField( _("Raw name for person"), default="{name|upper} {surname}", diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index b239813f3..f50395dee 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -3982,6 +3982,13 @@ class ImageContainerModel: class CompleteIdentifierItem(models.Model, ImageContainerModel): HAS_QR_CODE = True + cached_label = models.TextField( + _("Cached name"), + blank=True, + default="", + db_index=True, + help_text=_("Generated automatically - do not edit"), + ) complete_identifier = models.TextField( _("Complete identifier"), blank=True, default="" ) @@ -4001,6 +4008,19 @@ class CompleteIdentifierItem(models.Model, ImageContainerModel): return "" return self.qrcode.path + def _profile_generate_cached_label(self): + slug = getattr(self, "SLUG", None) + if not slug: + return + return get_generated_id(slug + "_cached_label", self) + + def _generate_cached_label(self): + label = self._profile_generate_cached_label() + if not label: + # to be eventually overloaded by parent class + return str(self) + return label + def generate_qrcode(self, request=None, secure=True, tmpdir=None): url = self.get_absolute_url() site = Site.objects.get_current() diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 2ce8a5f9b..7c7e6a6a5 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -555,9 +555,11 @@ def _cached_label_changed(sender, **kwargs): instance.refresh_cache() instance._cached_label_checked = True - cached_labels = ["cached_label"] + cached_labels = [] if hasattr(instance, "CACHED_LABELS"): cached_labels = instance.CACHED_LABELS + if hasattr(instance, "cached_label") and "cached_label" not in cached_labels: + cached_labels.append("cached_label") changed = [] for cached_label in cached_labels: gen_func = "_generate_" + cached_label @@ -1929,7 +1931,7 @@ def _update_gen_id_dct(item, dct, initial_key, fkey=None, filters=None): if not fkey: fkey = initial_key[:] if fkey.startswith("settings__"): - dct[fkey] = getattr(settings, fkey[len("settings__") :]) or "" + dct[fkey] = getattr(settings, fkey[len("settings__"):]) or "" return obj = item for k in fkey.split("__"): @@ -1965,11 +1967,13 @@ def get_generated_id(key, item): if not hasattr(profile, key): return formula = getattr(profile, key) + if not formula: + return "" dct = {} # jinja2 style if "{{" in formula or "{%" in formula: - # naive parse - only simple jija2 is managed + # naive parse - only simple jinja2 is managed key_list = [] for key in PARSE_JINJA.findall(formula): key = key.strip().split("|")[0] @@ -1986,7 +1990,7 @@ def get_generated_id(key, item): new_keys = [] for key in key_list: if key.startswith("not "): - key = key[len("not ") :].strip() + key = key[len("not "):].strip() key = key.split(".")[0] if " % " in key: keys = key.split(" % ")[1] |