109 lines
3.3 KiB
Python
109 lines
3.3 KiB
Python
import os
|
|
import logging
|
|
import json
|
|
|
|
from flask import Flask, render_template, g, url_for, redirect
|
|
from werkzeug.exceptions import abort
|
|
|
|
import calendar_interface
|
|
import db
|
|
import auth
|
|
|
|
logging.basicConfig(format='%(asctime)s,%(msecs)d %(levelname)-4s [%(filename)s:%(lineno)d] %(message)s',
|
|
datefmt='%Y-%m-%d:%H:%M:%S',
|
|
level=logging.INFO
|
|
)
|
|
|
|
app = Flask(__name__)
|
|
app.config.from_object(os.environ["CONFIG"])# load config, pointed to by env var
|
|
|
|
app.teardown_appcontext(db.close_db)
|
|
app.cli.add_command(db.init_db_command)
|
|
app.register_blueprint(auth.bp)
|
|
|
|
# first time start up msal token acquire for test purposes (raises if not successful)
|
|
if not (app.config["DEBUG"] or app.config["TESTING"]):
|
|
calendar_interface.get_access_token()
|
|
|
|
@app.route('/')
|
|
@auth.login_required
|
|
def index():
|
|
events = calendar_interface.get_future_calendar_events().get("value")
|
|
calendar_interface.convert_datetimes(events)
|
|
# Check current users attendance in event
|
|
for event in events:
|
|
ret = calendar_interface.check_attendee_presence(event, g.user["email"])
|
|
event["user_attends"] = ret
|
|
if events is None:
|
|
abort(404)
|
|
else:
|
|
return render_template('index.html', events=events, user=g.user)
|
|
|
|
|
|
@app.route('/<id>')
|
|
@auth.login_required
|
|
def show_event(id):
|
|
event = calendar_interface.get_calendar_event(id)
|
|
|
|
if "error" in event:
|
|
print("id:", id)
|
|
logging.error("Found error in event query with id\n%s", event)
|
|
logging.error(json.dumps(event, indent=2))
|
|
abort(404)
|
|
|
|
calendar_interface.convert_datetime(event)
|
|
|
|
attendees= ", ".join(
|
|
[a["emailAddress"]["name"] + " (" + a["emailAddress"]["name"] + ")" for a in event["attendees"]]
|
|
)
|
|
location= "<br/>".join(event["location"]["address"].values())
|
|
|
|
return render_template(
|
|
'show_event.html',
|
|
subject=event["subject"],
|
|
attendees=attendees,
|
|
content=event["body"]["content"],
|
|
location=location,
|
|
start=event["start"]["date"] + " - " + event["start"]["time"],
|
|
end=event["end"]["date"] + " - " + event["end"]["time"]
|
|
)
|
|
|
|
@app.route('/<id>/attend')
|
|
@auth.login_required
|
|
def attend(id):
|
|
event = calendar_interface.get_calendar_event(id)
|
|
|
|
user = g.user["username"]
|
|
email = g.user["email"]
|
|
|
|
# only update attendees, now the whole event
|
|
data = {}
|
|
data["attendees"] = event["attendees"]
|
|
|
|
logging.info("attempting to attend event with user: %s %s", email, user)
|
|
calendar_interface.add_attendee(data, user, email)
|
|
|
|
logging.info("Sending data for attendee update:\n%s", json.dumps(data, indent=2))
|
|
calendar_interface.update_calendar_event(id, data)
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
@app.route('/<id>/unattend')
|
|
@auth.login_required
|
|
def unattend(id):
|
|
event = calendar_interface.get_calendar_event(id)
|
|
|
|
user = g.user["username"]
|
|
email = g.user["email"]
|
|
|
|
# only update attendees, now the whole event
|
|
data = {}
|
|
data["attendees"] = event["attendees"]
|
|
|
|
logging.info("attempting to unattend event with user: %s %s", email, user)
|
|
calendar_interface.delte_attendee(data, email)
|
|
|
|
logging.info("Sending data for attendee update:\n%s", json.dumps(data, indent=2))
|
|
calendar_interface.update_calendar_event(id, data)
|
|
|
|
return redirect(url_for('index')) |