Create `ExtendedPaginationMixin`.

This commit is contained in:
Santiago Lamora 2019-10-31 17:16:51 +01:00
parent d288770c25
commit 054b7e92ac
2 changed files with 28 additions and 21 deletions

View File

@ -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."""

View File

@ -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"