From 36ff1a140d93b097f90e3eddf3211f229a1f837e Mon Sep 17 00:00:00 2001
From: Robin Sonnabend <robin@fsmpi.rwth-aachen.de>
Date: Thu, 9 Mar 2017 16:14:19 +0100
Subject: [PATCH] Add option to manually send reminder mails

/close #52
---
 server.py                    | 27 +++++++++++++++++++++++++++
 templates/protocol-show.html |  3 +++
 2 files changed, 30 insertions(+)

diff --git a/server.py b/server.py
index d88bd31..3949cbb 100755
--- a/server.py
+++ b/server.py
@@ -661,6 +661,33 @@ def send_protocol(protocol):
     flash("Das Protokoll wurde versandt.", "alert-success")
     return redirect(request.args.get("next") or url_for("show_protocol", protocol_id=protocol.id))
 
+@app.route("/protocol/reminder/<int:protocol_id>")
+@login_required
+@db_lookup(Protocol)
+@require_modify_right()
+def send_protocol_reminder(protocol):
+    if not config.MAIL_ACTIVE:
+        flash("Die Mailfunktion ist nicht aktiviert.", "alert-error")
+        return redirect(request.args.get("next") or url_for("show_protocol", protocol_id=protocol_id))
+    meetingreminders = MeetingReminder.query.filter_by(protocoltype_id=protocol.protocoltype.id).all()
+    if len(meetingreminders) == 0:
+        flash("Für diesen Protokolltyp sind keine Einladungsmails konfiguriert.", "alert-error")
+        return redirect(request.args.get("next") or url_for("show_protocol", protocol_id=protocol_id))
+    day_difference = (protocol.date - datetime.now().date()).days
+    past_reminders = [
+        meetingreminder for meetingreminder in meetingreminders
+        if meetingreminder.days_before > day_difference
+    ]
+    if len(past_reminders) == 0:
+        flash("Bisher hätte keine Einladungsmail verschickt werden sollen, schicke letzte.", "alert-info")
+        past_reminders = meetingreminders
+    past_reminders = sorted(past_reminders, key=lambda r: r.days_before)
+    choosen_reminder = past_reminders[0]
+    tasks.send_reminder(choosen_reminder, protocol)
+    flash("Einladungsmail ist versandt.", "alert-success")
+    return redirect(request.args.get("next") or url_for("show_protocol", protocol_id=protocol.id))
+        
+
 @app.route("/protocol/tops/new/<int:protocol_id>", methods=["GET", "POST"])
 @login_required
 @db_lookup(Protocol)
diff --git a/templates/protocol-show.html b/templates/protocol-show.html
index 9673b8e..1316ede 100644
--- a/templates/protocol-show.html
+++ b/templates/protocol-show.html
@@ -27,6 +27,9 @@
                 {% endif %}
                 {% if not protocol.is_done() %}
                     <a class="btn btn-default" href="{{url_for("get_protocol_template", protocol_id=protocol.id)}}">Vorlage</a>
+                    {% if config.MAIL_ACTIVE %}
+                        <a class="btn btn-default" href="{{url_for("send_protocol_reminder", protocol_id=protocol.id)}}" onclick="return confirm('Bist du dir sicher, dass du manuell eine Einladung verschicken willst? Dies wird auch automatisch geschehen.');">Einladung versenden</a>
+                    {% endif %}
                 {% else %}
                     {% if config.MAIL_ACTIVE %}
                         <a class="btn btn-default" href="{{url_for("send_protocol", protocol_id=protocol.id)}}">Mail versenden</a>
-- 
GitLab