From 666e7a724b45bc234ba1662206e48adfbd3512eb Mon Sep 17 00:00:00 2001
From: Robin Sonnabend <robin@fsmpi.rwth-aachen.de>
Date: Mon, 5 Feb 2018 19:09:29 +0100
Subject: [PATCH] Send reminder mails if protocols remain unfinished

This adds the configuration key MAX_PAST_INDEX_DAYS_BEFORE_REMINDER
ref #158
---
 server.py                           | 11 ++++++-----
 tasks.py                            | 14 ++++++++++++++
 templates/remind-finishing-mail.txt | 10 ++++++++++
 3 files changed, 30 insertions(+), 5 deletions(-)
 create mode 100644 templates/remind-finishing-mail.txt

diff --git a/server.py b/server.py
index a429ca3..a98dac6 100755
--- a/server.py
+++ b/server.py
@@ -1381,24 +1381,25 @@ except ImportError as exc:
         atexit.register(scheduler.shutdown)
 
 def check_and_send_reminders():
-    print("check and send reminders")
     if not config.MAIL_ACTIVE:
         return
     with app.app_context():
         current_time = datetime.now()
         current_day = current_time.date()
-        print("regular action for reminders")
         for protocol in Protocol.query.filter(Protocol.done != True).all():
             day_difference = (protocol.date - current_day).days
             usual_time = protocol.get_time()
             protocol_time = datetime(1, 1, 1, usual_time.hour, usual_time.minute)
             hour_difference = (protocol_time - current_time).seconds // 3600
-            print("diff: {} days, {} hours".format(day_difference, hour_difference))
             for reminder in protocol.protocoltype.reminders:
-                print(reminder)
                 if day_difference == reminder.days_before and hour_difference == 0:
-                    print("reminder matching, sending")
                     tasks.send_reminder(reminder, protocol)
+            if (day_difference < 0
+                and -day_difference > config.MAX_PAST_INDEX_DAYS_BEFORE_REMINDER
+                and hour_difference == 0): # once per day
+                tasks.remind_finishing(protocol, -day_difference,
+                    config.MAX_PAST_INDEX_DAYS_BEFORE_REMINDER)
+
 
 if __name__ == "__main__":
     manager.run()
diff --git a/tasks.py b/tasks.py
index e0efdeb..080c839 100644
--- a/tasks.py
+++ b/tasks.py
@@ -663,6 +663,20 @@ def send_reminder_async(reminder_id, protocol_id):
                 "Tagesordnung der {}".format(protocol.protocoltype.name),
                 reminder_text, reply_to=protocol.protocoltype.private_mail)
 
+def remind_finishing(protocol, delay_days, min_delay_days):
+    remind_finishing.delay(protocol.id, delay_days, min_delay_days)
+
+@celery.task
+def remind_finishing(protocol_id, delay_days, min_delay_days):
+    with app.app_context():
+        protocol = Protocol.query.filter_by(id=protocol_id).first()
+        mail_text = render_template("remind-finishing-mail.txt",
+            protocol=protocol, delay_days=delay_days,
+            min_delay_days=min_delay_days)
+        send_mail(protocol, protocol.protocoltype.private_mail,
+            "Unfertiges Protokoll der {}".format(protocol.protocoltype.name),
+            mail_text, reply_to=protocol.protocoltype.private_mail)
+
 def send_protocol_private(protocol):
     send_protocol_async.delay(protocol.id, show_private=True)
     send_todomails_async.delay(protocol.id)
diff --git a/templates/remind-finishing-mail.txt b/templates/remind-finishing-mail.txt
new file mode 100644
index 0000000..28be285
--- /dev/null
+++ b/templates/remind-finishing-mail.txt
@@ -0,0 +1,10 @@
+Das Protokoll {{protocol.protocoltype.name}} vom {{protocol.date|datify}} ist, obwohl diese Sitzung bereits {{delay_days}} in der Vergangenheit liegt, noch nicht fertig.
+Bitte erledigt dies.
+
+Falls die Sitzung nicht stattgefunden hat, könnt ihr sie ohne Protokoll schließen oder von den Admins löschen lassen.
+Sollte sie stattgefunden haben, stellt bitte das Protokoll fertig.
+
+Bis zum Erledigen wird euch diese Erinnerungsmail täglich zugesendet.
+
+Mit{% if (delay_days - min_delay_days) < min_delay_days %} freundlichen{% endif %} Grüßen
+Euer Protokollsystem
-- 
GitLab