sas: add pagination on AlbumView

This commit is contained in:
Antoine Bartuccio 2019-09-16 01:26:20 +02:00
parent 8330e1eaf2
commit b8ad2d4835
Signed by: klmp200
GPG Key ID: E7245548C53F904B
4 changed files with 33 additions and 11 deletions

View File

@ -114,6 +114,7 @@
{% endmacro %} {% endmacro %}
{% macro paginate(page_obj, paginator) %} {% macro paginate(page_obj, paginator) %}
{% if page_obj.has_previous() or page_obj.has_next() %}
{% if page_obj.has_previous() %} {% if page_obj.has_previous() %}
<a href="?page={{ page_obj.previous_page_number() }}">{% trans %}Previous{% endtrans %}</a> <a href="?page={{ page_obj.previous_page_number() }}">{% trans %}Previous{% endtrans %}</a>
{% else %} {% else %}
@ -131,6 +132,7 @@
{% else %} {% else %}
<span class="disabled">{% trans %}Next{% endtrans %}</span> <span class="disabled">{% trans %}Next{% endtrans %}</span>
{% endif %} {% endif %}
{% endif %}
{% endmacro %} {% endmacro %}
{% macro select_all_checkbox(form_id) %} {% macro select_all_checkbox(form_id) %}

View File

@ -1,4 +1,5 @@
{% extends "core/base.jinja" %} {% extends "core/base.jinja" %}
{% from "core/macros.jinja" import paginate %}
{% block title %} {% block title %}
{% trans %}SAS{% endtrans %} {% trans %}SAS{% endtrans %}
@ -62,7 +63,7 @@
{% endfor %} {% endfor %}
</div> </div>
<div> <div>
{% for p in album.children_pictures.order_by('id') %} {% for p in pictures %}
<div style="display: inline-block;"> <div style="display: inline-block;">
{% if edit_mode %} {% if edit_mode %}
<input type="checkbox" name="file_list" value="{{ p.id }}"> <input type="checkbox" name="file_list" value="{{ p.id }}">
@ -77,6 +78,8 @@
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<br>
{{ paginate(pictures, paginator) }}
{% if edit_mode %} {% if edit_mode %}
</form> </form>
{% endif %} {% endif %}

View File

@ -23,7 +23,7 @@
# #
from django.shortcuts import redirect from django.shortcuts import redirect
from django.http import HttpResponse from django.http import HttpResponse, Http404
from django.core.urlresolvers import reverse_lazy, reverse from django.core.urlresolvers import reverse_lazy, reverse
from core.views.forms import SelectDate from core.views.forms import SelectDate
from django.views.generic import DetailView, TemplateView from django.views.generic import DetailView, TemplateView
@ -32,6 +32,7 @@ from django.utils.translation import ugettext_lazy as _
from django.conf import settings from django.conf import settings
from django import forms from django import forms
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core.paginator import Paginator, InvalidPage
from ajax_select import make_ajax_field from ajax_select import make_ajax_field
from ajax_select.fields import AutoCompleteSelectMultipleField from ajax_select.fields import AutoCompleteSelectMultipleField
@ -252,6 +253,14 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
form_class = SASForm form_class = SASForm
pk_url_kwarg = "album_id" pk_url_kwarg = "album_id"
template_name = "sas/album.jinja" template_name = "sas/album.jinja"
paginate_by = settings.SITH_SAS_IMAGES_PER_PAGE
def dispatch(self, request, *args, **kwargs):
try:
self.asked_page = int(request.GET.get("page", 1))
except ValueError:
raise Http404
return super(AlbumView, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.form = self.get_form() self.form = self.get_form()
@ -291,6 +300,13 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
kwargs = super(AlbumView, self).get_context_data(**kwargs) kwargs = super(AlbumView, self).get_context_data(**kwargs)
kwargs["paginator"] = Paginator(
self.object.children_pictures.order_by("id"), self.paginate_by
)
try:
kwargs["pictures"] = kwargs["paginator"].page(self.asked_page)
except InvalidPage:
raise Http404
kwargs["form"] = self.form kwargs["form"] = self.form
kwargs["clipboard"] = SithFile.objects.filter( kwargs["clipboard"] = SithFile.objects.filter(
id__in=self.request.session["clipboard"] id__in=self.request.session["clipboard"]

View File

@ -338,6 +338,7 @@ SITH_FORUM_PAGE_LENGTH = 30
# SAS variables # SAS variables
SITH_SAS_ROOT_DIR_ID = 4 SITH_SAS_ROOT_DIR_ID = 4
SITH_SAS_IMAGES_PER_PAGE = 30
SITH_BOARD_SUFFIX = "-bureau" SITH_BOARD_SUFFIX = "-bureau"
SITH_MEMBER_SUFFIX = "-membres" SITH_MEMBER_SUFFIX = "-membres"