From 00d7e410ba36f148adc686fc74ec3663bd3cae71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Wed, 13 Nov 2019 12:37:54 +0300 Subject: [PATCH] Elastic on --- apps/search_indexes/__init__.py | 2 +- apps/search_indexes/signals.py | 154 ++++++++++++++++---------------- project/settings/base.py | 6 +- 3 files changed, 81 insertions(+), 81 deletions(-) diff --git a/apps/search_indexes/__init__.py b/apps/search_indexes/__init__.py index c45cbc38..6c812219 100644 --- a/apps/search_indexes/__init__.py +++ b/apps/search_indexes/__init__.py @@ -1 +1 @@ -# from search_indexes.signals import update_document +from search_indexes.signals import update_document diff --git a/apps/search_indexes/signals.py b/apps/search_indexes/signals.py index 6cf109fb..3da50fb8 100644 --- a/apps/search_indexes/signals.py +++ b/apps/search_indexes/signals.py @@ -1,77 +1,77 @@ -# """Search indexes app signals.""" -# from django.db.models.signals import post_save -# from django.dispatch import receiver -# from django_elasticsearch_dsl.registries import registry -# -# -# @receiver(post_save) -# def update_document(sender, **kwargs): -# from establishment.models import Establishment -# app_label = sender._meta.app_label -# model_name = sender._meta.model_name -# instance = kwargs['instance'] -# -# if app_label == 'location': -# if model_name == 'country': -# establishments = Establishment.objects.filter( -# address__city__country=instance) -# for establishment in establishments: -# registry.update(establishment) -# if model_name == 'city': -# establishments = Establishment.objects.filter( -# address__city=instance) -# for establishment in establishments: -# registry.update(establishment) -# if model_name == 'address': -# establishments = Establishment.objects.filter( -# address=instance) -# for establishment in establishments: -# registry.update(establishment) -# -# if app_label == 'establishment': -# # todo: remove after migration -# from establishment import models as establishment_models -# if model_name == 'establishmenttype': -# if isinstance(instance, establishment_models.EstablishmentType): -# establishments = Establishment.objects.filter( -# establishment_type=instance) -# for establishment in establishments: -# registry.update(establishment) -# if model_name == 'establishmentsubtype': -# if isinstance(instance, establishment_models.EstablishmentSubType): -# establishments = Establishment.objects.filter( -# establishment_subtypes=instance) -# for establishment in establishments: -# registry.update(establishment) -# -# if app_label == 'tag': -# if model_name == 'tag': -# establishments = Establishment.objects.filter(tags=instance) -# for establishment in establishments: -# registry.update(establishment) -# -# -# @receiver(post_save) -# def update_news(sender, **kwargs): -# from news.models import News -# app_label = sender._meta.app_label -# model_name = sender._meta.model_name -# instance = kwargs['instance'] -# -# if app_label == 'location': -# if model_name == 'country': -# qs = News.objects.filter(country=instance) -# for news in qs: -# registry.update(news) -# -# if app_label == 'news': -# if model_name == 'newstype': -# qs = News.objects.filter(news_type=instance) -# for news in qs: -# registry.update(news) -# -# if app_label == 'tag': -# if model_name == 'tag': -# qs = News.objects.filter(tags=instance) -# for news in qs: -# registry.update(news) +"""Search indexes app signals.""" +from django.db.models.signals import post_save +from django.dispatch import receiver +from django_elasticsearch_dsl.registries import registry + + +@receiver(post_save) +def update_document(sender, **kwargs): + from establishment.models import Establishment + app_label = sender._meta.app_label + model_name = sender._meta.model_name + instance = kwargs['instance'] + + if app_label == 'location': + if model_name == 'country': + establishments = Establishment.objects.filter( + address__city__country=instance) + for establishment in establishments: + registry.update(establishment) + if model_name == 'city': + establishments = Establishment.objects.filter( + address__city=instance) + for establishment in establishments: + registry.update(establishment) + if model_name == 'address': + establishments = Establishment.objects.filter( + address=instance) + for establishment in establishments: + registry.update(establishment) + + if app_label == 'establishment': + # todo: remove after migration + from establishment import models as establishment_models + if model_name == 'establishmenttype': + if isinstance(instance, establishment_models.EstablishmentType): + establishments = Establishment.objects.filter( + establishment_type=instance) + for establishment in establishments: + registry.update(establishment) + if model_name == 'establishmentsubtype': + if isinstance(instance, establishment_models.EstablishmentSubType): + establishments = Establishment.objects.filter( + establishment_subtypes=instance) + for establishment in establishments: + registry.update(establishment) + + if app_label == 'tag': + if model_name == 'tag': + establishments = Establishment.objects.filter(tags=instance) + for establishment in establishments: + registry.update(establishment) + + +@receiver(post_save) +def update_news(sender, **kwargs): + from news.models import News + app_label = sender._meta.app_label + model_name = sender._meta.model_name + instance = kwargs['instance'] + + if app_label == 'location': + if model_name == 'country': + qs = News.objects.filter(country=instance) + for news in qs: + registry.update(news) + + if app_label == 'news': + if model_name == 'newstype': + qs = News.objects.filter(news_type=instance) + for news in qs: + registry.update(news) + + if app_label == 'tag': + if model_name == 'tag': + qs = News.objects.filter(tags=instance) + for news in qs: + registry.update(news) diff --git a/project/settings/base.py b/project/settings/base.py index 50756ff2..5e3017c7 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -66,7 +66,7 @@ PROJECT_APPS = [ 'partner.apps.PartnerConfig', 'product.apps.ProductConfig', 'recipe.apps.RecipeConfig', - # 'search_indexes.apps.SearchIndexesConfig', + 'search_indexes.apps.SearchIndexesConfig', 'translation.apps.TranslationConfig', 'configuration.apps.ConfigurationConfig', 'timetable.apps.TimetableConfig', @@ -79,8 +79,8 @@ PROJECT_APPS = [ EXTERNAL_APPS = [ 'corsheaders', - # 'django_elasticsearch_dsl', - # 'django_elasticsearch_dsl_drf', + 'django_elasticsearch_dsl', + 'django_elasticsearch_dsl_drf', 'django_filters', 'drf_yasg', 'fcm_django',