Moved part of the pagination logic from the view to Pagination.

This commit is contained in:
依瑪貓 2020-07-07 21:04:07 +08:00
parent df0380362b
commit 897f82bd26
2 changed files with 13 additions and 15 deletions

View File

@ -72,6 +72,7 @@ class BaseReportView(generic.ListView):
""" """
page_no = None page_no = None
page_size = None page_size = None
pagination = None
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
"""Adds object_list to the context. """Adds object_list to the context.
@ -110,7 +111,6 @@ class BaseReportView(generic.ListView):
return HttpResponseRedirect( return HttpResponseRedirect(
str(UrlBuilder(request.get_full_path()) str(UrlBuilder(request.get_full_path())
.del_param("page"))) .del_param("page")))
print("accounting.views.BaseReportView.get() 1 before")
try: try:
r = super(BaseReportView, self) \ r = super(BaseReportView, self) \
.get(request, *args, **kwargs) .get(request, *args, **kwargs)
@ -118,15 +118,12 @@ class BaseReportView(generic.ListView):
return HttpResponseRedirect( return HttpResponseRedirect(
str(UrlBuilder(request.get_full_path()) str(UrlBuilder(request.get_full_path())
.del_param("page"))) .del_param("page")))
print("accounting.views.BaseReportView.get() 2 after")
return r return r
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
print("accounting.views.BaseReportView.get_context_data() 1 before") data = super(BaseReportView, self).get_context_data(**kwargs)
r = super(BaseReportView, self).get_context_data(**kwargs) data["pagination"] = self.pagination
print("accounting.views.BaseReportView.get_context_data() 2 after") return data
print(r)
return r
class CashReportView(BaseReportView): class CashReportView(BaseReportView):
@ -141,7 +138,6 @@ class CashReportView(BaseReportView):
Returns: Returns:
List[Record]: The accounting records for the cash report List[Record]: The accounting records for the cash report
""" """
print("accounting.views.CashReportView.get_queryset() 1 before")
period = PeriodParser(self.kwargs["period_spec"]) period = PeriodParser(self.kwargs["period_spec"])
if self.kwargs["subject_code"] == "0": if self.kwargs["subject_code"] == "0":
records = Record.objects.raw( records = Record.objects.raw(
@ -204,8 +200,6 @@ ORDER BY
period.end, period.end,
self.kwargs["subject_code"] + "%", self.kwargs["subject_code"] + "%",
self.kwargs["subject_code"] + "%"]) self.kwargs["subject_code"] + "%"])
pagination = Pagination( self.pagination = Pagination(
len(records), self.page_no, self.page_size, True) records, self.page_no, self.page_size, True)
start_no = pagination.page_size * (pagination.page_no - 1) return self.pagination.records
print("accounting.views.CashReportView.get_queryset() 2 after")
return records[start_no:start_no + pagination.page_size]

View File

@ -144,17 +144,19 @@ class Pagination:
Attributes: Attributes:
page_no (int): The current page number page_no (int): The current page number
page_size (int): The page size page_size (int): The page size
records (Model): The records in the current page
""" """
page_no = None page_no = None
page_size = None page_size = None
records = None
DEFAULT_PAGE_SIZE = 10 DEFAULT_PAGE_SIZE = 10
def __init__(self, count, page_no, page_size, is_reverse=False): def __init__(self, records, page_no, page_size, is_reverse=False):
self.page_size = page_size \ self.page_size = page_size \
if page_size is not None \ if page_size is not None \
else self.DEFAULT_PAGE_SIZE else self.DEFAULT_PAGE_SIZE
total_pages = int((count - 1) / self.page_size) + 1 total_pages = int((len(records) - 1) / self.page_size) + 1
default_page = 1 if not is_reverse else total_pages default_page = 1 if not is_reverse else total_pages
if page_no == default_page: if page_no == default_page:
raise PageNoOutOfRangeException() raise PageNoOutOfRangeException()
@ -163,6 +165,8 @@ class Pagination:
else default_page else default_page
if self.page_no > total_pages: if self.page_no > total_pages:
raise PageNoOutOfRangeException() raise PageNoOutOfRangeException()
start_no = self.page_size * (self.page_no - 1)
self.records = records[start_no:start_no + self.page_size]
class PageNoOutOfRangeException(Exception): class PageNoOutOfRangeException(Exception):