From e32098e68cb985f675b816bb47038254c1120bc1 Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Wed, 26 Jan 2022 16:47:24 +0100
Subject: [PATCH] Added basic homepage

---
 {src => saturn}/__main__.py        |  6 ++++-
 saturn/calendar/__init__.py        | 29 +++++++++++++++++++++++
 saturn/server/__init__.py          | 20 ++++++++++++++++
 saturn/server/templates/index.html | 38 ++++++++++++++++++++++++++++++
 src/server/__init__.py             | 19 ---------------
 5 files changed, 92 insertions(+), 20 deletions(-)
 rename {src => saturn}/__main__.py (85%)
 create mode 100644 saturn/calendar/__init__.py
 create mode 100644 saturn/server/__init__.py
 create mode 100644 saturn/server/templates/index.html
 delete mode 100644 src/server/__init__.py

diff --git a/src/__main__.py b/saturn/__main__.py
similarity index 85%
rename from src/__main__.py
rename to saturn/__main__.py
index 3fc0d03..c2da513 100644
--- a/src/__main__.py
+++ b/saturn/__main__.py
@@ -10,7 +10,8 @@ from pathlib import Path
 from datetime import date
 import caldav
 
-from .server import app
+from .server import create_app
+from .calendar import get_davclient
 
 
 parser = argparse.ArgumentParser(description='Web-based CalDav client')
@@ -28,6 +29,9 @@ log = logging.getLogger('saturn')
 log.info('Starting saturn')
 
 
+davclient = get_davclient(config['caldav'])
+app = create_app(davclient)
+
 if __name__ == '__main__':
     app.run(
         host=config.get('server', 'host', fallback=None),
diff --git a/saturn/calendar/__init__.py b/saturn/calendar/__init__.py
new file mode 100644
index 0000000..c2d4a1a
--- /dev/null
+++ b/saturn/calendar/__init__.py
@@ -0,0 +1,29 @@
+import caldav
+
+from datetime import date, datetime
+
+
+def get_davclient(config):
+    return caldav.davclient.DAVClient(
+        config.get('host'),
+        username=config.get('username', fallback=None),
+        password=config.get('password', fallback=None),
+    )
+
+def get_events(davclient):
+    pri = davclient.principal()
+
+    events = []
+    events_day = []
+
+    for cal in pri.calendars():
+        events += [event.instance.vevent
+            for event in cal.date_search(start=date.today())
+            if type(event.instance.vevent.dtstart.value) == datetime]
+        events_day += [event.instance.vevent
+            for event in cal.date_search(start=date.today())
+            if type(event.instance.vevent.dtstart.value) != datetime]
+    events.sort(key=lambda event:event.dtstart.value)
+    events_day.sort(key=lambda event:event.dtstart.value)
+    
+    return events, events_day
diff --git a/saturn/server/__init__.py b/saturn/server/__init__.py
new file mode 100644
index 0000000..7c3e716
--- /dev/null
+++ b/saturn/server/__init__.py
@@ -0,0 +1,20 @@
+import flask
+
+from datetime import date, datetime
+from ..calendar import get_events
+
+
+def create_app(davclient):
+    app = flask.Flask(__name__)
+    
+    @app.route('/')
+    def home():
+        events, events_day = get_events(davclient)
+
+        return flask.render_template(
+            'index.html',
+            events = events,
+            events_day = events_day,
+        )
+
+    return app
diff --git a/saturn/server/templates/index.html b/saturn/server/templates/index.html
new file mode 100644
index 0000000..43ba2bc
--- /dev/null
+++ b/saturn/server/templates/index.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html lang='en'>
+	<head>
+		<title>Saturn</title>
+	</head>
+	<body>
+		{% for event in events %}
+			<div>
+				<ul>
+					<li>{{ event.dtstart.value }}</li>
+					<li>{{ event.dtend.value }}</li>
+					<li>{{ event.summary.value }}</li>
+					{% if event.description %}
+					<li>{{ event.description.value }}</li>
+					{% endif %}
+					{% if event.location %}
+					<li>{{ event.location.value }}</li>
+					{% endif %}
+					{% if event.rrule %}
+					<li>{{ event.rrule.value }}</li>
+					{% endif %}
+				</ul>
+			</div>
+		{% endfor %}
+		{% for event in events_day %}
+			<div>
+				<ul>
+					<li>{{ event.dtstart.value }}</li>
+					<li>{{ event.dtend.value }}</li>
+					<li>{{ event.summary.value }}</li>
+					{% if event.description %}
+					<li>{{ event.description.value }}</li>
+					{% endif %}
+				</ul>
+			</div>
+		{% endfor %}
+	</body>
+</html>
diff --git a/src/server/__init__.py b/src/server/__init__.py
deleted file mode 100644
index 8cb1c6d..0000000
--- a/src/server/__init__.py
+++ /dev/null
@@ -1,19 +0,0 @@
-import flask
-
-app = flask.Flask(__name__)
-
-@app.route('/')
-def home():
-    return 'Hello World!'
-    #cli = caldav.davclient.DAVClient(
-    #    config.get('caldav', 'host'),
-    #    username=config.get('caldav', 'username', fallback=None),
-    #    password=config.get('caldav', 'password', fallback=None),
-    #)
-    #pri = cli.principal()
-    #
-    #res = ''
-    #for cal in pri.calendars():
-    #    for event in cal.date_search(start=date.today()):
-    #        res += f'<li>{event.instance.vevent.dtstart.value}</li>'
-    #return f'<ul>{res}</ul>'
