updates for stage 2
[edumed.git] / wtem / management / commands / notify_teachers.py
index 354ab43..e7d3db0 100644 (file)
@@ -9,6 +9,11 @@ from wtem.management.commands import send_mail
 from wtem.models import Confirmation
 
 
+THRESHOLD = 0
+
+AFTER_DEADLINE = True
+
+
 class Command(BaseCommand):
     def handle(self, *args, **options):
         sent = 0
@@ -16,19 +21,25 @@ class Command(BaseCommand):
 
         query = Contact.objects.filter(form_tag='olimpiada').order_by('contact').distinct('contact')
         template_name = 'notify_unconfirmed'
-        message_template = 'wtem/' + template_name + '.txt'
+        message_template = 'wtem/' + template_name + ('_after' if AFTER_DEADLINE else '') + '.txt'
         subject = render_to_string('wtem/' + template_name + '_subject.txt')
 
-        threshold = timezone.now() - timedelta(4)
+        threshold = timezone.now() - timedelta(THRESHOLD)
 
         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))
+            contacts = []
+            for similar_contact in Contact.objects.filter(contact=contact.contact, form_tag=contact.form_tag):
+                contact_emails = [s['email'] for s in similar_contact.body.get('student', [])]
+                new_unconfirmed = list(Confirmation.objects.filter(
+                    contact=similar_contact, confirmed=False, contact__created_at__lt=threshold,
+                    email__in=contact_emails))
+                unconfirmed += new_unconfirmed
+                if new_unconfirmed:
+                    contacts.append(similar_contact)
             if not unconfirmed:
                 continue
-            message = render_to_string(message_template, {'unconfirmed': unconfirmed})
+            message = render_to_string(message_template, {'unconfirmed': unconfirmed, 'contacts': contacts})
             try:
                 self.send_message(message, subject, contact.contact)
             except Exception as e: