From 1038c182c050c9357d769341c3ef9d11f2d173f3 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Tue, 24 Jan 2017 09:46:24 +0100 Subject: Envoi des mails de manière groupé pour ne pas ouvrir trop de connexions au smtp --- management/commands/sendmails.py | 14 +++++++++++--- templates/mail_footer.txt | 2 +- templates/timetable_email.txt | 5 +---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py index 89c5c1b..1669bcc 100644 --- a/management/commands/sendmails.py +++ b/management/commands/sendmails.py @@ -1,7 +1,8 @@ from django.core.management.base import BaseCommand, CommandError -from django.core.mail import send_mail +from django.core.mail import send_mass_mail from django.utils import timezone from django.template import Context, loader +from django.conf import settings from edt.models import Group, Subscription, Course from edt.utils import get_week, group_courses @@ -21,6 +22,9 @@ class Command(BaseCommand): subscriptions = Subscription.objects.filter(active=True) content = {} + mails = [] + + print("Generating messages...") for subscription in subscriptions: if subscription.group.id not in content: courses = Course.objects.get_courses_for_group(subscription.group, begin__gte=start, begin__lt=end) @@ -31,7 +35,11 @@ class Command(BaseCommand): content[subscription.group.id] = template.render(context) footer = loader.get_template("mail_footer.txt") - context = Context({"token": subscription.token}) + context = Context({"admins": settings.ADMINS, "token": subscription.token}) mail_content = content[subscription.group.id] + footer.render(context) - # send content + 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) + self.stdout.write(self.style.SUCCESS("Done.")) diff --git a/templates/mail_footer.txt b/templates/mail_footer.txt index 59cb5d6..004abe5 100644 --- a/templates/mail_footer.txt +++ b/templates/mail_footer.txt @@ -1 +1 @@ -Pour vous désinscrire de cet emploi du temps, suivez ce lien : {% url "cancel" token %} +{# Pour vous désinscrire de cet emploi du temps, suivez ce lien : {% url "cancel" token %} #}Pour contacter l'administrateur du service, envoyez un mail à cette adresse : {{ admins|first|last }} diff --git a/templates/timetable_email.txt b/templates/timetable_email.txt index f8a4037..dcc07d1 100644 --- a/templates/timetable_email.txt +++ b/templates/timetable_email.txt @@ -1,7 +1,4 @@ -{% autoescape off %}{{ subscription.group.timetable.name }} - {{ subscription.group.name }} - Semaine {{ week }} - -{% for day in courses %} -{{ day.0.begin|date:"l j F o" }} - de {{ day.0.begin|date:"H:i" }} à {% with day|last as last %}{{ last.end|date:"H:i" }}{% endwith %} +{% autoescape off %}{% for day in courses %}{{ day.0.begin|date:"l j F o" }} - de {{ day.0.begin|date:"H:i" }} à {% with day|last as last %}{{ last.end|date:"H:i" }}{% endwith %} {% for course in day %} * {{ course.name }}, de {{ course.begin|date:"H:i" }} à {{ course.end|date:"H:i" }}{% if course.rooms.all|length > 0 %} Salle{% if course.rooms.all|length > 1 %}s{% endif %} {% for room in course.rooms.all %}{{ room }} {% endfor %}{% endif %} -- cgit v1.2.1