diff --git a/cookbook/forms.py b/cookbook/forms.py index 78e50cf81..1e3ef6881 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -535,10 +535,11 @@ class SpacePreferenceForm(forms.ModelForm): 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 diff --git a/cookbook/helper/recipe_search.py b/cookbook/helper/recipe_search.py index 2e46bd77d..2d9641128 100644 --- a/cookbook/helper/recipe_search.py +++ b/cookbook/helper/recipe_search.py @@ -590,10 +590,13 @@ class RecipeFacet(): 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 @@ -647,17 +650,23 @@ class RecipeFacet(): 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 diff --git a/cookbook/migrations/0164_space_show_facet_count.py b/cookbook/migrations/0164_space_show_facet_count.py new file mode 100644 index 000000000..fbab0961b --- /dev/null +++ b/cookbook/migrations/0164_space_show_facet_count.py @@ -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), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 98027abf1..278324bcb 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -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 diff --git a/cookbook/views/views.py b/cookbook/views/views.py index afc49a7a4..c40dce37c 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -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) diff --git a/vue/src/apps/RecipeSearchView/RecipeSearchView.vue b/vue/src/apps/RecipeSearchView/RecipeSearchView.vue index b98ff7337..4172e7795 100644 --- a/vue/src/apps/RecipeSearchView/RecipeSearchView.vue +++ b/vue/src/apps/RecipeSearchView/RecipeSearchView.vue @@ -1,6 +1,6 @@