From a41ed5fe085e1dd6928500e37d8068347b2c998f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Thu, 21 Sep 2017 09:39:27 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20rapidit=C3=A9,=20fait=20le=20tri=20(boucl?= =?UTF-8?q?e=20for)=20apr=C3=A8s=20le=20paginator=20pour=20pas=20d=C3=A9pa?= =?UTF-8?q?sser=2020-25=20elts=20dans=20la=20boucle=20for?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logs/views.py | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/logs/views.py b/logs/views.py index 7e88237d..17b92947 100644 --- a/logs/views.py +++ b/logs/views.py @@ -23,6 +23,9 @@ # App de gestion des statistiques pour re2o # Gabriel Détraz # Gplv2 + +from __future__ import unicode_literals + from django.http import HttpResponse from django.shortcuts import render, redirect from django.shortcuts import get_object_or_404 @@ -76,30 +79,39 @@ def index(request): # Select only wanted versions versions = Version.objects.filter(content_type__in=ContentType.objects.filter(model__in=content_type_filter)).order_by('revision__date_created').reverse().select_related('revision') - # Setup nice struct for template - versions_list = [] - for v in versions : - if v.object : - versions_list.append( - {'rev_id' : v.revision.id, - 'comment': v.revision.comment, - 'datetime': v.revision.date_created.strftime('%d/%m/%y %H:%M:%S'), - 'username': v.revision.user.get_username() if v.revision.user else '?', - 'user_id': v.revision.user_id, - 'version': v } - ) - - paginator = Paginator(versions_list, pagination_number) + paginator = Paginator(versions, pagination_number) page = request.GET.get('page') try: - versions_list = paginator.page(page) + versions = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. - versions_list = paginator.page(1) + versions = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. - versions_list = paginator.page(paginator.num_pages) - return render(request, 'logs/index.html', {'versions_list': versions_list}) + versions = paginator.page(paginator.num_pages) + + # Force to have a list instead of QuerySet + versions.count(0) + # Items to remove later because invalid + to_remove = [] + # Parse every item (max = pagination_number) + for i in range( len( versions.object_list ) ): + if versions.object_list[i].object : + v = versions.object_list[i] + versions.object_list[i] = { + 'rev_id' : v.revision.id, + 'comment': v.revision.comment, + 'datetime': v.revision.date_created.strftime('%d/%m/%y %H:%M:%S'), + 'username': v.revision.user.get_username() if v.revision.user else '?', + 'user_id': v.revision.user_id, + 'version': v } + else : + to_remove.append(i) + # Remove all tagged invalid items + for i in to_remove : + versions.object_list.pop(i) + + return render(request, 'logs/index.html', {'versions_list': versions}) @login_required @permission_required('cableur')