From 3ca90d4aed87ed8b39c4984a290de6a3b898a7bd Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Fri, 27 Oct 2017 16:52:38 +0200 Subject: [PATCH] add command to notify teachers about unconfirmed students --- wtem/management/commands/notify_teachers.py | 45 +++++++++++++++++++ wtem/templates/wtem/notify_unconfirmed.txt | 13 ++++++ .../wtem/notify_unconfirmed_subject.txt | 1 + 3 files changed, 59 insertions(+) create mode 100644 wtem/management/commands/notify_teachers.py create mode 100644 wtem/templates/wtem/notify_unconfirmed.txt create mode 100644 wtem/templates/wtem/notify_unconfirmed_subject.txt diff --git a/wtem/management/commands/notify_teachers.py b/wtem/management/commands/notify_teachers.py new file mode 100644 index 0000000..354ab43 --- /dev/null +++ b/wtem/management/commands/notify_teachers.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +from datetime import timedelta +from django.core.management.base import BaseCommand +from django.template.loader import render_to_string +from django.utils import timezone + +from contact.models import Contact +from wtem.management.commands import send_mail +from wtem.models import Confirmation + + +class Command(BaseCommand): + def handle(self, *args, **options): + sent = 0 + failed = 0 + + query = Contact.objects.filter(form_tag='olimpiada').order_by('contact').distinct('contact') + template_name = 'notify_unconfirmed' + message_template = 'wtem/' + template_name + '.txt' + subject = render_to_string('wtem/' + template_name + '_subject.txt') + + threshold = timezone.now() - timedelta(4) + + for contact in query: + unconfirmed = [] + for similar_contact in Contact.objects.filter(contact=contact.contact): + unconfirmed += list(Confirmation.objects.filter( + contact=similar_contact, confirmed=False, contact__created_at__lt=threshold)) + if not unconfirmed: + continue + message = render_to_string(message_template, {'unconfirmed': unconfirmed}) + try: + self.send_message(message, subject, contact.contact) + except Exception as e: + failed += 1 + self.stdout.write('failed sending to: ' + contact.contact + ' - ' + str(e)) + else: + sent += 1 + self.stdout.write('message sent to: ' + contact.contact) + + self.stdout.write('sent: %s, failed: %s' % (sent, failed)) + + def send_message(self, message, subject, email): + self.stdout.write('>>> sending to %s' % email) + send_mail(subject=subject, body=message, to=[email]) diff --git a/wtem/templates/wtem/notify_unconfirmed.txt b/wtem/templates/wtem/notify_unconfirmed.txt new file mode 100644 index 0000000..7fc8fb7 --- /dev/null +++ b/wtem/templates/wtem/notify_unconfirmed.txt @@ -0,0 +1,13 @@ +Dzień dobry, +Dziękujemy za zarejestrowanie uczniów do udziału w Olimpiadzie Cyfrowej. + +Sprawdziliśmy, że nie mamy potwierdzenia rejestracji od kilku Pani/Pana uczniów (tzn. nie kliknęli w link, który otrzymali na maila): +{% for confirmation in unconfirmed %} +{{ confirmation.first_name }} {{ confirmation.last_name}} – {{ confirmation.email }}{% endfor %} + +Może to oznaczać, że podczas rejestracji zostały podane nieprawidłowe adresy, albo że mail od nas wpadł uczniom do spamu. Prosimy o sprawdzenie tego i przypomnienie uczniom o konieczności kliknięcia w link z maila. + +Pozdrawiamy, + +zespół Olimpiady Cyfrowej +fundacja Nowoczesna Polska \ No newline at end of file diff --git a/wtem/templates/wtem/notify_unconfirmed_subject.txt b/wtem/templates/wtem/notify_unconfirmed_subject.txt new file mode 100644 index 0000000..d5bf64d --- /dev/null +++ b/wtem/templates/wtem/notify_unconfirmed_subject.txt @@ -0,0 +1 @@ +Olimpiada Cyfrowa – brak potwierdzenia zgłoszeń przez uczniów \ No newline at end of file -- 2.20.1