Skip to content

Commit 3b30eae

Browse files
committed
(experimental) add running totals functionality
1 parent 6679767 commit 3b30eae

File tree

6 files changed

+705
-2
lines changed

6 files changed

+705
-2
lines changed

hordak/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class AccountAdmin(MPTTModelAdmin):
2727
"balance_sum",
2828
"income",
2929
)
30-
readonly_fields = ("balance",)
30+
readonly_fields = ("balance", "balance_sum", "income")
3131
raw_id_fields = ("parent",)
3232
search_fields = (
3333
"code",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from django.core.mail import mail_admins
2+
from django.core.management.base import BaseCommand
3+
4+
from hordak.models import Account
5+
6+
7+
class Command(BaseCommand):
8+
help = "Recalculate running totals for all accounts"
9+
10+
def add_arguments(self, parser):
11+
parser.add_argument(
12+
"--check",
13+
action="store_true",
14+
dest="check",
15+
default=False,
16+
help="Check if the running totals are correct",
17+
)
18+
parser.add_argument(
19+
"--mail-admins",
20+
action="store_true",
21+
dest="mail_admins",
22+
default=False,
23+
help="Mail admins if the running totals are incorrect",
24+
)
25+
26+
def handle(self, *args, **options):
27+
print("Recalculating running totals for all accounts")
28+
all_values_are_correct = True
29+
queryset = Account.objects.all()
30+
for account in queryset[:1000]:
31+
value_correct = account.update_running_totals(check_only=options["check"])
32+
if not value_correct:
33+
all_values_are_correct = False
34+
35+
if options["mail_admins"] and not all_values_are_correct:
36+
mail_admins(
37+
"Running totals are incorrect",
38+
"Running totals are incorrect for some accounts",
39+
)
40+
41+
return 0 if all_values_are_correct else "Running totals are incorrect"

0 commit comments

Comments
 (0)