dynamic filters

This commit is contained in:
Kuroshini 2019-12-13 14:57:15 +03:00
parent 295518fd12
commit 9fbb8f01de
2 changed files with 37 additions and 0 deletions

View File

@ -123,6 +123,8 @@ class FiltersTagCategoryBaseSerializer(serializers.ModelSerializer):
return obj in ['open_now', ]
def get_param_name(self, obj):
if obj.index_name == 'wine-color':
return 'wine_colors_id__in'
return 'tags_id__in'
def get_fields(self, *args, **kwargs):

View File

@ -213,6 +213,41 @@ class FiltersTagCategoryViewSet(TagCategoryViewSet):
@staticmethod
def remove_empty_filters(filters, facets):
# parse facets
if facets.get('_filter_tag'):
tags_to_preserve = list(map(lambda el: el['key'], facets['_filter_tag']['tag']['buckets']))
if facets.get('_filter_wine_colors'):
wine_colors_to_preserve = list(map(lambda el: el['key'], facets['_filter_wine_colors']['wine_colors']['buckets']))
if facets.get('_filter_wine_region_id'):
wine_regions_to_preserve = list(map(lambda el: el['key'], facets['_filter_wine_region_id']['wine_region_id']['buckets']))
if facets.get('_filter_toque_number'):
toque_numbers = list(map(lambda el: el['key'], facets['_filter_toque_number']['toque_number']['buckets']))
if facets.get('_filter_works_noon'):
works_noon = list(map(lambda el: el['key'], facets['_filter_works_noon']['works_noon']['buckets']))
if facets.get('_filter_works_evening'):
works_evening = list(map(lambda el: el['key'], facets['_filter_works_evening']['works_evening']['buckets']))
if facets.get('_filter_works_at_weekday'):
works_at_weekday = list(map(lambda el: el['key'], facets['_filter_works_at_weekday']['works_at_weekday']['buckets']))
if facets.get('_filter_works_now'):
works_now = list(map(lambda el: el['key'], facets['_filter_works_now']['works_now']['buckets']))
# remove empty filters
for category in filters:
param_name = category.get('param_name')
if param_name == 'tags_id__in':
category['filters'] = list(filter(lambda tag: tag['id'] in tags_to_preserve, category['filters']))
elif param_name == 'wine_colors_id__in':
category['filters'] = list(filter(lambda tag: tag['id'] in wine_colors_to_preserve, category['filters']))
elif param_name == 'wine_region_id__in':
category['filters'] = list(filter(lambda tag: tag['id'] in wine_regions_to_preserve, category['filters']))
elif param_name == 'toque_number__in':
category['filters'] = list(filter(lambda tag: tag['id'] in toque_numbers, category['filters']))
elif param_name == 'works_noon__in':
category['filters'] = list(filter(lambda tag: tag['id'] in works_noon, category['filters']))
elif param_name == 'works_evening__in':
category['filters'] = list(filter(lambda tag: tag['id'] in works_evening, category['filters']))
elif param_name == 'works_at_weekday__in':
category['filters'] = list(filter(lambda tag: tag['id'] in works_at_weekday, category['filters']))
return filters