From 40b65c7a7b5bb1810c4175c894c189ac00152958 Mon Sep 17 00:00:00 2001 From: Evarin Date: Sun, 30 Dec 2018 00:03:31 +0100 Subject: [PATCH] =?UTF-8?q?Ordre=20des=20r=C3=A9sultats=20quand=20la=20rec?= =?UTF-8?q?herche=20est=20en=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- avisstage/views_search.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/avisstage/views_search.py b/avisstage/views_search.py index 0aea8c0..c6ea3c7 100644 --- a/avisstage/views_search.py +++ b/avisstage/views_search.py @@ -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')