mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-07 15:18:20 -05:00
Fix after rebase
This commit is contained in:
@@ -6,9 +6,12 @@ from django.contrib.postgres.aggregates import StringAgg
|
|||||||
from django.contrib.postgres.search import (
|
from django.contrib.postgres.search import (
|
||||||
SearchQuery, SearchRank, SearchVector, TrigramSimilarity,
|
SearchQuery, SearchRank, SearchVector, TrigramSimilarity,
|
||||||
)
|
)
|
||||||
from django.db.models import Q
|
from django.db.models import Q, Case, When, Value
|
||||||
|
from django.forms import IntegerField
|
||||||
from django.utils import translation
|
from django.utils import translation
|
||||||
|
|
||||||
|
from cookbook.models import ViewLog
|
||||||
|
|
||||||
|
|
||||||
DICTIONARY = {
|
DICTIONARY = {
|
||||||
# TODO find custom dictionaries - maybe from here https://www.postgresql.org/message-id/CAF4Au4x6X_wSXFwsQYE8q5o0aQZANrvYjZJ8uOnsiHDnOVPPEg%40mail.gmail.com
|
# TODO find custom dictionaries - maybe from here https://www.postgresql.org/message-id/CAF4Au4x6X_wSXFwsQYE8q5o0aQZANrvYjZJ8uOnsiHDnOVPPEg%40mail.gmail.com
|
||||||
@@ -25,8 +28,8 @@ DICTIONARY = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def search_recipes(queryset, params):
|
def search_recipes(request, queryset, params):
|
||||||
search_string = params.get('query', '').strip()
|
search_string = params.get('query', '')
|
||||||
search_keywords = params.getlist('keywords', [])
|
search_keywords = params.getlist('keywords', [])
|
||||||
search_foods = params.getlist('foods', [])
|
search_foods = params.getlist('foods', [])
|
||||||
search_books = params.getlist('books', [])
|
search_books = params.getlist('books', [])
|
||||||
@@ -39,6 +42,18 @@ def search_recipes(queryset, params):
|
|||||||
search_random = params.get('random', False)
|
search_random = params.get('random', False)
|
||||||
search_last_viewed = int(params.get('last_viewed', 0))
|
search_last_viewed = int(params.get('last_viewed', 0))
|
||||||
|
|
||||||
|
if search_last_viewed > 0:
|
||||||
|
last_viewed_recipes = ViewLog.objects.filter(
|
||||||
|
created_by=request.user, space=request.space,
|
||||||
|
created_at__gte=datetime.now() - timedelta(days=14)).values_list(
|
||||||
|
'recipe__pk', flat=True).distinct()
|
||||||
|
return queryset.filter(pk__in=list(set(last_viewed_recipes))[-search_last_viewed:])
|
||||||
|
|
||||||
|
queryset = queryset.annotate(
|
||||||
|
new_recipe=Case(When(
|
||||||
|
created_at__gte=(datetime.now() - timedelta(days=7)), then=Value(100)),
|
||||||
|
default=Value(0), )).order_by('-new_recipe', 'name')
|
||||||
|
|
||||||
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql'] and search_string != '':
|
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql'] and search_string != '':
|
||||||
# queryset = queryset.annotate(similarity=TrigramSimilarity('name', search_string), )
|
# queryset = queryset.annotate(similarity=TrigramSimilarity('name', search_string), )
|
||||||
# .filter(Q(similarity__gt=0.1) | Q(name__unaccent__icontains=search_string)).order_by('-similarity')
|
# .filter(Q(similarity__gt=0.1) | Q(name__unaccent__icontains=search_string)).order_by('-similarity')
|
||||||
@@ -79,6 +94,13 @@ def search_recipes(queryset, params):
|
|||||||
| Q(name__istartswith=search_string)
|
| Q(name__istartswith=search_string)
|
||||||
)
|
)
|
||||||
.order_by('-rank'))
|
.order_by('-rank'))
|
||||||
|
else:
|
||||||
|
queryset = queryset.filter(name__icontains=search_string)
|
||||||
|
|
||||||
|
if len(search_keywords) > 0:
|
||||||
|
if search_keywords_or == 'true':
|
||||||
|
queryset = queryset.filter(keywords__id__in=search_keywords)
|
||||||
|
else:
|
||||||
for k in search_keywords:
|
for k in search_keywords:
|
||||||
queryset = queryset.filter(keywords__id=k)
|
queryset = queryset.filter(keywords__id=k)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user