From e64089f06fc7196f9cb1ed910068e4a1a3521a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Wed, 29 Jul 2020 19:08:10 +0800 Subject: [PATCH] Added the default_spec() method to Period, and applied it to the views in the accounting application. --- accounting/views.py | 19 ++++++------------- mia_core/period.py | 9 +++++++++ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/accounting/views.py b/accounting/views.py index 19edb4a..1af536e 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -27,7 +27,6 @@ from django.db.models.functions import TruncMonth, Coalesce from django.http import HttpResponseRedirect from django.shortcuts import render from django.urls import reverse -from django.utils import dateformat, timezone from django.utils.translation import pgettext, gettext_noop from django.views.decorators.http import require_GET, require_POST @@ -72,9 +71,8 @@ def cash_default(request): """ account = Account.objects.get( code=settings.ACCOUNTING["DEFAULT_CASH_ACCOUNT"]) - period_spec = dateformat.format(timezone.localdate(), "Y-m") return HttpResponseRedirect( - reverse("accounting:cash", args=(account, period_spec))) + reverse("accounting:cash", args=(account, Period.default_spec()))) @require_GET @@ -296,9 +294,8 @@ def ledger_default(request): """ account = Account.objects.get( code=settings.ACCOUNTING["DEFAULT_LEDGER_ACCOUNT"]) - period_spec = dateformat.format(timezone.localdate(), "Y-m") return HttpResponseRedirect( - reverse("accounting:ledger", args=(account, period_spec))) + reverse("accounting:ledger", args=(account, Period.default_spec()))) @require_GET @@ -441,9 +438,8 @@ def journal_default(request): Returns: HttpResponseRedirect: The redirection to the default month. """ - period_spec = dateformat.format(timezone.localdate(), "Y-m") return HttpResponseRedirect( - reverse("accounting:journal", args=(period_spec,))) + reverse("accounting:journal", args=(Period.default_spec(),))) @require_GET @@ -528,9 +524,8 @@ def trial_balance_default(request): Returns: HttpResponseRedirect: The redirection to the default month. """ - period_spec = dateformat.format(timezone.localdate(), "Y-m") return HttpResponseRedirect( - reverse("accounting:trial-balance", args=(period_spec,))) + reverse("accounting:trial-balance", args=(Period.default_spec(),))) @require_GET @@ -637,9 +632,8 @@ def income_statement_default(request): Returns: HttpResponseRedirect: The redirection to the default month. """ - period_spec = dateformat.format(timezone.localdate(), "Y-m") return HttpResponseRedirect( - reverse("accounting:income-statement", args=(period_spec,))) + reverse("accounting:income-statement", args=(Period.default_spec(),))) @require_GET @@ -722,9 +716,8 @@ def balance_sheet_default(request): Returns: HttpResponseRedirect: The redirection to the default month. """ - period_spec = dateformat.format(timezone.localdate(), "Y-m") return HttpResponseRedirect( - reverse("accounting:balance-sheet", args=(period_spec,))) + reverse("accounting:balance-sheet", args=(Period.default_spec(),))) @require_GET diff --git a/mia_core/period.py b/mia_core/period.py index 8960cfd..5646c74 100644 --- a/mia_core/period.py +++ b/mia_core/period.py @@ -314,6 +314,15 @@ class Period: "defaultDate": self.chosen_month, }) + @staticmethod + def default_spec(): + """Returns the specification for the default period. + + Returns: + str: The specification for the default period + """ + return dateformat.format(timezone.localdate(), "Y-m") + class Parser: """The period parser.