From 49916a7498d4182734b50970edee0aa8b7231af7 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 20 May 2021 09:43:29 -0700 Subject: [PATCH] Sorting for announcement headers and pagination --- bookwyrm/templates/book/book.html | 4 +-- .../templates/settings/announcements.html | 7 +++++- bookwyrm/views/announcements.py | 25 ++++++++++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/bookwyrm/templates/book/book.html b/bookwyrm/templates/book/book.html index ff027a3d..fb0a4510 100644 --- a/bookwyrm/templates/book/book.html +++ b/bookwyrm/templates/book/book.html @@ -1,7 +1,5 @@ {% extends 'layout.html' %} -{% load i18n %} -{% load bookwyrm_tags %} -{% load humanize %} +{% load i18n %}{% load bookwyrm_tags %}{% load humanize %}{% load utilities %} {% block title %}{{ book.title }}{% endblock %} diff --git a/bookwyrm/templates/settings/announcements.html b/bookwyrm/templates/settings/announcements.html index d1396179..52ede3da 100644 --- a/bookwyrm/templates/settings/announcements.html +++ b/bookwyrm/templates/settings/announcements.html @@ -17,7 +17,11 @@ - {% url 'settings-announcements' as url %} + {% for announcement in announcements %} + diff --git a/bookwyrm/views/announcements.py b/bookwyrm/views/announcements.py index 8ea5952c..95ba5ed5 100644 --- a/bookwyrm/views/announcements.py +++ b/bookwyrm/views/announcements.py @@ -1,14 +1,16 @@ """ make announcements """ from django.contrib.auth.decorators import login_required, permission_required +from django.core.paginator import Paginator from django.shortcuts import get_object_or_404 from django.template.response import TemplateResponse from django.utils.decorators import method_decorator from django.views import View from bookwyrm import forms, models +from bookwyrm.settings import PAGE_LENGTH -# pylint: disable= no-self-use +# pylint: disable=no-self-use @method_decorator(login_required, name="dispatch") @method_decorator( permission_required("bookwyrm.edit_instance_settings", raise_exception=True), @@ -19,9 +21,24 @@ class Announcements(View): def get(self, request): """view and create announcements""" + announcements = models.Announcement.objects + + sort = request.GET.get("sort", "-created_date") + sort_fields = [ + "created_date", + "preview", + "start_date", + "end_date", + "active", + ] + if sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]: + announcements = announcements.order_by(sort) data = { - "announcements": models.Announcement.objects.all(), + "announcements": Paginator(announcements, PAGE_LENGTH).get_page( + request.GET.get("page") + ), "form": forms.AnnouncementForm(), + "sort": sort, } return TemplateResponse(request, "settings/announcements.html", data) @@ -33,7 +50,9 @@ class Announcements(View): # reset the create form form = forms.AnnouncementForm() data = { - "announcements": models.Announcement.objects.all(), + "announcements": Paginator( + models.Announcement.objects, PAGE_LENGTH + ).get_page(request.GET.get("page")), "form": form, } return TemplateResponse(request, "settings/announcements.html", data)
+ {% url 'settings-announcements' as url %} + {% trans "Date added" as text %} + {% include 'snippets/table-sort-header.html' with field="created_date" sort=sort text=text %} + {% trans "Preview" as text %} {% include 'snippets/table-sort-header.html' with field="preview" sort=sort text=text %} @@ -37,6 +41,7 @@
{{ announcement.created_date|naturalday }} {{ announcement.preview }} {{ announcement.start_date|naturaltime|default:'' }} {{ announcement.end_date|naturaltime|default:'' }}