mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-03 13:19:16 -05:00
Merge pull request #1374 from smilerz/patch-updated-search
fix sort by new and show recent recipes
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user