Replaced the CashReportView list view with the cash view function, to simplify. Moved the pagination query parameter parser from the view to Pagination.

This commit is contained in:
2020-07-11 17:23:38 +08:00
parent 2f53bcfd43
commit fa7416d0f3
3 changed files with 110 additions and 158 deletions

View File

@ -18,24 +18,20 @@
"""The view controllers of the accounting application.
"""
from datetime import date
from django.http import HttpResponseRedirect, HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.utils import dateformat
from django.utils.decorators import method_decorator
from django.utils.timezone import localdate
from django.utils.translation import get_language, pgettext
from django.views import generic
from django.views.decorators.http import require_GET
from accounting.models import Record, Transaction, Subject
from mia_core.period import Period
from mia import settings
from mia_core.digest_auth import digest_login_required
from mia_core.utils import UrlBuilder, Pagination, \
PageNoOutOfRangeException
from mia_core.period import Period
from mia_core.utils import Pagination
@require_GET
@ -65,101 +61,24 @@ def cash_home(request):
reverse("accounting:cash", args=(subject_code, period_spec)))
@method_decorator(digest_login_required, name='dispatch')
class BaseReportView(generic.ListView):
"""A base account report.
Attributes:
page_no (int): The specified page number
page_size (int): The specified page size
period (Period): The template period helper
subject (Subject): The currently-specified subject
"""
page_no = None
page_size = None
pagination = None
period = None
subject = None
def get(self, request, *args, **kwargs):
"""Adds object_list to the context.
Args:
request (HttpRequest): The request.
args (list): The remaining arguments.
kwargs (dict): The keyword arguments.
Returns:
The response
"""
if request.user.is_anonymous:
return HttpResponse(status=401)
try:
self.page_size = int(request.GET["page-size"])
if self.page_size < 1:
return HttpResponseRedirect(
str(UrlBuilder(request.get_full_path())
.del_param("page-size")))
except KeyError:
self.page_size = None
except ValueError:
return HttpResponseRedirect(
str(UrlBuilder(request.get_full_path())
.del_param("page-size")))
try:
self.page_no = int(request.GET["page"])
if self.page_no < 1:
return HttpResponseRedirect(
str(UrlBuilder(request.get_full_path())
.del_param("page")))
except KeyError:
self.page_no = None
except ValueError:
return HttpResponseRedirect(
str(UrlBuilder(request.get_full_path())
.del_param("page")))
try:
r = super(BaseReportView, self) \
.get(request, *args, **kwargs)
except PageNoOutOfRangeException:
return HttpResponseRedirect(
str(UrlBuilder(request.get_full_path())
.del_param("page")))
return r
def get_context_data(self, **kwargs):
data = super(BaseReportView, self).get_context_data(**kwargs)
data["period"] = self.period
data["subject"] = self.subject
data["pagination"] = self.pagination
return data
class CashReportView(BaseReportView):
"""The accounting cash report."""
http_method_names = ["get"]
template_name = "accounting/cash.html"
context_object_name = "records"
def get_queryset(self):
"""Return the accounting records for the cash report.
Returns:
List[Record]: The accounting records for the cash report
"""
first_txn = Transaction.objects.order_by("date").first()
data_start = first_txn.date if first_txn is not None else None
last_txn = Transaction.objects.order_by("-date").first()
data_end = last_txn.date if last_txn is not None else None
self.period = Period(
get_language(), data_start, data_end,
self.kwargs["period_spec"])
if self.kwargs["subject_code"] == "0":
self.subject = Subject(code="0")
self.subject.title_zhtw = pgettext(
"Accounting|", "Current assets and liabilities")
records = Record.objects.raw(
"""SELECT r.*
@require_GET
@digest_login_required
def cash(request, subject_code, period_spec):
"""The cash account report."""
first_txn = Transaction.objects.order_by("date").first()
data_start = first_txn.date if first_txn is not None else None
last_txn = Transaction.objects.order_by("-date").first()
data_end = last_txn.date if last_txn is not None else None
period = Period(
get_language(), data_start, data_end,
period_spec)
# The list data
if subject_code == "0":
subject = Subject(code="0")
subject.title_zhtw = pgettext(
"Accounting|", "Current Assets And Liabilities")
records = Record.objects.raw(
"""SELECT r.*
FROM accounting_records AS r
INNER JOIN (SELECT
t1.sn AS sn,
@ -188,12 +107,11 @@ ORDER BY
t.ord,
CASE WHEN is_credit THEN 1 ELSE 2 END,
r.ord""",
[self.period.start, self.period.end])
else:
self.subject = Subject.objects.filter(
code=self.kwargs["subject_code"]).first()
records = Record.objects.raw(
"""SELECT r.*
[period.start, period.end])
else:
subject = Subject.objects.filter(code=subject_code).first()
records = Record.objects.raw(
"""SELECT r.*
FROM accounting_records AS r
INNER JOIN (SELECT
t1.sn AS sn,
@ -216,11 +134,14 @@ ORDER BY
t.ord,
CASE WHEN is_credit THEN 1 ELSE 2 END,
r.ord""",
[self.period.start,
self.period.end,
self.subject.code + "%",
self.subject.code + "%"])
self.pagination = Pagination(
self.request.get_full_path(), records,
self.page_no, self.page_size, True)
return self.pagination.records
[period.start,
period.end,
subject.code + "%",
subject.code + "%"])
pagination = Pagination(request, records, True)
return render(request, "accounting/cash.html", {
"records": pagination.records,
"pagination": pagination,
"subject": subject,
"period": period,
})