else: if origin.term != instance.term: origin_boundary_term_id = origin.term.id boundary_term_id = instance.term.id entities_ids = EntityModel.objects.instance_of( *_model_with_place_mixin).filter( terms__id=origin_boundary_term_id).values_list( 'id', flat=True) EntityModel.terms.through.objects.filter( entity_id__in=list(entities_ids), term_id=origin_boundary_term_id).update( term_id=boundary_term_id) clear_boundary_polygons_cache(instance) #============================================================================== # Connect #============================================================================== clazz = BoundaryModel.materialized pre_delete.connect(on_pre_delete_boundary, clazz, dispatch_uid=make_dispatch_uid(pre_delete, on_pre_delete_boundary, clazz)) pre_save.connect(on_pre_save_boundary, clazz, dispatch_uid=make_dispatch_uid(pre_save, on_pre_save_boundary, clazz))
for language in languages ] # ============================================================================== # Entity model event handlers # ============================================================================== # invalidate after terms set changed Model = EntityModel.terms.through @receiver(m2m_changed, sender=Model, dispatch_uid=make_dispatch_uid(m2m_changed, 'invalidate_after_terms_set_changed', Model)) def invalidate_after_terms_set_changed(sender, instance, **kwargs): if getattr(instance, "_during_terms_normalization", False): return action = kwargs.pop('action', None) if action in ["pre_remove", "pre_add"]: valid_pk_set = getattr(instance, "_valid_pk_set", None) if valid_pk_set is None: valid_pk_set = set() setattr(instance, "_valid_pk_set", valid_pk_set) pk_set = kwargs.pop('pk_set') if getattr(instance, "_during_terms_validation", False):
**{"{}___customer_id".format(x.__name__): instance.pk} ) for x in _model_with_customer_category_mixin] entities = EntityModel.objects.filter(reduce(OR, q_lst)) for entity in entities: entity.save(validate_customer_category=True) # def on_pre_delete_customer(sender, instance, **kwargs): # pass #============================================================================== # Connect #============================================================================== # EmailCategory email_category_model = EmailCategoryModel.materialized # pre_delete.connect(on_pre_delete_email_category, email_category_model, # dispatch_uid=make_dispatch_uid(pre_delete, on_pre_delete_email_category, email_category_model)) pre_save.connect(on_pre_save_email_category, email_category_model, dispatch_uid=make_dispatch_uid(pre_save, on_pre_save_email_category, email_category_model)) # Customer customer_model = CustomerModel.materialized # pre_delete.connect(on_pre_delete_customer, customer_model, # dispatch_uid=make_dispatch_uid(pre_delete, on_pre_delete_customer, customer_model)) pre_save.connect(on_pre_save_customer, customer_model, dispatch_uid=make_dispatch_uid(pre_save, on_pre_save_customer, customer_model))
#============================================================================== # BaseTemplate and subclass models event handlers #============================================================================== def invalidate_entity_after_save(sender, instance, **kwargs): """ Clear templates buffer RUS: Очищает буфер template_buffer после сохранения шаблонных тегов. """ BaseRenderTemplateTag.clear_template_buffer() def invalidate_entity_before_delete(sender, instance, **kwargs): """ RUS: Очищает буфер template_buffer после сохранения шаблонных тегов. """ invalidate_entity_after_save(sender, instance, **kwargs) # отправляет сигналы обработчику после сохранения и перед удалением шаблонных тегов. Model = BaseTemplate for clazz in itertools.chain([Model], Model.get_all_subclasses()): pre_delete.connect(invalidate_entity_before_delete, clazz, dispatch_uid=make_dispatch_uid( pre_delete, invalidate_entity_before_delete, clazz)) post_save.connect(invalidate_entity_after_save, clazz, dispatch_uid=make_dispatch_uid( post_save, invalidate_entity_after_save, clazz))
#============================================================================== # DataMartModel and subclass models event handlers #============================================================================== def invalidate_data_mart_after_save(sender, instance, **kwargs): """ Clear simple page buffer RUS: Очищает буфер simple_page_buffer после сохранения витрины данных. """ clear_simple_page_buffer() def invalidate_data_mart_before_delete(sender, instance, **kwargs): """ RUS: Очищает буфер simple_page_buffer перед удалением витрины данных. """ invalidate_data_mart_after_save(sender, instance, **kwargs) # RUS: Обработчик событий отправляет сигналы после сохранения и перед удалением витрины данных. Model = DataMartModel.materialized for clazz in itertools.chain([Model], Model.get_all_subclasses()): pre_delete.connect(invalidate_data_mart_before_delete, clazz, dispatch_uid=make_dispatch_uid( pre_delete, invalidate_data_mart_before_delete, clazz)) post_save.connect(invalidate_data_mart_after_save, clazz, dispatch_uid=make_dispatch_uid( post_save, invalidate_data_mart_after_save, clazz))
def invalidate_term_before_delete(sender, instance, **kwargs): """ RUS: Очищает ключ кеша макета отображения перед удалением терминов. """ layout = get_views_layouts().get(instance.key, None) if layout is not None: layout.hard_delete() setattr(sender, VIEW_LAYOUT_CACHE_KEY, None) # отправляет сигналы обработчику PageLayout (макету страницы) после сохранения и перед удалением терминов. pre_save.connect( invalidate_term_before_save, sender=PageLayout, dispatch_uid=make_dispatch_uid( pre_save, invalidate_term_before_save, PageLayout ) ) pre_delete.connect( invalidate_term_before_delete, sender=PageLayout, dispatch_uid=make_dispatch_uid( pre_delete, invalidate_term_before_delete, PageLayout ) ) #==============================================================================
# ============================================================================== def on_pre_save_postzone(sender, instance, **kwargs): if instance.pk is not None: try: origin = sender.objects.get(pk=instance.pk) except sender.DoesNotExist: pass else: if origin.term != instance.term: origin_zone_term_id = origin.term.id zone_term_id = instance.term.id entities_ids = EntityModel.objects.instance_of( *_model_with_place_mixin).filter( terms__id=origin_zone_term_id).values_list('id', flat=True) EntityModel.terms.through.objects.filter( entity_id__in=list(entities_ids), term_id=origin_zone_term_id).update(term_id=zone_term_id) #============================================================================== # Connect #============================================================================== clazz = PostZoneModel.materialized pre_save.connect(on_pre_save_postzone, clazz, dispatch_uid=make_dispatch_uid(pre_save, on_pre_save_postzone, clazz))
#============================================================================== def invalidate_simple_page_after_save(sender, instance, **kwargs): """ Clear simple page buffer RUS: Очищает буфер simple_page_buffer после сохранения страницы. """ clear_simple_page_buffer() def invalidate_simple_page_before_delete(sender, instance, **kwargs): """ RUS: Очищает буфер simple_page_buffer перед удалением страницы. """ invalidate_simple_page_after_save(sender, instance, **kwargs) Model = SimplePage # отправляет сигналы обработчику SimplePage после сохранения и перед удалением страницы. post_save.connect(invalidate_simple_page_after_save, sender=Model, dispatch_uid=make_dispatch_uid( post_save, invalidate_simple_page_after_save, Model )) pre_delete.connect(invalidate_simple_page_before_delete, sender=Model, dispatch_uid=make_dispatch_uid( pre_delete, invalidate_simple_page_before_delete, Model ))
sender.id, app_label, label, sender.data_mart_model, 'media', language[0]) for label in ('summary', 'detail') for language in languages ] #============================================================================== # DataMart model event handlers #============================================================================== Model = DataMartModel.terms.through @receiver(m2m_changed, sender=Model, dispatch_uid=make_dispatch_uid(m2m_changed, 'invalidate_after_terms_set_changed', Model)) def invalidate_after_terms_set_changed(sender, instance, **kwargs): if getattr(instance, "_during_terms_normalization", False): return action = kwargs.pop('action', None) if action in ["pre_remove", "pre_add"]: valid_pk_set = getattr(instance, "_valid_pk_set", None) if valid_pk_set is None: valid_pk_set = set() setattr(instance, "_valid_pk_set", valid_pk_set) pk_set = kwargs.pop('pk_set', None)
""" RUS: Очищает буфер simple_page_buffer перед удалением терминов. """ invalidate_term_after_save(sender, instance, **kwargs) def invalidate_term_after_move(sender, instance, target, position, prev_parent, **kwargs): """ RUS: Очищает буфер simple_page_buffer после сохранения перемещенных терминов. """ invalidate_term_after_save(sender, instance, **kwargs) # RUS: Обработчик событий отправляет сигналы после сохранения, перед удалением и после перемещения терминов. Model = TermModel.materialized post_save.connect(invalidate_term_after_save, sender=Model, dispatch_uid=make_dispatch_uid(post_save, invalidate_term_after_save, Model)) pre_delete.connect(invalidate_term_before_delete, sender=Model, dispatch_uid=make_dispatch_uid( pre_delete, invalidate_term_before_delete, Model)) move_to_done.connect(invalidate_term_after_move, sender=Model, dispatch_uid=make_dispatch_uid( move_to_done, invalidate_term_after_move, Model))