aboutsummaryrefslogtreecommitdiff
path: root/management/commands/sendmails.py
diff options
context:
space:
mode:
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."))