aboutsummaryrefslogtreecommitdiff
path: root/management/commands/sendmails.py
blob: dca7222ea4ea8edb67e4ebfb337d663d5e5d9dbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#    Copyright (C) 2017  Alban Gruin
#
#    celcatsanitizer is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    celcatsanitizer is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    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 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"

    def handle(self, *args, **options):
        translation.activate(settings.LANGUAGE_CODE)

        year, week = get_current_or_next_week()
        start, end = get_week(year, week)

        footer = loader.get_template("mail/mail_footer.txt")
        mails = []

        print("Generating messages...")
        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)

            for subscription in subscriptions:
                context = Context({"admins": settings.ADMINS, "token": subscription.token, "domain": settings.DEFAULT_DOMAIN})
                mail_content = content + footer.render(context)

                mails.append((title, mail_content, settings.DEFAULT_FROM_EMAIL, [subscription.email],))

        print("Sending mails...")
        send_mass_mail(mails)
        self.stdout.write(self.style.SUCCESS("Done."))