summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archaeological_context_records/migrations/0101_squashed.py3
-rw-r--r--archaeological_context_records/models.py13
-rw-r--r--archaeological_finds/migrations/0101_squashed.py6
-rw-r--r--archaeological_finds/models_finds.py8
-rw-r--r--archaeological_finds/tests.py2
-rw-r--r--archaeological_operations/models.py14
-rw-r--r--ishtar_common/models.py23
-rw-r--r--ishtar_common/models_common.py54
-rw-r--r--ishtar_common/utils.py49
9 files changed, 88 insertions, 84 deletions
diff --git a/archaeological_context_records/migrations/0101_squashed.py b/archaeological_context_records/migrations/0101_squashed.py
index fcdf81789..119e28b61 100644
--- a/archaeological_context_records/migrations/0101_squashed.py
+++ b/archaeological_context_records/migrations/0101_squashed.py
@@ -111,8 +111,7 @@ class Migration(migrations.Migration):
'ordering': ('cached_label',),
'permissions': (('view_own_contextrecord', 'Can view own Context Record'), ('add_own_contextrecord', 'Can add own Context Record'), ('change_own_contextrecord', 'Can change own Context Record'), ('delete_own_contextrecord', 'Can delete own Context Record')),
},
- bases=(ishtar_common.models.BulkUpdatedItem,
- ishtar_common.models.DocumentItem,
+ bases=(ishtar_common.models.DocumentItem,
ishtar_common.models.StatisticItem,
ishtar_common.models.TemplateItem,
ishtar_common.models.OwnPerms,
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py
index e13633d90..0ac13e43a 100644
--- a/archaeological_context_records/models.py
+++ b/archaeological_context_records/models.py
@@ -52,7 +52,6 @@ from ishtar_common.models import (
post_delete_record_relation,
post_save_cache,
ValueGetter,
- BulkUpdatedItem,
RelationItem,
Town,
get_current_profile,
@@ -461,7 +460,6 @@ class GeographicSubTownItem(GeoItem):
class ContextRecord(
- BulkUpdatedItem,
DocumentItem,
BaseHistorizedItem,
CompleteIdentifierItem,
@@ -1286,8 +1284,15 @@ def context_record_post_save(sender, **kwargs):
cached_label_changed(sender=sender, **kwargs)
post_save_geo(sender=sender, **kwargs)
instance = kwargs.get("instance", None)
- if instance and instance.pk:
- ContextRecordTree.update(instance.pk)
+ if not instance or not instance.pk:
+ return
+ ContextRecordTree.update(instance.pk)
+ BaseFind = apps.get_model("archaeological_finds", "BaseFind")
+ Find = apps.get_model("archaeological_finds", "Find")
+ for bf in instance.base_finds.all():
+ cached_label_changed(BaseFind, instance=bf)
+ for f in bf.find.all():
+ cached_label_changed(Find, instance=f)
post_save.connect(context_record_post_save, sender=ContextRecord)
diff --git a/archaeological_finds/migrations/0101_squashed.py b/archaeological_finds/migrations/0101_squashed.py
index 7a7daab0c..4cae8d0d5 100644
--- a/archaeological_finds/migrations/0101_squashed.py
+++ b/archaeological_finds/migrations/0101_squashed.py
@@ -101,8 +101,7 @@ class Migration(migrations.Migration):
'verbose_name_plural': 'Base finds',
'permissions': (('view_own_basefind', 'Can view own Base find'), ('add_own_basefind', 'Can add own Base find'), ('change_own_basefind', 'Can change own Base find'), ('delete_own_basefind', 'Can delete own Base find')),
},
- bases=(ishtar_common.models.BulkUpdatedItem,
- ishtar_common.models.StatisticItem, ishtar_common.models.TemplateItem, models.Model, ishtar_common.models.CachedGen, ishtar_common.models_common.FixAssociated, ishtar_common.models.OwnPerms, ishtar_common.models.ValueGetter),
+ bases=(ishtar_common.models.StatisticItem, ishtar_common.models.TemplateItem, models.Model, ishtar_common.models.CachedGen, ishtar_common.models_common.FixAssociated, ishtar_common.models.OwnPerms, ishtar_common.models.ValueGetter),
),
migrations.CreateModel(
name='BatchType',
@@ -234,8 +233,7 @@ class Migration(migrations.Migration):
'ordering': ('cached_label',),
'permissions': (('view_own_find', 'Can view own Find'), ('add_own_find', 'Can add own Find'), ('change_own_find', 'Can change own Find'), ('delete_own_find', 'Can delete own Find')),
},
- bases=(ishtar_common.models.BulkUpdatedItem,
- ishtar_common.models.ValueGetter, ishtar_common.models.DocumentItem, ishtar_common.models.StatisticItem, ishtar_common.models.TemplateItem, models.Model, ishtar_common.models.CachedGen, ishtar_common.models_common.FixAssociated, ishtar_common.models.ImageContainerModel, ishtar_common.models.OwnPerms, ishtar_common.models.MainItem),
+ bases=(ishtar_common.models.ValueGetter, ishtar_common.models.DocumentItem, ishtar_common.models.StatisticItem, ishtar_common.models.TemplateItem, models.Model, ishtar_common.models.CachedGen, ishtar_common.models_common.FixAssociated, ishtar_common.models.ImageContainerModel, ishtar_common.models.OwnPerms, ishtar_common.models.MainItem),
),
migrations.CreateModel(
name='FindBasket',
diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py
index 53a1e3eef..7da45166e 100644
--- a/archaeological_finds/models_finds.py
+++ b/archaeological_finds/models_finds.py
@@ -47,7 +47,6 @@ from ishtar_common.models import (
BaseHistorizedItem,
Basket,
BiographicalNote,
- BulkUpdatedItem,
CompleteIdentifierItem,
Document,
DocumentItem,
@@ -531,7 +530,6 @@ class BFBulkView(object):
class BaseFind(
- BulkUpdatedItem,
BaseHistorizedItem,
GeographicSubTownItem,
CompleteIdentifierItem,
@@ -879,6 +877,11 @@ class BaseFind(
def post_save_basefind(sender, **kwargs):
cached_label_changed(sender, **kwargs)
post_save_geo(sender, **kwargs)
+ instance = kwargs.get("instance", None)
+ if not instance or not instance.pk:
+ return
+ for f in instance.find.all():
+ cached_label_changed(Find, instance=f)
def pre_delete_basefind(sender, **kwargs):
@@ -1078,7 +1081,6 @@ def query_loan(is_true=True):
class Find(
- BulkUpdatedItem,
ValueGetter,
DocumentItem,
BaseHistorizedItem,
diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py
index 5a7e53849..e7b520fb6 100644
--- a/archaeological_finds/tests.py
+++ b/archaeological_finds/tests.py
@@ -1148,7 +1148,7 @@ class FindTest(FindInit, TestCase):
cr.operation.save()
base_find = models.BaseFind.objects.get(pk=base_find.pk)
find = models.Find.objects.get(pk=find.pk)
- cr = ContextRecord.objects.get(pk=cr.pk)
+ ContextRecord.objects.get(pk=cr.pk)
self.assertIn("PAT", find.external_id)
self.assertIn("PAT", base_find.external_id)
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index e847429a0..4220e6cc2 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -2434,12 +2434,19 @@ def operation_post_save(sender, **kwargs):
for parcel in operation.parcels.all():
parcel.copy_to_file()
- # external id update
+ # external id, cached_labels update
for parcel in operation.parcels.all():
- parcel.update_external_id(save=True)
+ cached_label_changed(Parcel, instance=parcel)
+ ContextRecord = apps.get_model("archaeological_context_records", "ContextRecord")
+ BaseFind = apps.get_model("archaeological_finds", "BaseFind")
+ Find = apps.get_model("archaeological_finds", "Find")
for cr in operation.context_record.all():
- cr.update_external_id(save=True)
+ cached_label_changed(ContextRecord, instance=cr)
+ for bf in cr.base_finds.all():
+ cached_label_changed(BaseFind, instance=bf)
+ for f in bf.find.all():
+ cached_label_changed(Find, instance=f)
post_save.connect(operation_post_save, sender=Operation)
@@ -3127,6 +3134,7 @@ def strip_zero(value):
class Parcel(LightHistorizedItem):
+ SLUG = "parcel"
EXTERNAL_ID_KEY = "parcel_external_id"
BASE_SEARCH_VECTORS = [
SearchVectorConfig("section"),
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 394bd5382..48ed55e77 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -577,29 +577,6 @@ class ImageModel(models.Model, ImageContainerModel):
return "{}-thumb.{}".format(".".join(splited[:-1]), splited[-1])
-class BulkUpdatedItem(object):
- @classmethod
- def bulk_recursion(cls, transaction_id, extra_args):
- """
- Prevent infinite recursion. Should not happen but wrong manipulation
- in the database or messy imports can generate circular relations
-
- :param transaction_id: current transaction ID (unix time) - if null
- a transaction ID is generated
- :param extra_args: arguments dealing with
- :return: (transaction ID, is a recursion)
- """
- # TODO: no more bulk update - should be removed
- if not transaction_id:
- transaction_id = str(time.time())
- args = ["cached_label_bulk_update", transaction_id] + extra_args
- key, val = get_cache(cls, args)
- if val:
- return transaction_id, True
- cache.set(key, 1, settings.CACHE_SMALLTIMEOUT)
- return transaction_id, False
-
-
class RelationItem(models.Model):
"""
Items with relation between them
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py
index 0b34400a1..98dbb047b 100644
--- a/ishtar_common/models_common.py
+++ b/ishtar_common/models_common.py
@@ -1615,20 +1615,39 @@ class BaseHistorizedItem(
def duplicate(self, user=None, data=None):
return duplicate_item(self, user, data)
- def update_external_id(self, save=False):
- if not self.EXTERNAL_ID_KEY or (
- self.external_id and not getattr(self, "auto_external_id", False)
- ):
+ def update_external_id(self, save=False, no_set=False):
+ if not hasattr(self, "external_id"):
+ return
+ if self.external_id and not getattr(self, "auto_external_id", False):
return
- external_id = get_generated_id(self.EXTERNAL_ID_KEY, self)
+ external_id_key = self.EXTERNAL_ID_KEY or (
+ hasattr(self, "SLUG") and (self.SLUG + "_external_id")
+ )
+ if not external_id_key:
+ return
+ external_id = get_generated_id(external_id_key, self)
if external_id == self.external_id:
return
- self.auto_external_id = True
- self.external_id = external_id
+ if no_set:
+ return external_id
+ try:
+ self.auto_external_id = True
+ except AttributeError:
+ pass
+ try:
+ self.external_id = external_id
+ except AttributeError:
+ return
self._cached_label_checked = False
if save:
- self.skip_history_when_saving = True
- self.save()
+ if self.pk:
+ self.__class__.objects.filter(pk=self.pk).update(
+ auto_external_id=True,
+ external_id=external_id
+ )
+ else:
+ self.skip_history_when_saving = True
+ self.save()
return external_id
def get_last_history_date(self):
@@ -4524,22 +4543,25 @@ class CompleteIdentifierItem(models.Model, ImageContainerModel):
def regenerate_all_ids(self, save=True):
if getattr(self, "_prevent_loop", False):
- return
- modified = False
+ return {}
+ updated = {}
custom_index = self.generate_custom_index()
if custom_index != self.custom_index:
- modified = True
self.custom_index = custom_index
+ updated["custom_index"] = custom_index
complete_id = self.generate_complete_identifier()
if complete_id and complete_id != self.complete_identifier:
- modified = True
self.complete_identifier = complete_id
- if modified:
+ updated["complete_identifier"] = complete_id
+ if updated:
self._prevent_loop = True
self.skip_history_when_saving = True
if save:
- self.save()
- return modified
+ if self.pk:
+ self.__class__.objects.filter(pk=self.pk).update(**updated)
+ else:
+ self.save()
+ return updated
class SearchVectorConfig:
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index 40fb4db04..3249bb20e 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -871,14 +871,8 @@ def cached_label_changed(sender, **kwargs):
queue = getattr(instance, "_queue", settings.CELERY_DEFAULT_QUEUE)
if hasattr(instance, "external_id") and hasattr(instance, "auto_external_id") \
and hasattr(instance, "SLUG") and not getattr(instance, "_external_id_checked", None):
- changed = load_task(_external_id_changed, "external_id_changed", None, sender,
- queue=queue, **kwargs)
- if changed and (
- not settings.USE_BACKGROUND_TASK
- or not instance.pk
- or not sender.objects.filter(pk=instance.pk).count()
- ): # cached_label_changed re-triggered
- return
+ load_task(_external_id_changed, "external_id_changed", None, sender,
+ queue=queue, **kwargs)
force_update = kwargs.get("force_update", False)
if getattr(instance, "need_update", False):
@@ -943,10 +937,6 @@ def _cached_label_changed(sender, **kwargs):
updated = False
if force_update or hasattr(instance, "update_search_vector"):
updated = instance.update_search_vector()
- if hasattr(instance, "_cached_labels_bulk_update"):
- # updated = instance._cached_labels_bulk_update() or updated
- # disable now
- pass
if not updated and hasattr(instance, "_get_associated_cached_labels"):
for item in instance._get_associated_cached_labels():
item._cascade_change = True
@@ -955,7 +945,6 @@ def _cached_label_changed(sender, **kwargs):
if instance.timestamp_label:
item._timestamp = instance.timestamp_label
cached_label_changed(item.__class__, instance=item)
-
cache_key, __ = get_cache(sender, ["cached_label_changed", instance.pk])
cache.set(cache_key, None, settings.CACHE_TASK_TIMEOUT)
if cached_labels:
@@ -999,26 +988,30 @@ def _external_id_changed(sender, **kwargs):
if getattr(instance, "_external_id_checked", None):
return
instance._queue = kwargs.get("queue", settings.CELERY_DEFAULT_QUEUE)
- updated = False
+ updated = {}
if not instance.external_id or instance.auto_external_id:
- external_id = get_generated_id(instance.SLUG + "_external_id", instance)
- if external_id != instance.external_id:
- try:
- instance.auto_external_id = True
- instance.external_id = external_id
- updated = True
- except AttributeError:
- pass
+ if hasattr(instance, "update_external_id"):
+ external_id = instance.update_external_id(save=False, no_set=True)
+ else:
+ external_id = get_generated_id(instance.SLUG + "_external_id", instance)
+
+ if external_id and external_id != instance.external_id:
+ updated = {"auto_external_id": True, "external_id": external_id}
+ instance.auto_external_id = True
+ instance.external_id = external_id
if hasattr(instance, "regenerate_all_ids"):
- updated |= instance.regenerate_all_ids(save=False) or False
+ updated.update(instance.regenerate_all_ids(save=False))
instance._external_id_checked = True
if updated:
- if settings.USE_BACKGROUND_TASK and hasattr(instance, "no_post_process"):
- instance.no_post_process()
+ if instance.pk:
+ instance.__class__.objects.filter(pk=instance.pk).update(**updated)
else:
- instance._no_move = True
- instance.skip_history_when_saving = True
- instance.save()
+ if settings.USE_BACKGROUND_TASK and hasattr(instance, "no_post_process"):
+ instance.no_post_process()
+ else:
+ instance._no_move = True
+ instance.skip_history_when_saving = True
+ instance.save()
return True