diff --git a/apps/tag/views.py b/apps/tag/views.py index 6ad7f20a..6a594220 100644 --- a/apps/tag/views.py +++ b/apps/tag/views.py @@ -3,6 +3,9 @@ from django.conf import settings from rest_framework import generics, mixins, permissions, status, viewsets from rest_framework.decorators import action from rest_framework.response import Response +from rest_framework.serializers import ValidationError +from django.utils.translation import gettext_lazy as _ +from search_indexes import views as search_views from location.models import WineRegion from product.models import ProductType @@ -82,7 +85,7 @@ class FiltersTagCategoryViewSet(TagCategoryViewSet): if params_type == 'restaurant': additional_flags += ['toque_number', 'works_noon', 'works_evening', 'works_now'] - elif params_type == 'winery': + elif params_type in ['winery', 'wine']: additional_flags += ['wine_region'] elif params_type == 'artisan': @@ -175,12 +178,42 @@ class FiltersTagCategoryViewSet(TagCategoryViewSet): } result_list.append(works_at_weekday) - if 'tags_id__in' in query_params: - # filtering by params_type and tags id - # todo: result_list.append( filtering_data ) - pass + search_view_class = self.define_search_view_by_request(request) + facets = search_view_class.as_view({'get': 'list'})(self.mutate_request(self.request)).data['facets'] + return Response(self.remove_empty_filters(result_list, facets)) - return Response(result_list) + @staticmethod + def mutate_request(request): + """Remove all filtering get params and remove s_ from the rest of them""" + request.GET._mutable = True + for name in request.query_params.copy().keys(): + value = request.query_params.pop(name) + if name.startswith('s_'): + request.query_params[name[2:]] = value[0] + request.GET._mutable = False + return request._request + + @staticmethod + def define_search_view_by_request(request): + request.GET._mutable = True + if request.query_params.get('items'): + items = request.query_params.pop('items')[0] + else: + raise ValidationError({'detail': _('Missing required "items" parameter')}) + item_to_class = { + 'news': search_views.NewsDocumentViewSet, + 'establishments': search_views.EstablishmentDocumentViewSet, + 'products': search_views.ProductDocumentViewSet, + } + klass = item_to_class.get(items) + if klass is None: + raise ValidationError({'detail': _('news/establishments/products')}) + request.GET._mutable = False + return klass + + @staticmethod + def remove_empty_filters(filters, facets): + return filters # BackOffice user`s views & viewsets