From 9675b6372c6347a901f229e13edef5d6d2160b29 Mon Sep 17 00:00:00 2001 From: imperosol Date: Sun, 20 Oct 2024 12:19:00 +0200 Subject: [PATCH] add flags to the dump warning mail command --- .../management/commands/dump_warning_mail.py | 32 ++++++++++++++++++- locale/fr/LC_MESSAGES/django.po | 4 +-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/counter/management/commands/dump_warning_mail.py b/counter/management/commands/dump_warning_mail.py index 2b8fbfdd..9b966494 100644 --- a/counter/management/commands/dump_warning_mail.py +++ b/counter/management/commands/dump_warning_mail.py @@ -1,4 +1,5 @@ import logging +import time from smtplib import SMTPException from django.conf import settings @@ -25,9 +26,34 @@ class Command(BaseCommand): self.logger.setLevel(logging.INFO) super().__init__(*args, **kwargs) + def add_arguments(self, parser): + parser.add_argument( + "--dry-run", + action="store_true", + help="Do not send the mails, just display the number of users concerned", + ) + parser.add_argument( + "-d", + "--delay", + type=float, + default=0, + help="Delay in seconds between each mail sent", + ) + def handle(self, *args, **options): - users = list(self._get_users()) + users: list[User] = list(self._get_users()) self.stdout.write(f"{len(users)} users will be warned of their account dump") + if options["verbosity"] > 1: + self.stdout.write("Users concerned:\n") + self.stdout.write( + "\n".join( + f" - {user.get_display_name()} ({user.email}) : " + f"{user.customer.amount} €" + for user in users + ) + ) + if options["dry_run"]: + return dumps = [] for user in users: is_success = self._send_mail(user) @@ -38,6 +64,10 @@ class Command(BaseCommand): warning_mail_error=not is_success, ) ) + if options["delay"]: + # avoid spamming the mail server too much + time.sleep(options["delay"]) + AccountDump.objects.bulk_create(dumps) self.stdout.write("Finished !") diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index 54408c09..cc150a82 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-10-20 11:06+0200\n" +"POT-Creation-Date: 2024-10-20 12:20+0200\n" "PO-Revision-Date: 2016-07-18\n" "Last-Translator: Maréchal \n" @@ -3629,7 +3629,7 @@ msgstr "Produit parent" msgid "Buying groups" msgstr "Groupes d'achat" -#: counter/management/commands/dump_warning_mail.py:82 +#: counter/management/commands/dump_warning_mail.py:112 msgid "Clearing of your AE account" msgstr "Vidange de votre compte AE"