From 530b1a8986fb5f3c8b38d6ef5bf001dd112b1921 Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 19 Jan 2022 08:39:40 -0600 Subject: [PATCH] fix sort by new --- cookbook/helper/recipe_search.py | 13 +++ cookbook/serializer.py | 3 +- .../RecipeSearchView/RecipeSearchView.vue | 97 ++++++++----------- 3 files changed, 52 insertions(+), 61 deletions(-) diff --git a/cookbook/helper/recipe_search.py b/cookbook/helper/recipe_search.py index fb074b2ae..f5560ca1b 100644 --- a/cookbook/helper/recipe_search.py +++ b/cookbook/helper/recipe_search.py @@ -85,6 +85,7 @@ class RecipeSearch(): self._queryset = queryset self.recently_viewed_recipes(self._last_viewed) self._favorite_recipes() + self._new_recipes() # self._last_viewed() # self._last_cooked() self.keyword_filters(keywords=self._keywords, operator=self._keywords_or) @@ -110,6 +111,8 @@ class RecipeSearch(): order = [] # TODO add user preferences here: name, date cooked, rating, times cooked, date created, date viewed, random if '-recent' in self.orderby and self._last_viewed: order += ['-recent'] + if '-new_recipe' in self.orderby and self._new: + order += ['-new_recipe'] if '-rank' in self.orderby and '-simularity' in self.orderby: self._queryset = self._queryset.annotate(score=Sum(F('rank')+F('simularity'))) @@ -153,6 +156,16 @@ class RecipeSearch(): else: self._queryset = self._queryset.filter(name__icontains=self._string) + def _new_recipes(self, new_days=7): + # TODO make new days a user-setting + if not self._new: + return + self._queryset = ( + self._queryset.annotate(new_recipe=Case( + When(created_at__gte=(timezone.now() - timedelta(days=new_days)), then=('pk')), default=Value(0), )) + ) + self.orderby += ['-new_recipe'] + def recently_viewed_recipes(self, last_viewed=None): if not last_viewed: return diff --git a/cookbook/serializer.py b/cookbook/serializer.py index ad6015e70..a9d8e05fc 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -502,7 +502,6 @@ class NutritionInformationSerializer(serializers.ModelSerializer): proteins = CustomDecimalField() calories = CustomDecimalField() - def create(self, validated_data): validated_data['space'] = self.context['request'].space return super().create(validated_data) @@ -534,7 +533,7 @@ class RecipeBaseSerializer(WritableNestedModelSerializer): # TODO make days of new recipe a setting def is_recipe_new(self, obj): - if obj.created_at > (timezone.now() - timedelta(days=7)): + if getattr(obj, 'new_recipe', None) or obj.created_at > (timezone.now() - timedelta(days=7)): return True else: return False diff --git a/vue/src/apps/RecipeSearchView/RecipeSearchView.vue b/vue/src/apps/RecipeSearchView/RecipeSearchView.vue index 8d0378180..669af365e 100644 --- a/vue/src/apps/RecipeSearchView/RecipeSearchView.vue +++ b/vue/src/apps/RecipeSearchView/RecipeSearchView.vue @@ -16,17 +16,17 @@ - + - - + + - +
@@ -250,7 +250,7 @@
-