From a50a8a01922543443eaeaa937545ecd8e16be25c Mon Sep 17 00:00:00 2001
From: Robin Sonnabend <robin@fsmpi.rwth-aachen.de>
Date: Wed, 6 Jun 2018 17:01:58 +0200
Subject: [PATCH] Add (public) ical appointment calendar

---
 server.py            | 42 +++++++++++++++++++++++++++++++-----------
 templates/index.html |  3 +++
 2 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/server.py b/server.py
index 41f7c85..bc22ac5 100755
--- a/server.py
+++ b/server.py
@@ -1809,17 +1809,9 @@ def feed_appointments_atom(protocoltype):
         mimetype="application/atom+xml")
 
 
-@app.route("/feed/appointments/ical/<int:protocoltype_id>")
-@db_lookup(ProtocolType)
-def feed_appointments_ical(protocoltype):
-    if not protocoltype.has_public_anonymous_view_right():
-        abort(403)
-    protocols = [
-        protocol for protocol in protocoltype.protocols
-        if not protocol.is_done()
-    ]
+def make_calendar_from_protocols(protocols, summary):
     calendar = icalendar.Calendar()
-    calendar["summary"] = protocoltype.short_name
+    calendar["summary"] = summary
     calendar["prodid"] = "Protokollsystem 3"
     calendar["version"] = "2.0"
     for protocol in protocols:
@@ -1830,7 +1822,7 @@ def feed_appointments_ical(protocoltype):
         event["dtstamp"] = to_datetime(start)
         event["dtstart"] = to_datetime(start)
         event["dtend"] = to_datetime(start + timedelta(hours=3))
-        event["summary"] = protocoltype.short_name
+        event["summary"] = protocol.protocoltype.short_name
         event["description"] = "\n".join(
             top.name for top in protocol.get_tops())
         calendar.add_component(event)
@@ -1842,6 +1834,34 @@ def feed_appointments_ical(protocoltype):
     return Response(content.encode("utf-8"), mimetype="text/calendar")
 
 
+@app.route("/feed/appointments/ical/<int:protocoltype_id>")
+@db_lookup(ProtocolType)
+def feed_appointments_ical(protocoltype):
+    if not protocoltype.has_public_anonymous_view_right():
+        abort(403)
+    protocols = [
+        protocol for protocol in protocoltype.protocols
+        if not protocol.is_done()
+    ]
+    return make_calendar_from_protocols(protocols, protocoltype.short_name)
+
+
+@app.route("/feed/appointments/ical/")
+def feed_all_appointments_ical():
+    user = current_user()
+    types = [
+        protocoltype for protocoltype in ProtocolType.query.all()
+        if (protocoltype.has_private_view_right(user)
+            or protocoltype.has_public_view_right(user)
+            or protocoltype.is_public)]
+    protocols = [
+        protocol
+        for protocoltype in types
+        for protocol in protocoltype.protocols
+    ]
+    return make_calendar_from_protocols(protocols, "Sitzungskalender")
+
+
 @app.route("/like/new")
 @login_required
 @protect_csrf
diff --git a/templates/index.html b/templates/index.html
index 7db27c9..5d4acd7 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -11,6 +11,9 @@
             {% if check_login() %}
                 <a href="{{url_for("new_protocol")}}">Neu</a>
             {% endif %}
+            <a href="{{url_for("feed_all_appointments_ical")}}">
+              <img src="{{url_for("static", filename="images/feed-icon.svg")}}" width="18px"></img>
+            </a>
         </h3>
         <ul>
             {% if open_protocols|length > 0 %}
-- 
GitLab