Ordre des résultats quand la recherche est en cache
This commit is contained in:
parent
eb2d4bd274
commit
40b65c7a7b
1 changed files with 15 additions and 6 deletions
|
@ -6,7 +6,7 @@ from django import forms
|
|||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.cache import cache
|
||||
from django.core.paginator import Paginator
|
||||
from django.db.models import Q
|
||||
from django.db.models import Q, Case, When
|
||||
from django.http import JsonResponse, HttpResponseBadRequest
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
|
||||
|
@ -125,9 +125,9 @@ def cherche(**kwargs):
|
|||
if not use_dsl:
|
||||
kwargs['tri'] = '-date_maj'
|
||||
|
||||
if field_relevant('tri') and kwargs['tri'] != 'pertinence':
|
||||
if field_relevant('tri') and kwargs['tri'] in ['-date_maj']:
|
||||
tri = kwargs['tri']
|
||||
resultat = resultat.order_by(kwargs['tri'])
|
||||
resultat = resultat.order_by(tri)
|
||||
|
||||
return resultat, tri
|
||||
|
||||
|
@ -145,6 +145,7 @@ def recherche_resultats(request):
|
|||
tri = ''
|
||||
vue = 'vue-liste'
|
||||
lieux = []
|
||||
stageids = []
|
||||
if request.method == "GET":
|
||||
form = SearchForm(request.GET)
|
||||
if form.is_valid():
|
||||
|
@ -159,8 +160,13 @@ def recherche_resultats(request):
|
|||
if cached is None:
|
||||
# Requête effective
|
||||
stages, tri = cherche(**search_args)
|
||||
stageids = stages.values_list('id', flat=True)
|
||||
lieux = [[stageid, lieuid] for (stageid, lieuid) in stages.values_list('id', 'lieux') if lieuid is not None]
|
||||
stageids = list(stages.values_list('id', flat=True))
|
||||
print(tri)
|
||||
print(stageids[:5])
|
||||
print([k.id for k in stages[:5]])
|
||||
lieux = [[stageid, lieuid] for (stageid, lieuid)
|
||||
in stages.values_list('id', 'lieux')
|
||||
if lieuid is not None]
|
||||
|
||||
# Sauvegarde dans le cache
|
||||
to_cache = {"stages": stageids, "lieux": lieux, "tri": tri}
|
||||
|
@ -173,6 +179,7 @@ def recherche_resultats(request):
|
|||
tri = cached["tri"]
|
||||
logger.info("recherche en cache")
|
||||
|
||||
print(stageids)
|
||||
# Pagination
|
||||
paginator = Paginator(stageids, 25)
|
||||
try:
|
||||
|
@ -184,7 +191,9 @@ def recherche_resultats(request):
|
|||
stages = stages[max(0, stageids.start_index()-1):
|
||||
stageids.end_index()]
|
||||
else:
|
||||
stages = Stage.objects.filter(id__in=stageids)
|
||||
orderer = Case(*[When(pk=pk, then=pos)
|
||||
for pos, pk in enumerate(stageids)])
|
||||
stages = Stage.objects.filter(id__in=stageids).order_by(orderer)
|
||||
|
||||
stages = stages.prefetch_related('lieux', 'auteur',
|
||||
'matieres', 'thematiques')
|
||||
|
|
Loading…
Reference in a new issue