From b26de0fca265cfaba7a4e7bdfafa67df7985609e Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Mon, 23 Jan 2017 13:24:40 +0100 Subject: Implémentation initiale de l'envoi des mails aux abonnés --- management/commands/sendmails.py | 33 +++++++++++++++++++++++++++++++++ templates/timetable_email.txt | 9 +++++++++ 2 files changed, 42 insertions(+) create mode 100644 management/commands/sendmails.py create mode 100644 templates/timetable_email.txt diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py new file mode 100644 index 0000000..4e3b663 --- /dev/null +++ b/management/commands/sendmails.py @@ -0,0 +1,33 @@ +from django.core.management.base import BaseCommand, CommandError +from django.core.mail import send_mail +from django.utils import timezone +from django.template import Context, loader + +from edt.models import Group, Subscription, Course +from edt.utils import get_week, group_courses + +import datetime + + +class Command(BaseCommand): + help = "Sends emails to subscribed users" + + def handle(self, *args, **options): + year, week, day = timezone.now().isocalendar() + if day >= 6: + year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() + + start, end = get_week(year, week) + + subscriptions = Subscription.objects.filter(active=True) + content = {} + 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("timetable_email.txt") + context = Context({"subscription": subscription, "courses": grouped_courses, "week": week}) + content[subscription.group.id] = template.render(context) + + # send content diff --git a/templates/timetable_email.txt b/templates/timetable_email.txt new file mode 100644 index 0000000..f8a4037 --- /dev/null +++ b/templates/timetable_email.txt @@ -0,0 +1,9 @@ +{% 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 %} +{% 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 %} +{% endfor %} +{% endfor %}{% endautoescape %} -- cgit v1.2.1