From 054b7e92ac1d43560a3056805027ff3a35ac4516 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 31 Oct 2019 17:16:51 +0100 Subject: [PATCH] Create `ExtendedPaginationMixin`. --- musician/mixins.py | 21 +++++++++++++++++++++ musician/views.py | 28 +++++++--------------------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/musician/mixins.py b/musician/mixins.py index cf58bff..681141a 100644 --- a/musician/mixins.py +++ b/musician/mixins.py @@ -24,6 +24,27 @@ class CustomContextMixin(ContextMixin): return context +class ExtendedPaginationMixin: + paginate_by = 20 + paginate_by_kwarg = 'per_page' + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'per_page_values': [5, 10, 20, 50], + 'per_page_param': self.paginate_by_kwarg, + }) + return context + + def get_paginate_by(self, queryset): + per_page = self.request.GET.get(self.paginate_by_kwarg) or self.paginate_by + try: + paginate_by = int(per_page) + except ValueError: + paginate_by = self.paginate_by + return paginate_by + + class UserTokenRequiredMixin(UserPassesTestMixin): def test_func(self): """Check that the user has an authorized token.""" diff --git a/musician/views.py b/musician/views.py index ef45234..240e287 100644 --- a/musician/views.py +++ b/musician/views.py @@ -11,7 +11,7 @@ from . import api, get_version from .auth import login as auth_login from .auth import logout as auth_logout from .forms import LoginForm -from .mixins import CustomContextMixin, UserTokenRequiredMixin +from .mixins import CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView): @@ -30,35 +30,21 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView): return context +class ServiceListView(CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin, ListView): + """Base list view to all services""" + pass + + class MailView(CustomContextMixin, UserTokenRequiredMixin, TemplateView): template_name = "musician/mail.html" -class MailingListsView(CustomContextMixin, UserTokenRequiredMixin, ListView): +class MailingListsView(ServiceListView): template_name = "musician/mailinglists.html" - paginate_by = 20 - paginate_by_kwarg = 'per_page' def get_queryset(self): return self.orchestra.retrieve_service_list('mailinglist') - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context.update({ - 'page_param': self.page_kwarg, - 'per_page_values': [5, 10, 20, 50], - 'per_page_param': self.paginate_by_kwarg, - }) - return context - - def get_paginate_by(self, queryset): - per_page = self.request.GET.get(self.paginate_by_kwarg) or self.paginate_by - try: - paginate_by = int(per_page) - except ValueError: - paginate_by = self.paginate_by - return paginate_by - class DatabasesView(CustomContextMixin, UserTokenRequiredMixin, TemplateView): template_name = "musician/databases.html"