82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
import os
|
|
import logging
|
|
import json
|
|
|
|
from flask import Flask, render_template, g, request, url_for, flash, 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 masl token acquire for test purposes (raises if not successful)
|
|
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)
|
|
|
|
if events is None:
|
|
abort(404)
|
|
else:
|
|
return render_template('index.html', events=events, user=g.user)
|
|
|
|
# @app.route('/<int:event_id>') # TODO: access detailed event view
|
|
# def post(post_id):
|
|
# post = get_post(post_id)
|
|
# return render_template('show_event.html', post=post)
|
|
|
|
@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')) |