Extra fields for establishments

This commit is contained in:
Kuroshini 2019-12-02 21:26:55 +03:00
parent 4d54c84b51
commit 140ba040e7
5 changed files with 39 additions and 3 deletions

View File

@ -14,7 +14,7 @@ from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models from django.db import models
from django.db.models import When, Case, F, ExpressionWrapper, Subquery, Q from django.db.models import When, Case, F, ExpressionWrapper, Subquery, Q, Prefetch
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField from phonenumber_field.modelfields import PhoneNumberField
@ -23,6 +23,7 @@ from timezone_field import TimeZoneField
from collection.models import Collection from collection.models import Collection
from location.models import Address from location.models import Address
from main.models import Award, Currency from main.models import Award, Currency
from tag.models import Tag
from review.models import Review from review.models import Review
from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin, from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
TranslatedFieldsMixin, BaseAttributes, GalleryModelMixin, TranslatedFieldsMixin, BaseAttributes, GalleryModelMixin,
@ -321,6 +322,13 @@ class EstablishmentQuerySet(models.QuerySet):
"""Exclude countries.""" """Exclude countries."""
return self.exclude(address__city__country__in=countries) return self.exclude(address__city__country__in=countries)
def with_certain_tag_category_related(self, index_name, attr_name):
"""Includes extra tags."""
return self.prefetch_related(
Prefetch('tags', queryset=Tag.objects.filter(category__index_name=index_name),
to_attr=attr_name)
)
class Establishment(GalleryModelMixin, ProjectBaseMixin, URLImageMixin, class Establishment(GalleryModelMixin, ProjectBaseMixin, URLImageMixin,
TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin): TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin):

View File

@ -320,12 +320,16 @@ class EstablishmentListRetrieveSerializer(EstablishmentBaseSerializer):
address = AddressDetailSerializer() address = AddressDetailSerializer()
schedule = ScheduleRUDSerializer(many=True, allow_null=True) schedule = ScheduleRUDSerializer(many=True, allow_null=True)
restaurant_category = TagBaseSerializer(read_only=True, many=True, allow_null=True)
restaurant_cuisine = TagBaseSerializer(read_only=True, many=True, allow_null=True)
class Meta(EstablishmentBaseSerializer.Meta): class Meta(EstablishmentBaseSerializer.Meta):
"""Meta class.""" """Meta class."""
fields = EstablishmentBaseSerializer.Meta.fields + [ fields = EstablishmentBaseSerializer.Meta.fields + [
'schedule', 'schedule',
'restaurant_category',
'restaurant_cuisine',
] ]

View File

@ -35,7 +35,9 @@ class EstablishmentListView(EstablishmentMixinView, generics.ListAPIView):
def get_queryset(self): def get_queryset(self):
return super().get_queryset().with_schedule() \ return super().get_queryset().with_schedule() \
.with_extended_address_related().with_currency_related() .with_extended_address_related().with_currency_related() \
.with_certain_tag_category_related('category', 'restaurant_category') \
.with_certain_tag_category_related('cuisine', 'restaurant_cuisine') \
class EstablishmentRetrieveView(EstablishmentMixinView, generics.RetrieveAPIView): class EstablishmentRetrieveView(EstablishmentMixinView, generics.RetrieveAPIView):

View File

@ -49,6 +49,22 @@ class EstablishmentDocument(Document):
'value': fields.KeywordField(), 'value': fields.KeywordField(),
}, },
multi=True) multi=True)
restaurant_category = fields.ObjectField(
properties={
'id': fields.IntegerField(attr='id'),
'label': fields.ObjectField(attr='label_indexing',
properties=OBJECT_FIELD_PROPERTIES),
'value': fields.KeywordField(),
},
multi=True)
restaurant_cuisine = fields.ObjectField(
properties={
'id': fields.IntegerField(attr='id'),
'label': fields.ObjectField(attr='label_indexing',
properties=OBJECT_FIELD_PROPERTIES),
'value': fields.KeywordField(),
},
multi=True)
visible_tags = fields.ObjectField( visible_tags = fields.ObjectField(
properties={ properties={
'id': fields.IntegerField(attr='id'), 'id': fields.IntegerField(attr='id'),
@ -142,4 +158,6 @@ class EstablishmentDocument(Document):
) )
def get_queryset(self): def get_queryset(self):
return super().get_queryset().with_es_related() return super().get_queryset().with_es_related() \
.with_certain_tag_category_related('category', 'restaurant_category') \
.with_certain_tag_category_related('cuisine', 'restaurant_cuisine')

View File

@ -229,6 +229,8 @@ class EstablishmentDocumentSerializer(InFavoritesMixin, DocumentSerializer):
establishment_subtypes = EstablishmentTypeSerializer(many=True) establishment_subtypes = EstablishmentTypeSerializer(many=True)
address = AddressDocumentSerializer(allow_null=True) address = AddressDocumentSerializer(allow_null=True)
tags = TagsDocumentSerializer(many=True, source='visible_tags') tags = TagsDocumentSerializer(many=True, source='visible_tags')
restaurant_category = TagsDocumentSerializer(many=True)
restaurant_cuisine = TagsDocumentSerializer(many=True)
schedule = ScheduleDocumentSerializer(many=True, allow_null=True) schedule = ScheduleDocumentSerializer(many=True, allow_null=True)
class Meta: class Meta:
@ -247,6 +249,8 @@ class EstablishmentDocumentSerializer(InFavoritesMixin, DocumentSerializer):
'preview_image', 'preview_image',
'address', 'address',
'tags', 'tags',
'restaurant_category',
'restaurant_cuisine',
'schedule', 'schedule',
'works_noon', 'works_noon',
'works_evening', 'works_evening',