added recently viewed to new search

This commit is contained in:
vabene1111 2021-04-26 17:21:14 +02:00
parent ec95f8032c
commit dc27f39393
5 changed files with 30 additions and 9 deletions

View File

@ -3,10 +3,11 @@ from functools import reduce
from django.contrib.postgres.search import TrigramSimilarity
from django.db.models import Q
from cookbook.models import ViewLog
from recipes import settings
def search_recipes(queryset, params):
def search_recipes(request, queryset, params):
search_string = params.get('query', '')
search_keywords = params.getlist('keywords', [])
search_foods = params.getlist('foods', [])
@ -18,9 +19,18 @@ def search_recipes(queryset, params):
search_internal = params.get('internal', None)
search_random = params.get('random', False)
search_last_viewed = int(params.get('last_viewed', 0))
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']:
queryset = queryset.annotate(similarity=TrigramSimilarity('name', search_string), ).filter(Q(similarity__gt=0.1) | Q(name__unaccent__icontains=search_string)).order_by('-similarity')
if search_last_viewed > 0:
last_viewed_recipes = ViewLog.objects.filter(created_by=request.user, space=request.space).order_by(
'-created_at').values('recipe').distinct().all()[:search_last_viewed]
return queryset.filter(pk__in=last_viewed_recipes)
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2',
'django.db.backends.postgresql']:
queryset = queryset.annotate(similarity=TrigramSimilarity('name', search_string), ).filter(
Q(similarity__gt=0.1) | Q(name__unaccent__icontains=search_string)).order_by('-similarity')
else:
queryset = queryset.filter(name__icontains=search_string)

View File

@ -351,7 +351,7 @@ class RecipeViewSet(viewsets.ModelViewSet):
if not (share and self.detail):
self.queryset = self.queryset.filter(space=self.request.space)
self.queryset = search_recipes(self.queryset, self.request.GET)
self.queryset = search_recipes(self.request, self.queryset, self.request.GET)
return super().get_queryset()

View File

@ -138,12 +138,15 @@
<div class="col col-md-12">
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));grid-gap: 1rem;">
<template v-if="search_input === '' && search_keywords.length === 0 && search_foods.length === 0 && search_books.length === 0">
<template
v-if="search_input === '' && search_keywords.length === 0 && search_foods.length === 0 && search_books.length === 0">
<recipe-card v-bind:key="`mp_${m.id}`" v-for="m in meal_plans" :recipe="m.recipe"
:meal_plan="m"></recipe-card>
:meal_plan="m" :footer_text="m.meal_type_name" footer_icon="far fa-calendar-alt"></recipe-card>
<recipe-card v-for="r in last_viewed_recipes" v-bind:key="`rv_${r.id}`" :recipe="r" v-bind:footer_text="$t('Recently_Viewed')" footer_icon="fas fa-eye"></recipe-card>
</template>
<recipe-card v-for="r in recipes" v-bind:key="r.id" :recipe="r"></recipe-card>
<recipe-card v-for="r in recipes" v-bind:key="r.id" :recipe="r" ></recipe-card>
</div>
</div>
</div>
@ -197,6 +200,7 @@ export default {
return {
recipes: [],
meal_plans: [],
last_viewed_recipes: [],
search_input: '',
search_internal: false,
@ -260,6 +264,10 @@ export default {
}).then(result => {
this.meal_plans = result.data
})
apiClient.listRecipes({query: {last_viewed: 5}}).then(result => {
this.last_viewed_recipes = result.data.results
})
},
genericSelectChanged: function (obj) {
this[obj.var] = obj.val

View File

@ -29,8 +29,8 @@
</b-card-body>
<b-card-footer v-if="meal_plan !== undefined">
<i class="far fa-calendar-alt"></i> {{ meal_plan.meal_type_name }}
<b-card-footer v-if="footer_text !== undefined">
<i v-bind:class="footer_icon"></i> {{ footer_text }}
</b-card-footer>
</b-card>
@ -50,6 +50,8 @@ export default {
props: {
recipe: Object,
meal_plan: Object,
footer_text: String,
footer_icon: String,
},
data() {
return {

View File

@ -19,6 +19,7 @@
"New_Recipe": "New Recipe",
"Url_Import": "Url Import",
"Reset_Search": "Reset Search",
"Recently_Viewed": "Recently Viewed",
"Keywords": "Keywords",
"Books": "Books",