tags dynamic filters #4

This commit is contained in:
Kuroshini 2019-12-03 23:49:45 +03:00
parent eaf5c41e1d
commit f9fb2aa17e

View File

@ -120,7 +120,7 @@ class CustomFacetedSearchFilterBackend(FacetedSearchFilterBackend):
facet_name = '_filter_' + __field
for category in TagCategory.objects.prefetch_related('tags').filter(public=True,
value_type=TagCategory.LIST):
tags_to_remove = list(map(lambda t: t.id, category.tags.all()))
tags_to_remove = list(map(lambda t: str(t.id), category.tags.all()))
qs = queryset.__copy__()
qs.query = queryset.query._clone()
filterer = make_tags_filter(__facet, tags_to_remove)
@ -142,16 +142,16 @@ class CustomFacetedSearchFilterBackend(FacetedSearchFilterBackend):
filter=agg_filter
).bucket(__field, agg)
tag_facets.append(qs.execute().aggregations[facet_name])
preserve_ids.append(tags_to_remove)
preserve_ids.append(list(map(int, tags_to_remove)))
view.paginator.facets_computed.update({facet_name: self.merge_buckets(tag_facets, preserve_ids)})
return queryset
@staticmethod
def merge_buckets(buckets: list, presrve_ids: list):
def merge_buckets(buckets: list, preserve_ids: list):
"""Reduces all buckets preserving class"""
result_bucket = buckets[0]
result_bucket.tag.buckets = list(filter(lambda x: x['key'] in presrve_ids[0], result_bucket.tag.buckets._l_))
for bucket, ids in list(zip(buckets, presrve_ids))[1:]:
result_bucket.tag.buckets = list(filter(lambda x: x['key'] in preserve_ids[0], result_bucket.tag.buckets._l_))
for bucket, ids in list(zip(buckets, preserve_ids))[1:]:
for tag in bucket.tag.buckets._l_:
if tag['key'] in ids:
result_bucket.tag.buckets.append(tag)