From 5c4c4c60c3841cbeb14716b0483ced9f0cc76155 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Mon, 18 Nov 2019 20:12:58 +0300 Subject: [PATCH] ES as separate server --- apps/establishment/models.py | 9 ++++----- apps/news/models.py | 4 ++-- apps/news/serializers.py | 2 +- apps/product/models.py | 8 ++++---- apps/search_indexes/documents/news.py | 7 +++++++ apps/search_indexes/documents/product.py | 8 ++++++++ apps/search_indexes/serializers.py | 4 ++-- apps/utils/models.py | 13 +++++++++++++ 8 files changed, 41 insertions(+), 14 deletions(-) diff --git a/apps/establishment/models.py b/apps/establishment/models.py index 3bd9dd00..7fc0b2d6 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -22,11 +22,10 @@ from django.core.validators import MinValueValidator, MaxValueValidator from collection.models import Collection from location.models import Address from main.models import Award, Currency -from tag.models import TagCategory from review.models import Review from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin, TranslatedFieldsMixin, BaseAttributes, GalleryModelMixin, - IntermediateGalleryModelMixin) + IntermediateGalleryModelMixin, HasTagsMixin) # todo: establishment type&subtypes check @@ -320,7 +319,7 @@ class EstablishmentQuerySet(models.QuerySet): return self.exclude(address__city__country__in=countries) -class Establishment(GalleryModelMixin, ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin): +class Establishment(GalleryModelMixin, ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin, HasTagsMixin): """Establishment model.""" # todo: delete image URL fields after moving on gallery @@ -418,9 +417,9 @@ class Establishment(GalleryModelMixin, ProjectBaseMixin, URLImageMixin, Translat @property def visible_tags(self): - return self.tags.exclude(category__value_type=TagCategory.BOOLEAN)\ + return super().visible_tags\ .exclude(category__index_name__in=['guide', 'collection', 'purchased_item', - 'business_tag', 'business_tags_de']) + 'business_tag', 'business_tags_de'])\ # todo: recalculate toque_number def recalculate_toque_number(self): diff --git a/apps/news/models.py b/apps/news/models.py index 7e26de5f..d0e79c64 100644 --- a/apps/news/models.py +++ b/apps/news/models.py @@ -7,7 +7,7 @@ from django.utils.translation import gettext_lazy as _ from rest_framework.reverse import reverse from rating.models import Rating, ViewCount -from utils.models import (BaseAttributes, TJSONField, TranslatedFieldsMixin, +from utils.models import (BaseAttributes, TJSONField, TranslatedFieldsMixin, HasTagsMixin, ProjectBaseMixin, GalleryModelMixin, IntermediateGalleryModelMixin) from utils.querysets import TranslationQuerysetMixin from django.conf import settings @@ -126,7 +126,7 @@ class NewsQuerySet(TranslationQuerysetMixin): ) -class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin): +class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin): """News model.""" STR_FIELD_NAME = 'title' diff --git a/apps/news/serializers.py b/apps/news/serializers.py index 3fca14b8..f0390ea5 100644 --- a/apps/news/serializers.py +++ b/apps/news/serializers.py @@ -64,7 +64,7 @@ class NewsBaseSerializer(ProjectModelSerializer): title_translated = TranslatedField() subtitle_translated = TranslatedField() news_type = NewsTypeSerializer(read_only=True) - tags = TagBaseSerializer(read_only=True, many=True) + tags = TagBaseSerializer(read_only=True, many=True, source='related_tags') in_favorites = serializers.BooleanField(allow_null=True) view_counter = serializers.IntegerField(read_only=True) diff --git a/apps/product/models.py b/apps/product/models.py index 4a92a5fa..126f59e9 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -7,7 +7,7 @@ from django.db.models import Case, When from django.utils.translation import gettext_lazy as _ from django.core.validators import MaxValueValidator, MinValueValidator -from utils.models import (BaseAttributes, ProjectBaseMixin, +from utils.models import (BaseAttributes, ProjectBaseMixin, HasTagsMixin, TranslatedFieldsMixin, TJSONField, GalleryModelMixin, IntermediateGalleryModelMixin) @@ -131,7 +131,7 @@ class ProductQuerySet(models.QuerySet): ) -class Product(GalleryModelMixin, TranslatedFieldsMixin, BaseAttributes): +class Product(GalleryModelMixin, TranslatedFieldsMixin, BaseAttributes, HasTagsMixin): """Product models.""" EARLIEST_VINTAGE_YEAR = 1700 @@ -256,8 +256,8 @@ class Product(GalleryModelMixin, TranslatedFieldsMixin, BaseAttributes): @property def related_tags(self): - return self.tags.exclude(category__index_name__in=['sugar-content', 'wine-color', 'bottles-produced', - 'serial-number', 'grape-variety']).prefetch_related('category') + return super().visible_tags.exclude(category__index_name__in=['sugar-content', 'wine-color', + 'bottles-produced','serial-number', 'grape-variety']) @property def display_name(self): diff --git a/apps/search_indexes/documents/news.py b/apps/search_indexes/documents/news.py index ff659416..e39036d3 100644 --- a/apps/search_indexes/documents/news.py +++ b/apps/search_indexes/documents/news.py @@ -34,6 +34,13 @@ class NewsDocument(Document): 'value': fields.KeywordField() }, multi=True) + visible_tags = fields.ObjectField( + properties={ + 'id': fields.IntegerField(attr='id'), + 'label': fields.ObjectField(attr='label_indexing', + properties=OBJECT_FIELD_PROPERTIES), + }, + multi=True) class Django: diff --git a/apps/search_indexes/documents/product.py b/apps/search_indexes/documents/product.py index 874be5a6..4ac42a56 100644 --- a/apps/search_indexes/documents/product.py +++ b/apps/search_indexes/documents/product.py @@ -107,6 +107,14 @@ class ProductDocument(Document): }, multi=True ) + related_tags = fields.ObjectField( + properties={ + 'id': fields.IntegerField(), + 'label': fields.ObjectField(attr='label_indexing', properties=OBJECT_FIELD_PROPERTIES), + 'value': fields.KeywordField(), + }, + multi=True + ) name = fields.TextField(attr='display_name', analyzer='english') name_ru = fields.TextField(attr='display_name', analyzer='russian') name_fr = fields.TextField(attr='display_name', analyzer='french') diff --git a/apps/search_indexes/serializers.py b/apps/search_indexes/serializers.py index 1a3dea7a..774c4b1b 100644 --- a/apps/search_indexes/serializers.py +++ b/apps/search_indexes/serializers.py @@ -148,7 +148,7 @@ class NewsDocumentSerializer(DocumentSerializer): title_translated = serializers.SerializerMethodField(allow_null=True) subtitle_translated = serializers.SerializerMethodField(allow_null=True) news_type = NewsTypeSerializer() - tags = TagsDocumentSerializer(many=True) + tags = TagsDocumentSerializer(many=True, source='visible_tags') class Meta: """Meta class.""" @@ -214,7 +214,7 @@ class EstablishmentDocumentSerializer(DocumentSerializer): class ProductDocumentSerializer(DocumentSerializer): """Product document serializer""" - tags = TagsDocumentSerializer(many=True) + tags = TagsDocumentSerializer(many=True, source='related_tags') subtypes = ProductSubtypeDocumentSerializer(many=True, allow_null=True) wine_region = WineRegionDocumentSerializer(allow_null=True) wine_colors = TagDocumentSerializer(many=True) diff --git a/apps/utils/models.py b/apps/utils/models.py index a325c8ff..e508be6f 100644 --- a/apps/utils/models.py +++ b/apps/utils/models.py @@ -418,4 +418,17 @@ class IntermediateGalleryModelMixin(models.Model): return self.image.title if self.image.title else self.id +class HasTagsMixin(models.Model): + """Mixin for filtering tags""" + + @property + def visible_tags(self): + return self.tags.filter(category__public=True).prefetch_related('category')\ + .exclude(category__value_type='bool') + + class Meta: + """Meta class.""" + abstract = True + + timezone.datetime.now().date().isoformat() \ No newline at end of file