diff options
author | Alban Gruin | 2017-02-12 12:30:30 +0100 |
---|---|---|
committer | Alban Gruin | 2017-02-12 12:30:30 +0100 |
commit | 6bb960804653efe4b0da436d01d9d9855090f82a (patch) | |
tree | 5723e82f5eed8685976755064b50b1e73ebb33fd /management/commands | |
parent | bb5c77bf35b13f5c11bc4cdbc0325983e349b984 (diff) |
Refonte du système d'envoi des mails. N'envoie plus de mails si l'emploi du temps est vide
Diffstat (limited to 'management/commands')
-rw-r--r-- | management/commands/sendmails.py | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py index 3126243..dca7222 100644 --- a/management/commands/sendmails.py +++ b/management/commands/sendmails.py @@ -14,17 +14,29 @@ # with celcatsanitizer; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +from django.conf import settings from django.core.management.base import BaseCommand, CommandError from django.core.mail import send_mass_mail +from django.db.models import Count from django.utils import timezone, translation from django.template import Context, loader -from django.conf import settings from edt.models import Group, Subscription, Course from edt.utils import get_current_or_next_week, get_week, group_courses import datetime +def __generate_mails(start, end): + subscribed_groups = Subscription.objects.filter(active=True).annotate(nb=Count("group")) + for subscription in subscribed_groups: + courses = Course.objects.get_courses_for_group(subscription.group, begin__gte=start, begin__lt=end) + if len(courses) > 0: + grouped_courses = group_courses(courses) + + template = loader.get_template("mail/mail_timetable.txt") + context = Context({"subscription": subscription, "courses": grouped_couses, "week": week}) + yield subscription.group, template.render(context) + class Command(BaseCommand): help = "Sends emails to subscribed users" @@ -35,25 +47,19 @@ class Command(BaseCommand): year, week = get_current_or_next_week() start, end = get_week(year, week) - subscriptions = Subscription.objects.filter(active=True) - content = {} + footer = loader.get_template("mail/mail_footer.txt") 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) - grouped_courses = group_courses(courses) - - template = loader.get_template("mail/mail_timetable.txt") - context = Context({"subscription": subscription, "courses": grouped_courses, "week": week}) - content[subscription.group.id] = template.render(context) + for group, content in __generate_mails(start, end): + subscriptions = Subscription.objects.filter(active=True, group=group) + title = "{0} - {1} - Semaine {2}".format(subscription.group.timetable.name, subscription.group.name, week) - footer = loader.get_template("mail/mail_footer.txt") - context = Context({"admins": settings.ADMINS, "token": subscription.token, "domain": settings.DEFAULT_DOMAIN}) - mail_content = content[subscription.group.id] + footer.render(context) + for subscription in subscriptions: + context = Context({"admins": settings.ADMINS, "token": subscription.token, "domain": settings.DEFAULT_DOMAIN}) + mail_content = content + 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],)) + mails.append((title, mail_content, settings.DEFAULT_FROM_EMAIL, [subscription.email],)) print("Sending mails...") send_mass_mail(mails) |