From d91081d8539988aaca426022c3f28ad78a024bea Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Mon, 13 Feb 2017 12:25:18 +0100 Subject: Ajout d'un mail pour signaler l'absence de cours pendant la semaine --- management/commands/sendmails.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'management/commands/sendmails.py') diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py index 5a2054f..e30bb1b 100644 --- a/management/commands/sendmails.py +++ b/management/commands/sendmails.py @@ -38,7 +38,10 @@ class Command(BaseCommand): subscriptions = Subscription.objects.filter(active=True) content = {} mails = [] + footer = loader.get_template("mail/mail_footer.txt") + no_event = loader.get_template("mail/mail_noevent.txt") + timetable = loader.get_template("mail/mail_timetable.txt") print("Generating messages...") for subscription in subscriptions: @@ -47,16 +50,15 @@ class Command(BaseCommand): if len(courses) > 0: grouped_courses = group_courses(courses) + context = Context({"courses": grouped_courses, "week": week}) + content[subscription.group.id] = timetable.render(context) + else: + context = Context({"group": subscription.group, "week": week}) + content[subscription.group.id] = no_event.render(context) - template = loader.get_template("mail/mail_timetable.txt") - context = Context({"subscription": subscription, "courses": grouped_courses, "week": week}) - content[subscription.group.id] = template.render(context) - - if subscription.group.id in content: - 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],)) + 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],)) print("Sending mails...") send_mass_mail(mails) -- cgit v1.2.1 From de405f2212fa64ad2a6a11f6d18186571021ad8c Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Mon, 13 Feb 2017 19:39:41 +0100 Subject: Regroupement des deux mails d'emploi du temps dans une seule template, c'est plus propre --- management/commands/sendmails.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'management/commands/sendmails.py') diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py index e30bb1b..9a05a9c 100644 --- a/management/commands/sendmails.py +++ b/management/commands/sendmails.py @@ -40,7 +40,6 @@ class Command(BaseCommand): mails = [] footer = loader.get_template("mail/mail_footer.txt") - no_event = loader.get_template("mail/mail_noevent.txt") timetable = loader.get_template("mail/mail_timetable.txt") print("Generating messages...") @@ -48,13 +47,9 @@ class Command(BaseCommand): if subscription.group.id not in content: courses = Course.objects.get_courses_for_group(subscription.group, begin__gte=start, begin__lt=end) - if len(courses) > 0: - grouped_courses = group_courses(courses) - context = Context({"courses": grouped_courses, "week": week}) - content[subscription.group.id] = timetable.render(context) - else: - context = Context({"group": subscription.group, "week": week}) - content[subscription.group.id] = no_event.render(context) + grouped_courses = group_courses(courses) + 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) -- cgit v1.2.1 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 From a9f2d9a4c736511235140f362ab33eb7da0cda61 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Mon, 27 Feb 2017 11:14:26 +0100 Subject: Nettoyage des imports --- management/commands/sendmails.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'management/commands/sendmails.py') diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py index b53d6f9..c7725b7 100644 --- a/management/commands/sendmails.py +++ b/management/commands/sendmails.py @@ -14,17 +14,15 @@ # with celcatsanitizer; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -from django.core.management.base import BaseCommand, CommandError +from django.core.management.base import BaseCommand from django.core.mail import send_mass_mail -from django.utils import timezone, translation +from django.utils import translation from django.template import Context, loader from django.conf import settings -from edt.models import Group, Subscription, Course +from edt.models import Course, Subscription from edt.utils import get_current_or_next_week, get_week, group_courses -import datetime - class Command(BaseCommand): help = "Sends emails to subscribed users" -- cgit v1.2.1