Skip to content

Commit

Permalink
better fix for counting facets
Browse files Browse the repository at this point in the history
  • Loading branch information
smilerz committed Jan 17, 2022
1 parent 2595a26 commit 4e0cc34
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
5 changes: 3 additions & 2 deletions cookbook/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,11 @@ def __init__(self, *args, **kwargs):
class Meta:
model = Space

fields = ('food_inherit', 'reset_food_inherit',)
fields = ('food_inherit', 'reset_food_inherit', 'show_facet_count')

help_texts = {
'food_inherit': _('Fields on food that should be inherited by default.'), }
'food_inherit': _('Fields on food that should be inherited by default.'),
'show_facet_count': _('Show recipe counts on search filters'), }

widgets = {
'food_inherit': MultiSelectWidget
Expand Down
29 changes: 19 additions & 10 deletions cookbook/helper/recipe_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,13 @@ def get_books(self):

def get_ratings(self):
if self.Ratings is None:
if self._queryset is None:
self._queryset = Recipe.objects.filter(id__in=self._recipe_list)
rating_qs = self._queryset.annotate(rating=Round(Avg(Case(When(cooklog__created_by=self._request.user, then='cooklog__rating'), default=Value(0)))))
self.Ratings = dict(Counter(r.rating for r in rating_qs))
if not self._request.space.demo and self._request.space.show_facet_count:
if self._queryset is None:
self._queryset = Recipe.objects.filter(id__in=self._recipe_list)
rating_qs = self._queryset.annotate(rating=Round(Avg(Case(When(cooklog__created_by=self._request.user, then='cooklog__rating'), default=Value(0)))))
self.Ratings = dict(Counter(r.rating for r in rating_qs))
else:
self.Rating = {}
self.set_cache('Ratings', self.Ratings)
return self.Ratings

Expand Down Expand Up @@ -647,17 +650,23 @@ def _keyword_queryset(self, queryset, keyword=None):
depth = getattr(keyword, 'depth', 0) + 1
steplen = depth * Keyword.steplen

return queryset.annotate(count=Coalesce(Subquery(self._recipe_count_queryset('keywords', depth, steplen)), 0)
).filter(depth=depth, count__gt=0
).values('id', 'name', 'count', 'numchild').order_by('name')
if not self._request.space.demo and self._request.space.show_facet_count:
return queryset.annotate(count=Coalesce(Subquery(self._recipe_count_queryset('keywords', depth, steplen)), 0)
).filter(depth=depth, count__gt=0
).values('id', 'name', 'count', 'numchild').order_by('name')
else:
return queryset.filter(depth=depth).values('id', 'name', 'numchild').order_by('name')

def _food_queryset(self, queryset, food=None):
depth = getattr(food, 'depth', 0) + 1
steplen = depth * Food.steplen

return queryset.annotate(count=Coalesce(Subquery(self._recipe_count_queryset('steps__ingredients__food', depth, steplen)), 0)
).filter(depth__lte=depth, count__gt=0
).values('id', 'name', 'count', 'numchild').order_by('name')
if not self._request.space.demo and self._request.space.show_facet_count:
return queryset.annotate(count=Coalesce(Subquery(self._recipe_count_queryset('steps__ingredients__food', depth, steplen)), 0)
).filter(depth__lte=depth, count__gt=0
).values('id', 'name', 'count', 'numchild').order_by('name')
else:
return queryset.filter(depth__lte=depth).values('id', 'name', 'numchild').order_by('name')


# # TODO: This might be faster https://github.com/django-treebeard/django-treebeard/issues/115
Expand Down
18 changes: 18 additions & 0 deletions cookbook/migrations/0164_space_show_facet_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.11 on 2022-01-17 22:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('cookbook', '0163_auto_20220105_0758'),
]

operations = [
migrations.AddField(
model_name='space',
name='show_facet_count',
field=models.BooleanField(default=False),
),
]
1 change: 1 addition & 0 deletions cookbook/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class Space(ExportModelOperationsMixin('space'), models.Model):
allow_sharing = models.BooleanField(default=True)
demo = models.BooleanField(default=False)
food_inherit = models.ManyToManyField(FoodInheritField, blank=True)
show_facet_count = models.BooleanField(default=False)

def __str__(self):
return self.name
Expand Down
2 changes: 2 additions & 0 deletions cookbook/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ def space(request):
form = SpacePreferenceForm(request.POST, prefix='space')
if form.is_valid():
request.space.food_inherit.set(form.cleaned_data['food_inherit'])
request.space.show_facet_count = form.cleaned_data['show_facet_count']
request.space.save()
if form.cleaned_data['reset_food_inherit']:
Food.reset_inheritance(space=request.space)

Expand Down
24 changes: 17 additions & 7 deletions vue/src/apps/RecipeSearchView/RecipeSearchView.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div id="app" style="margin-bottom: 4vh">
<RecipeSwitcher ref="ref_recipe_switcher"/>
<RecipeSwitcher ref="ref_recipe_switcher" />
<div class="row">
<div class="col-12 col-xl-8 col-lg-10 offset-xl-2 offset-lg-1">
<div class="row">
Expand Down Expand Up @@ -295,13 +295,23 @@ export default {
},
computed: {
ratingOptions: function () {
let ratingCount = undefined
if (Object.keys(this.facets?.Ratings ?? {}).length === 0) {
ratingCount = (x) => {
return ""
}
} else {
ratingCount = (x) => {
return ` (${x})`
}
}
return [
{ id: 5, label: "⭐⭐⭐⭐⭐" + " (" + (this.facets.Ratings?.["5.0"] ?? 0) + ")" },
{ id: 4, label: "⭐⭐⭐⭐ " + this.$t("and_up") + " (" + (this.facets.Ratings?.["4.0"] ?? 0) + ")" },
{ id: 3, label: "⭐⭐⭐ " + this.$t("and_up") + " (" + (this.facets.Ratings?.["3.0"] ?? 0) + ")" },
{ id: 2, label: "⭐⭐ " + this.$t("and_up") + " (" + (this.facets.Ratings?.["2.0"] ?? 0) + ")" },
{ id: 1, label: "" + this.$t("and_up") + " (" + (this.facets.Ratings?.["1.0"] ?? 0) + ")" },
{ id: 0, label: this.$t("Unrated") + " (" + (this.facets.Ratings?.["0.0"] ?? 0) + ")" },
{ id: 5, label: "⭐⭐⭐⭐⭐" + ratingCount(this.facets.Ratings?.["5.0"] ?? 0) },
{ id: 4, label: "⭐⭐⭐⭐ " + this.$t("and_up") + ratingCount(this.facets.Ratings?.["4.0"] ?? 0) },
{ id: 3, label: "⭐⭐⭐ " + this.$t("and_up") + ratingCount(this.facets.Ratings?.["3.0"] ?? 0) },
{ id: 2, label: "⭐⭐ " + this.$t("and_up") + ratingCount(this.facets.Ratings?.["2.0"] ?? 0) },
{ id: 1, label: "" + this.$t("and_up") + ratingCount(this.facets.Ratings?.["1.0"] ?? 0) },
{ id: 0, label: this.$t("Unrated") + ratingCount(this.facets.Ratings?.["0.0"] ?? 0) },
]
},
searchFiltered: function () {
Expand Down

0 comments on commit 4e0cc34

Please sign in to comment.