diff options
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r-- | ishtar_common/models_common.py | 58 |
1 files changed, 47 insertions, 11 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index e2495bcd5..d09d64d88 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -66,6 +66,7 @@ from ishtar_common.model_merging import merge_model_objects from ishtar_common.models_imports import Import from ishtar_common.templatetags.link_to_window import simple_link_to_window from ishtar_common.utils import ( + bulk_item_changed, cached_label_changed, disable_for_loaddata, duplicate_item, @@ -1906,6 +1907,8 @@ class BaseHistorizedItem( ) if not created and not external_id_updated: self.update_external_id() + if not hasattr(self, "_timestamp"): + self._timestamp = int(datetime.datetime.now().timestamp()) super(BaseHistorizedItem, self).save(*args, **kwargs) if created and self.update_external_id(): # force resave for external ID creation @@ -3131,11 +3134,11 @@ class GeographicItem(models.Model): if getattr(self, "_geodata_list", None): return self._geodata_list lst = [] + q = self.geodata if self.main_geodata: lst.append(self.main_geodata) - for geo in self.geodata.all(): - if geo != self.main_geodata: - lst.append(geo) + q = q.exclude(id=self.main_geodata_id) + lst += [geo for geo in self.geodata.all()] self._geodata_list = lst return lst @@ -3363,6 +3366,7 @@ class MainItem(ShortMenuItem, SerializeItem, SheetItem): SLUG = "" SHOW_URL = None DOWN_MODEL_UPDATE = [] + DOWN_MODEL_REVERSE_QUERY = {} INITIAL_VALUES = [] # list of field checkable if changed on save OLD_SHEET_EXPORT = True @@ -3385,11 +3389,15 @@ class MainItem(ShortMenuItem, SerializeItem, SheetItem): self._initial_values[field_name] = value return changed - def cascade_update(self, changed=True): + def cascade_update(self, changed=True, timestamp=None): if not changed: return if getattr(self, "_no_down_model_update", False): return + + if not timestamp: + timestamp = int(datetime.datetime.now().timestamp()) + queue = getattr(self, "_queue", settings.CELERY_DEFAULT_QUEUE) for down_model in self.DOWN_MODEL_UPDATE: if not settings.USE_BACKGROUND_TASK: @@ -3397,14 +3405,27 @@ class MainItem(ShortMenuItem, SerializeItem, SheetItem): if hasattr(rel.model, "need_update"): rel.update(need_update=True) continue + if down_model in self.DOWN_MODEL_REVERSE_QUERY: + key = self.DOWN_MODEL_REVERSE_QUERY[down_model] + new_down_model = getattr(self, down_model).model + if hasattr(new_down_model, "bulk_item_changed"): + new_down_model.bulk_item_changed({key: self.pk}) + continue for item in getattr(self, down_model).all(): if hasattr(self, "_timestamp"): item._timestamp = self._timestamp item._queue = queue if hasattr(item, "cached_label_changed"): - item.cached_label_changed() + if hasattr(item, "timestamp_label") and \ + (item.timestamp_label or 0) >= timestamp: + pass + else: + item.cached_label_changed() + if hasattr(item, "timestamp_geo") and \ + (item.timestamp_geo or 0) >= timestamp: + continue if hasattr(item, "main_geodata"): - item.post_save_geo() + item.post_save_geo(timestamp=timestamp) def no_post_process(self): self.skip_history_when_saving = True @@ -3490,16 +3511,30 @@ class MainItem(ShortMenuItem, SerializeItem, SheetItem): self._no_move = True self.save() - def cached_label_changed(self): + @classmethod + def bulk_item_changed(cls, query_dict, timestamp=None): + """ + Trigger changes in a bulk item list set by a query_dict (must be serializabled) + """ + if not settings.USE_BACKGROUND_TASK: + for item in cls.objects.filter(**query_dict).all(): + item.cached_label_changed(timestamp=timestamp) + return + bulk_item_changed(cls, query=query_dict, timestamp=timestamp) + + def cached_label_changed(self, timestamp=None): self.no_post_process() self._cached_label_checked = False - cached_label_changed(self.__class__, instance=self, created=False) + if not timestamp: + timestamp = int(datetime.datetime.now().timestamp()) + cached_label_changed(self.__class__, instance=self, created=False, + timestamp=timestamp) - def post_save_geo(self, save=True): + def post_save_geo(self, save=True, timestamp=None): if getattr(self, "_post_saved_geo", False): return self.no_post_process() - post_save_geo(self.__class__, instance=self, created=False) + post_save_geo(self.__class__, instance=self, created=False, timestamp=timestamp) return False def external_id_changed(self): @@ -3784,6 +3819,8 @@ class Town(GeographicItem, Imported, DocumentItem, MainItem, models.Model): def post_save_town(sender, **kwargs): + timestamp = int(datetime.datetime.now().timestamp()) + kwargs["timestamp"] = timestamp cached_label_changed(sender, **kwargs) town = kwargs["instance"] town.generate_geo() @@ -3812,7 +3849,6 @@ def geotown_attached_changed(sender, **kwargs): return instance = kwargs.get("instance", None) model = kwargs.get("model", None) - pk_set = kwargs.get("pk_set", None) action = kwargs.get("action", None) if not instance or not model or not hasattr(instance, "post_save_geo"): return |