From 0649ee9c528d7a3957fb65e195a4a56a702e8d48 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sun, 26 Feb 2017 22:08:08 +0100 Subject: Ajout du paramètre --test à la commande sendmails pour afficher les mails au lieu de les envoyer --- management/commands/sendmails.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'management/commands/sendmails.py') diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py index 9a05a9c..b53d6f9 100644 --- a/management/commands/sendmails.py +++ b/management/commands/sendmails.py @@ -29,6 +29,9 @@ import datetime class Command(BaseCommand): help = "Sends emails to subscribed users" + def add_arguments(self, parser): + parser.add_argument("--test", help="Print the content of mails instead of sending them", action="store_true") + def handle(self, *args, **options): translation.activate(settings.LANGUAGE_CODE) @@ -51,10 +54,17 @@ class Command(BaseCommand): context = Context({"courses": grouped_courses, "group": subscription.group, "week": week}) content[subscription.group.id] = timetable.render(context) - context = Context({"admins": settings.ADMINS, "token": subscription.token, "domain": settings.DEFAULT_DOMAIN}) - mail_content = content[subscription.group.id] + footer.render(context) - mails.append(("{0} - {1} - Semaine {2}".format(subscription.group.timetable.name, subscription.group.name, week), mail_content, settings.DEFAULT_FROM_EMAIL, [subscription.email],)) + if options["test"]: + print(subscription.group) + print(content[subscription.group.id]) + + if not options["test"]: + context = Context({"admins": settings.ADMINS, "token": subscription.token, "domain": settings.DEFAULT_DOMAIN}) + mail_content = content[subscription.group.id] + footer.render(context) + mails.append(("{0} - {1} - Semaine {2}".format(subscription.group.timetable.name, subscription.group.name, week), mail_content, settings.DEFAULT_FROM_EMAIL, [subscription.email],)) + + if not options["test"]: + print("Sending mails...") + send_mass_mail(mails) - print("Sending mails...") - send_mass_mail(mails) self.stdout.write(self.style.SUCCESS("Done.")) -- cgit v1.2.1