Added the current_app parameters to the uses of reverse() to enable different namespaces in the accounting application.

This commit is contained in:
依瑪貓 2020-08-17 23:05:01 +08:00
parent 062f4422d1
commit dec6233d4e
3 changed files with 35 additions and 16 deletions

View File

@ -22,6 +22,7 @@ import re
from typing import Union, Optional from typing import Union, Optional
from django import template from django import template
from django.template import RequestContext
from accounting.models import Account from accounting.models import Account
from accounting.utils import ReportUrl from accounting.utils import ReportUrl
@ -47,13 +48,15 @@ def accounting_amount(value: Union[str, int]) -> str:
return s return s
@register.simple_tag @register.simple_tag(takes_context=True)
def report_url(cash_account: Optional[Account], def report_url(context: RequestContext,
cash_account: Optional[Account],
ledger_account: Optional[Account], ledger_account: Optional[Account],
period: Optional[Period]) -> ReportUrl: period: Optional[Period]) -> ReportUrl:
"""Returns accounting report URL helper. """Returns accounting report URL helper.
Args: Args:
context: The request context.
cash_account: The current cash account. cash_account: The current cash account.
ledger_account: The current ledger account. ledger_account: The current ledger account.
period: The period. period: The period.
@ -62,6 +65,7 @@ def report_url(cash_account: Optional[Account],
ReportUrl: The accounting report URL helper. ReportUrl: The accounting report URL helper.
""" """
return ReportUrl( return ReportUrl(
namespace=context.request.resolver_match.namespace,
cash=cash_account or None, cash=cash_account or None,
ledger=ledger_account or None, ledger=ledger_account or None,
period=period or None) period=period or None)

View File

@ -85,6 +85,7 @@ class ReportUrl:
"""The URL of the accounting reports. """The URL of the accounting reports.
Args: Args:
namespace: The namespace of the current application.
cash: The currently-specified account of the cash: The currently-specified account of the
cash account or cash summary. cash account or cash summary.
ledger: The currently-specified account of the ledger: The currently-specified account of the
@ -92,36 +93,45 @@ class ReportUrl:
period: The currently-specified period. period: The currently-specified period.
""" """
def __init__(self, cash: Account = None, ledger: Account = None, def __init__(self, namespace: str, cash: Account = None,
period: Period = None): ledger: Account = None, period: Period = None,):
self._period = Period() if period is None else period self._period = Period() if period is None else period
self._cash = get_default_cash_account() if cash is None else cash self._cash = get_default_cash_account() if cash is None else cash
self._ledger = get_default_ledger_account()\ self._ledger = get_default_ledger_account()\
if ledger is None else ledger if ledger is None else ledger
self._namespace = namespace
def cash(self) -> str: def cash(self) -> str:
return reverse("accounting:cash", args=[self._cash, self._period]) return reverse("accounting:cash", args=[self._cash, self._period],
current_app=self._namespace)
def cash_summary(self) -> str: def cash_summary(self) -> str:
return reverse("accounting:cash-summary", args=[self._cash]) return reverse("accounting:cash-summary", args=[self._cash],
current_app=self._namespace)
def ledger(self) -> str: def ledger(self) -> str:
return reverse("accounting:ledger", args=[self._ledger, self._period]) return reverse("accounting:ledger", args=[self._ledger, self._period],
current_app=self._namespace)
def ledger_summary(self) -> str: def ledger_summary(self) -> str:
return reverse("accounting:ledger-summary", args=[self._ledger]) return reverse("accounting:ledger-summary", args=[self._ledger],
current_app=self._namespace)
def journal(self) -> str: def journal(self) -> str:
return reverse("accounting:journal", args=[self._period]) return reverse("accounting:journal", args=[self._period],
current_app=self._namespace)
def trial_balance(self) -> str: def trial_balance(self) -> str:
return reverse("accounting:trial-balance", args=[self._period]) return reverse("accounting:trial-balance", args=[self._period],
current_app=self._namespace)
def income_statement(self) -> str: def income_statement(self) -> str:
return reverse("accounting:income-statement", args=[self._period]) return reverse("accounting:income-statement", args=[self._period],
current_app=self._namespace)
def balance_sheet(self) -> str: def balance_sheet(self) -> str:
return reverse("accounting:balance-sheet", args=[self._period]) return reverse("accounting:balance-sheet", args=[self._period],
current_app=self._namespace)
class Populator: class Populator:

View File

@ -705,7 +705,8 @@ def balance_sheet(request: HttpRequest, period: Period) -> HttpResponse:
.filter(Q(amount__isnull=False), ~Q(amount=0)) .filter(Q(amount__isnull=False), ~Q(amount=0))
.order_by("code")) .order_by("code"))
for account in accounts: for account in accounts:
account.url = reverse("accounting:ledger", args=[account, period]) account.url = reverse("accounting:ledger", args=[account, period],
current_app=request.resolver_match.namespace)
balance = Record.objects \ balance = Record.objects \
.filter( .filter(
Q(transaction__date__lt=period.start) Q(transaction__date__lt=period.start)
@ -722,7 +723,8 @@ def balance_sheet(request: HttpRequest, period: Period) -> HttpResponse:
code=Account.ACCUMULATED_BALANCE) code=Account.ACCUMULATED_BALANCE)
brought_forward.amount = balance brought_forward.amount = balance
brought_forward.url = reverse( brought_forward.url = reverse(
"accounting:income-statement", args=[period.period_before()]) "accounting:income-statement", args=[period.period_before()],
current_app=request.resolver_match.namespace)
accounts.append(brought_forward) accounts.append(brought_forward)
balance = Record.objects \ balance = Record.objects \
.filter( .filter(
@ -740,7 +742,8 @@ def balance_sheet(request: HttpRequest, period: Period) -> HttpResponse:
net_change = Account.objects.get(code=Account.NET_CHANGE) net_change = Account.objects.get(code=Account.NET_CHANGE)
net_change.amount = balance net_change.amount = balance
net_change.url = reverse( net_change.url = reverse(
"accounting:income-statement", args=[period]) "accounting:income-statement", args=[period],
current_app=request.resolver_match.namespace)
accounts.append(net_change) accounts.append(net_change)
for account in [x for x in accounts if x.code[0] in "23"]: for account in [x for x in accounts if x.code[0] in "23"]:
account.amount = -account.amount account.amount = -account.amount
@ -883,7 +886,9 @@ class TransactionDeleteView(DeleteView):
return self.kwargs["txn"] return self.kwargs["txn"]
def get_success_url(self): def get_success_url(self):
return self.request.GET.get("r") or reverse("accounting:home") return self.request.GET.get("r")\
or reverse("accounting:home",
current_app=self.request.resolver_match.namespace)
@method_decorator(login_required, name="dispatch") @method_decorator(login_required, name="dispatch")