Created new category_test url

This commit is contained in:
dormantman 2019-12-10 14:27:16 +03:00
parent a2395b807e
commit 4ec812d617
3 changed files with 176 additions and 7 deletions

View File

@ -2,11 +2,14 @@
from rest_framework import serializers from rest_framework import serializers
from rest_framework.fields import SerializerMethodField from rest_framework.fields import SerializerMethodField
from establishment.models import (Establishment, EstablishmentType) from establishment.models import Establishment
from news.models import News, NewsType from establishment.models import EstablishmentType
from news.models import News
from news.models import NewsType
from tag import models from tag import models
from utils.exceptions import (ObjectAlreadyAdded, BindingObjectNotFound, from utils.exceptions import BindingObjectNotFound
RemovedBindingObjectNotFound) from utils.exceptions import ObjectAlreadyAdded
from utils.exceptions import RemovedBindingObjectNotFound
from utils.serializers import TranslatedField from utils.serializers import TranslatedField
@ -95,6 +98,43 @@ class TagCategoryBaseSerializer(serializers.ModelSerializer):
return TagBaseSerializer(instance=tags, many=True, read_only=True).data return TagBaseSerializer(instance=tags, many=True, read_only=True).data
class TestBaseSerializer(serializers.ModelSerializer):
"""Serializer for model TagCategory."""
label_translated = TranslatedField()
tags = SerializerMethodField()
class Meta:
"""Meta class."""
model = models.TagCategory
fields = (
'id',
'transliterated_name',
'name',
'tags',
)
def get_filters(self, obj):
query_params = dict(self.context['request'].query_params)
if len(query_params) > 1:
return []
params = {}
if 'establishment_type' in query_params:
params = {
'establishments__isnull': False,
}
elif 'product_type' in query_params:
params = {
'products__isnull': False,
}
tags = obj.tags.filter(**params).distinct()
return TagBaseSerializer(instance=tags, many=True, read_only=True).data
class TagCategoryShortSerializer(serializers.ModelSerializer): class TagCategoryShortSerializer(serializers.ModelSerializer):
"""Serializer for model TagCategory.""" """Serializer for model TagCategory."""

View File

@ -7,6 +7,7 @@ app_name = 'tag'
router = SimpleRouter() router = SimpleRouter()
router.register(r'categories', views.TagCategoryViewSet) router.register(r'categories', views.TagCategoryViewSet)
router.register(r'categories_test', views.TestTagCategoryViewSet)
router.register(r'chosen_tags', views.ChosenTagsView) router.register(r'chosen_tags', views.ChosenTagsView)
urlpatterns = [ urlpatterns = [

View File

@ -1,11 +1,25 @@
"""Tag views.""" """Tag views."""
from django.conf import settings from django.conf import settings
from django_elasticsearch_dsl_drf import constants
from django_elasticsearch_dsl_drf.filter_backends import FilteringFilterBackend
from elasticsearch_dsl import TermsFacet
from rest_framework import generics
from rest_framework import mixins
from rest_framework import permissions from rest_framework import permissions
from rest_framework import viewsets, mixins, status, generics from rest_framework import status
from rest_framework import viewsets
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.response import Response from rest_framework.response import Response
from tag import filters, models, serializers from search_indexes import utils
from search_indexes.documents import EstablishmentDocument
from search_indexes.filters import CustomFacetedSearchFilterBackend
from search_indexes.filters import CustomSearchFilterBackend
from search_indexes.serializers import EstablishmentDocumentSerializer
from tag import filters
from tag import models
from tag import serializers
from utils.pagination import ESDocumentPagination
class ChosenTagsView(generics.ListAPIView, viewsets.GenericViewSet): class ChosenTagsView(generics.ListAPIView, viewsets.GenericViewSet):
@ -36,7 +50,8 @@ class ChosenTagsView(generics.ListAPIView, viewsets.GenericViewSet):
serializer = self.get_serializer(queryset, many=True) serializer = self.get_serializer(queryset, many=True)
result_list = serializer.data result_list = serializer.data
if request.query_params.get('type') and (settings.ESTABLISHMENT_CHOSEN_TAGS or settings.NEWS_CHOSEN_TAGS): if request.query_params.get('type') and (settings.ESTABLISHMENT_CHOSEN_TAGS or settings.NEWS_CHOSEN_TAGS):
ordered_list = settings.ESTABLISHMENT_CHOSEN_TAGS if request.query_params.get('type') == 'establishment' else settings.NEWS_CHOSEN_TAGS ordered_list = settings.ESTABLISHMENT_CHOSEN_TAGS if request.query_params.get(
'type') == 'establishment' else settings.NEWS_CHOSEN_TAGS
result_list = sorted(result_list, key=lambda x: ordered_list.index(x['index_name'])) result_list = sorted(result_list, key=lambda x: ordered_list.index(x['index_name']))
return Response(result_list) return Response(result_list)
@ -53,6 +68,119 @@ class TagCategoryViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
serializer_class = serializers.TagCategoryBaseSerializer serializer_class = serializers.TagCategoryBaseSerializer
# User`s views & viewsets
class TestTagCategoryViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""ViewSet for TagCategory model."""
filterset_classes = [
filters.TagCategoryFilterSet,
FilteringFilterBackend,
CustomSearchFilterBackend,
CustomFacetedSearchFilterBackend,
]
document = EstablishmentDocument
pagination_class = ESDocumentPagination
permission_classes = (permissions.AllowAny,)
queryset = models.TagCategory.objects.with_tags().with_base_related(). \
distinct()
serializer_class = serializers.TestBaseSerializer
faceted_search_fields = {
'works_at_weekday': {
'field': 'works_at_weekday',
'facet': TermsFacet,
'enabled': True,
},
'toque_number': {
'field': 'toque_number',
'enabled': True,
'facet': TermsFacet,
},
'works_noon': {
'field': 'works_noon',
'facet': TermsFacet,
'enabled': True,
},
'works_evening': {
'field': 'works_evening',
'facet': TermsFacet,
'enabled': True,
},
'works_now': {
'field': 'works_now',
'facet': TermsFacet,
'enabled': True,
},
'tag': {
'field': 'tags.id',
'facet': TermsFacet,
'enabled': True,
'options': {
'size': utils.FACET_MAX_RESPONSE,
},
}
}
search_fields = {
'name': {
'fuzziness': 'auto:2,5',
'boost': 4
},
'transliterated_name': {
'fuzziness': 'auto:2,5',
'boost': 3
},
'description': {'fuzziness': 'auto:2,5'},
}
translated_search_fields = (
'description',
)
filter_fields = {
'slug': 'slug',
'tag': {
'field': 'tags.id',
'lookups': [constants.LOOKUP_QUERY_IN]
},
'toque_number': {
'field': 'toque_number',
'lookups': [
constants.LOOKUP_FILTER_RANGE,
constants.LOOKUP_QUERY_GT,
constants.LOOKUP_QUERY_GTE,
constants.LOOKUP_QUERY_LT,
constants.LOOKUP_QUERY_LTE,
constants.LOOKUP_QUERY_IN,
]
},
'works_noon': {
'field': 'works_noon',
'lookups': [
constants.LOOKUP_QUERY_IN,
],
},
'works_at_weekday': {
'field': 'works_at_weekday',
'lookups': [
constants.LOOKUP_QUERY_IN,
],
},
'works_evening': {
'field': 'works_evening',
'lookups': [
constants.LOOKUP_QUERY_IN,
],
},
'works_now': {
'field': 'works_now',
'lookups': [
constants.LOOKUP_FILTER_TERM,
]
},
}
# BackOffice user`s views & viewsets # BackOffice user`s views & viewsets
class BindObjectMixin: class BindObjectMixin:
"""Bind object mixin.""" """Bind object mixin."""