From c6adb87e4b83f7bec93adc5f5804ef765d6c096a Mon Sep 17 00:00:00 2001 From: detraz Date: Thu, 29 Nov 2018 18:22:08 +0100 Subject: [PATCH] Pagination, fix intelligence dans les models plutot que views --- printer/models.py | 7 ++++- printer/templates/printer/aff_digicodes.html | 6 ++-- printer/templates/printer/aff_jobs.html | 8 ++--- printer/templates/printer/sidebar.html | 13 ++++---- printer/views.py | 32 +++++++++++++------- re2o/base.py | 13 ++++++++ 6 files changed, 53 insertions(+), 26 deletions(-) diff --git a/printer/models.py b/printer/models.py index bbc9254f..b2553ea1 100644 --- a/printer/models.py +++ b/printer/models.py @@ -8,7 +8,7 @@ Author : Maxime Bombar . from __future__ import unicode_literals from numpy.random import randint -import unidecode +import unidecode, datetime from django.core.files.storage import FileSystemStorage from django.core.exceptions import ObjectDoesNotExist @@ -16,6 +16,7 @@ from django.core.exceptions import ObjectDoesNotExist from django.db import models from django.forms import ValidationError from django.utils.translation import ugettext_lazy as _ +from django.utils import timezone from django.template.defaultfilters import filesizeformat from re2o.mixins import RevMixin, AclMixin @@ -62,6 +63,10 @@ class Digicode(RevMixin, models.Model, AclMixin, FieldPermissionModelMixin): created = models.DateTimeField(auto_now_add=True) used_time = models.DateTimeField(null=True) + @classmethod + def active_codes(cls): + return cls.objects.filter(created__gte = timezone.now() - datetime.timedelta(days=3)) + def _gen_code(user): try_again = True while try_again: diff --git a/printer/templates/printer/aff_digicodes.html b/printer/templates/printer/aff_digicodes.html index a2202244..b7ecb5cd 100644 --- a/printer/templates/printer/aff_digicodes.html +++ b/printer/templates/printer/aff_digicodes.html @@ -33,9 +33,9 @@ with this program; if not, write to the Free Software Foundation, Inc., - - - + + + {% for digicode in digicodes_list %} diff --git a/printer/templates/printer/aff_jobs.html b/printer/templates/printer/aff_jobs.html index 197ef8df..41a3b587 100644 --- a/printer/templates/printer/aff_jobs.html +++ b/printer/templates/printer/aff_jobs.html @@ -32,10 +32,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% trans "User" %}{% trans "Code" %}{% trans "Created on" %}{% include "buttons/sort.html" with prefix='code' col='user' text=tr_user %}{% include "buttons/sort.html" with prefix='code' col='code' text=tr_code %}{% include "buttons/sort.html" with prefix='code' col='created' text=tr_created_on %}
- - - - + + + + diff --git a/printer/templates/printer/sidebar.html b/printer/templates/printer/sidebar.html index 2ae58f87..d4128469 100644 --- a/printer/templates/printer/sidebar.html +++ b/printer/templates/printer/sidebar.html @@ -29,6 +29,12 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "Print document" %} + {% can_create digicode %} + + + {% trans "Create Digicode" %} + + {% acl_end %} {% trans "List of jobs" %} @@ -37,11 +43,4 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "List of digicodes" %} - {% can_create digicode %} - - - {% trans "Create Digicode" %} - - {% acl_end %} - {% endblock %} diff --git a/printer/views.py b/printer/views.py index ee9052b9..b8865744 100644 --- a/printer/views.py +++ b/printer/views.py @@ -6,8 +6,6 @@ Author : Maxime Bombar . from __future__ import unicode_literals -import datetime - from django.urls import reverse from django.shortcuts import render, redirect from django.forms import modelformset_factory, formset_factory @@ -17,7 +15,10 @@ from django.utils.translation import ugettext as _ from re2o.views import form from users.models import User -from re2o.base import re2o_paginator +from re2o.base import ( + re2o_paginator, + SortTable +) from . import settings from .utils import pdfinfo, send_mail_printer, printer_enabled @@ -225,9 +226,14 @@ def index_jobs(request): """ Display jobs""" pagination_number = GeneralOption.get_cached_value('pagination_number') jobs = JobWithOptions.objects.select_related('user')\ - .select_related('print_operation')\ - .order_by('starttime').reverse() - jobs_list = re2o_paginator(request, jobs, pagination_number) + .select_related('print_operation') + jobs_list = SortTable.sort( + jobs, + request.GET.get('col'), + request.GET.get('order'), + SortTable.PRINTER_INDEX_JOB + ) + jobs_list = re2o_paginator(request, jobs_list, pagination_number) return render(request, 'printer/index_jobs.html', {'jobs_list': jobs_list}) @@ -235,21 +241,25 @@ def index_jobs(request): def index_digicodes(request): """Display available digicodes""" pagination_number = GeneralOption.get_cached_value('pagination_number') - digicodes = Digicode.objects.filter(created__gte=(datetime.datetime.now() - -datetime.timedelta(3))) - digicodes_list = re2o_paginator(request, digicodes, pagination_number) + digicodes = Digicode.active_codes() + digicodes_list = SortTable.sort( + digicodes, + request.GET.get('col'), + request.GET.get('order'), + SortTable.PRINTER_INDEX_CODE + ) + digicodes_list = re2o_paginator(request, digicodes_list, pagination_number) return render(request, 'printer/index_digicodes.html', {'digicodes_list': digicodes_list}, ) -@can_create(Digicode) @login_required +@can_create(Digicode) def create_code(request): """Generate a digicode""" code = CreateCodeForm( request.POST or None, - # form_kwargs={'user': request.user}, user = request.user, ) if code.is_valid(): diff --git a/re2o/base.py b/re2o/base.py index 023a16ff..1a51fd40 100644 --- a/re2o/base.py +++ b/re2o/base.py @@ -234,6 +234,19 @@ class SortTable: 'logs_date': ['date_created'], 'default': ['-date_created'] } + PRINTER_INDEX_CODE = { + 'code_code': ['code'], + 'code_user': ['user__pseudo'], + 'code_created': ['created'], + 'default': ['-created'] + } + PRINTER_INDEX_JOB = { + 'job_filename': ['filename'], + 'job_user': ['user__pseudo'], + 'job_starttime': ['starttime'], + 'job_status': ['status'], + 'default': ['-starttime'] + } @staticmethod def sort(request, col, order, values):
{% trans "File Name" %}{% trans "By user" %}{% trans "Start at" %}{% trans "Status" %}{% include "buttons/sort.html" with prefix='job' col='filename' text=tr_filename %}{% include "buttons/sort.html" with prefix='job' col='user' text=tr_byuser %}{% include "buttons/sort.html" with prefix='job' col='starttime' text=tr_startat %}{% include "buttons/sort.html" with prefix='status' col='status' text=tr_status %} {% trans "Number" %} {% trans "Price" %} {% trans "Print again" %}