mirror of
https://gitlab.crans.org/bde/nk20-scripts
synced 2024-11-26 18:37:15 +00:00
Compare commits
2 Commits
f41a5a32f7
...
034d8c43b6
Author | SHA1 | Date | |
---|---|---|---|
|
034d8c43b6 | ||
|
630fc9a0df |
45
management/commands/send_reports.py
Normal file
45
management/commands/send_reports.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||||
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from django.core.management import BaseCommand
|
||||||
|
from django.db.models import Q
|
||||||
|
from django.template.loader import render_to_string
|
||||||
|
from django.utils import timezone
|
||||||
|
from django.utils.translation import activate
|
||||||
|
|
||||||
|
from note.models import NoteUser, Transaction
|
||||||
|
from note.tables import HistoryTable
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
activate('fr')
|
||||||
|
notes = NoteUser.objects.filter(
|
||||||
|
user__memberships__date_end__gte=timezone.now(),
|
||||||
|
).distinct().all()
|
||||||
|
for note in notes:
|
||||||
|
now = timezone.now()
|
||||||
|
last_week = now - timedelta(days=7)
|
||||||
|
last_transactions = Transaction.objects.filter(
|
||||||
|
Q(source=note) | Q(destination=note),
|
||||||
|
created_at__gte=last_week,
|
||||||
|
).all()
|
||||||
|
if not last_transactions.exists():
|
||||||
|
continue
|
||||||
|
|
||||||
|
table = HistoryTable(last_transactions)
|
||||||
|
incoming = sum(tr.total for tr in last_transactions if tr.destination.pk == note.pk if tr.valid)
|
||||||
|
outcoming = sum(tr.total for tr in last_transactions if tr.source.pk == note.pk if tr.valid)
|
||||||
|
context = dict(
|
||||||
|
user=note.user,
|
||||||
|
table=table,
|
||||||
|
incoming=incoming,
|
||||||
|
outcoming=outcoming,
|
||||||
|
diff=incoming - outcoming,
|
||||||
|
now=now,
|
||||||
|
last_week=last_week,
|
||||||
|
)
|
||||||
|
html = render_to_string("note/mails/weekly_report.html", context)
|
||||||
|
note.user.email_user("[Note Kfet] Rapport de la Note Kfet", html, html_message=html)
|
Loading…
Reference in New Issue
Block a user