From b469770ff0d440c098a9e792be59144e49f0274d Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Tue, 17 Dec 2019 20:01:36 +0300 Subject: [PATCH] news active logic depended on activated descriptions --- apps/news/models.py | 5 +++++ apps/news/views.py | 3 ++- apps/search_indexes/documents/news.py | 6 ++++-- apps/search_indexes/serializers.py | 3 ++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/apps/news/models.py b/apps/news/models.py index 19a27463..ce785207 100644 --- a/apps/news/models.py +++ b/apps/news/models.py @@ -248,6 +248,11 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi """Duplicates for this news item excluding same country code labeled""" return News.objects.filter(duplication_uuid=self.duplication_uuid).exclude(country=self.country) + @property + def has_any_desc_active(self): + """Detects whether news item has any active description""" + return any(list(map(lambda v: v.lower() == 'true', self.locale_to_description_is_active.values()))) + @property def is_publish(self): return self.state in self.PUBLISHED_STATES diff --git a/apps/news/views.py b/apps/news/views.py index c234c10a..a8f3e389 100644 --- a/apps/news/views.py +++ b/apps/news/views.py @@ -54,7 +54,8 @@ class NewsListView(NewsMixinView, generics.ListAPIView): 'international_preferred': True, 'locale': locale, }) - return super().get_queryset(*args, **kwargs) + return super().get_queryset(*args, **kwargs)\ + .filter(locale_to_description_is_active__values__contains=['True']) class NewsDetailView(NewsMixinView, generics.RetrieveAPIView): diff --git a/apps/search_indexes/documents/news.py b/apps/search_indexes/documents/news.py index 62e3e984..4e76c303 100644 --- a/apps/search_indexes/documents/news.py +++ b/apps/search_indexes/documents/news.py @@ -3,6 +3,7 @@ from django.conf import settings from django_elasticsearch_dsl import Document, Index, fields from search_indexes.utils import OBJECT_FIELD_PROPERTIES from news import models +from json import dumps NewsIndex = Index(settings.ELASTICSEARCH_INDEX_NAMES.get(__name__, 'news')) @@ -17,7 +18,7 @@ class NewsDocument(Document): 'name': fields.KeywordField()}) title = fields.ObjectField(attr='title_indexing', properties=OBJECT_FIELD_PROPERTIES) - slugs = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES) + slugs = fields.KeywordField() backoffice_title = fields.TextField(analyzer='english') subtitle = fields.ObjectField(attr='subtitle_indexing', properties=OBJECT_FIELD_PROPERTIES) @@ -47,7 +48,7 @@ class NewsDocument(Document): start = fields.DateField(attr='start') def prepare_slugs(self, instance): - return {locale: instance.slugs.get(locale) for locale in OBJECT_FIELD_PROPERTIES} + return dumps(instance.slugs or {}) class Django: @@ -58,6 +59,7 @@ class NewsDocument(Document): 'state', 'is_highlighted', 'template', + 'has_any_desc_active', ) related_models = [models.NewsType] diff --git a/apps/search_indexes/serializers.py b/apps/search_indexes/serializers.py index 4cad05fc..2a183c10 100644 --- a/apps/search_indexes/serializers.py +++ b/apps/search_indexes/serializers.py @@ -6,6 +6,7 @@ from news.serializers import NewsTypeSerializer from search_indexes.documents import EstablishmentDocument, NewsDocument from search_indexes.documents.product import ProductDocument from search_indexes.utils import get_translated_value +from json import loads class TagsDocumentSerializer(serializers.Serializer): @@ -243,7 +244,7 @@ class NewsDocumentSerializer(InFavoritesMixin, DocumentSerializer): @staticmethod def get_slug(obj): - return get_translated_value(obj.slugs) + return get_translated_value(loads(obj.slugs)) @staticmethod def get_title_translated(obj):