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 @@ + + + + Saturn + + + {% for event in events %} +
+ +
+ {% endfor %} + {% for event in events_day %} +
+ +
+ {% endfor %} + + 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'
  • {event.instance.vevent.dtstart.value}
  • ' - #return f''