aboutsummaryrefslogtreecommitdiff
path: root/management/commands/sendmails.py
diff options
context:
space:
mode:
authorAlban Gruin2017-02-28 13:30:03 +0100
committerAlban Gruin2017-02-28 13:30:03 +0100
commitce0ed9347adde9ca80c11efe79766c966d5749ba (patch)
treee371a2d06374efbad1c1c8a4558d92ee0a07c025 /management/commands/sendmails.py
parenta543aaa14957f390d2b640a264113b639c7d3194 (diff)
parent66be6f2e7a3c642fb3c69e7c2a70cc3f898d77ea (diff)
Merge branch 'stable/0.y.z' into prod/pa1ch/0.y.zv0.7.0-pa1ch
Diffstat (limited to 'management/commands/sendmails.py')
-rw-r--r--management/commands/sendmails.py33
1 files changed, 19 insertions, 14 deletions
diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py
index 5a2054f..c7725b7 100644
--- a/management/commands/sendmails.py
+++ b/management/commands/sendmails.py
@@ -14,21 +14,22 @@
# 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"
+ 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)
@@ -38,26 +39,30 @@ class Command(BaseCommand):
subscriptions = Subscription.objects.filter(active=True)
content = {}
mails = []
+
footer = loader.get_template("mail/mail_footer.txt")
+ timetable = loader.get_template("mail/mail_timetable.txt")
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)
- if len(courses) > 0:
- grouped_courses = group_courses(courses)
+ grouped_courses = group_courses(courses)
+ context = Context({"courses": grouped_courses, "group": subscription.group, "week": week})
+ content[subscription.group.id] = timetable.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 options["test"]:
+ print(subscription.group)
+ print(content[subscription.group.id])
- if subscription.group.id in content:
+ 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],))
- print("Sending mails...")
- send_mass_mail(mails)
+ if not options["test"]:
+ print("Sending mails...")
+ send_mass_mail(mails)
+
self.stdout.write(self.style.SUCCESS("Done."))