Ordre des résultats quand la recherche est en cache

This commit is contained in:
Evarin 2018-12-30 00:03:31 +01:00
parent eb2d4bd274
commit 40b65c7a7b

View file

@ -6,7 +6,7 @@ from django import forms
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.cache import cache from django.core.cache import cache
from django.core.paginator import Paginator 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.http import JsonResponse, HttpResponseBadRequest
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
@ -125,9 +125,9 @@ def cherche(**kwargs):
if not use_dsl: if not use_dsl:
kwargs['tri'] = '-date_maj' 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'] tri = kwargs['tri']
resultat = resultat.order_by(kwargs['tri']) resultat = resultat.order_by(tri)
return resultat, tri return resultat, tri
@ -145,6 +145,7 @@ def recherche_resultats(request):
tri = '' tri = ''
vue = 'vue-liste' vue = 'vue-liste'
lieux = [] lieux = []
stageids = []
if request.method == "GET": if request.method == "GET":
form = SearchForm(request.GET) form = SearchForm(request.GET)
if form.is_valid(): if form.is_valid():
@ -159,8 +160,13 @@ def recherche_resultats(request):
if cached is None: if cached is None:
# Requête effective # Requête effective
stages, tri = cherche(**search_args) stages, tri = cherche(**search_args)
stageids = stages.values_list('id', flat=True) stageids = list(stages.values_list('id', flat=True))
lieux = [[stageid, lieuid] for (stageid, lieuid) in stages.values_list('id', 'lieux') if lieuid is not None] 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 # Sauvegarde dans le cache
to_cache = {"stages": stageids, "lieux": lieux, "tri": tri} to_cache = {"stages": stageids, "lieux": lieux, "tri": tri}
@ -173,6 +179,7 @@ def recherche_resultats(request):
tri = cached["tri"] tri = cached["tri"]
logger.info("recherche en cache") logger.info("recherche en cache")
print(stageids)
# Pagination # Pagination
paginator = Paginator(stageids, 25) paginator = Paginator(stageids, 25)
try: try:
@ -184,7 +191,9 @@ def recherche_resultats(request):
stages = stages[max(0, stageids.start_index()-1): stages = stages[max(0, stageids.start_index()-1):
stageids.end_index()] stageids.end_index()]
else: 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', stages = stages.prefetch_related('lieux', 'auteur',
'matieres', 'thematiques') 'matieres', 'thematiques')