Raphael Maenle
3c6d950bbc
- form added to push new device id to backend - device added to db model (needs to be pushed still) - form return right now just prints - design for device list created, still needs some updates
194 lines
5.6 KiB
Python
194 lines
5.6 KiB
Python
# Python standard libraries
|
|
import json
|
|
import os
|
|
import sqlite3
|
|
|
|
# Third-party libraries
|
|
import flask
|
|
from flask import render_template, flash
|
|
from flask import Flask, redirect, request, url_for
|
|
from flask_login import (
|
|
LoginManager,
|
|
current_user,
|
|
login_required,
|
|
login_user,
|
|
logout_user,
|
|
)
|
|
import requests
|
|
|
|
import server.googleHandler as google
|
|
|
|
from server import login_manager, app, db
|
|
from server.forms import LoginForm, RegistrationForm, DeviceForm
|
|
from server.models import User, Calendar, Device
|
|
|
|
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
|
|
|
|
@app.route("/")
|
|
def account():
|
|
return flask.redirect('account')
|
|
|
|
@app.route("/account")
|
|
def index():
|
|
if current_user.is_authenticated:
|
|
google.updateCalendars()
|
|
return (flask.render_template('account.html',
|
|
username = current_user.username, email = current_user.email, profile_img=current_user.profile_pic
|
|
)
|
|
)
|
|
else:
|
|
return flask.render_template('login.html')
|
|
|
|
@app.route("/view")
|
|
def view():
|
|
if not current_user.is_authenticated:
|
|
return flask.render_template('login.html')
|
|
else:
|
|
return (flask.render_template('view.html'))
|
|
|
|
@app.route("/devices", methods=['GET', 'POST'])
|
|
def devices():
|
|
if not current_user.is_authenticated:
|
|
return flask.render_template('login.html')
|
|
|
|
device = Device()
|
|
device.deviceId="Anthon-Mouse-Car"
|
|
devices = [device]
|
|
form = DeviceForm()
|
|
if form.validate_on_submit():
|
|
print(form.deviceId.data, flush=True)
|
|
# TODO add device to database here
|
|
|
|
return flask.render_template('devices.html', devices=devices, form=form)
|
|
|
|
|
|
@app.route("/calendar")
|
|
@login_required
|
|
def calendar():
|
|
calendars = google.calendarsFromDb()
|
|
return flask.render_template('calendar.html', calendars=calendars)
|
|
|
|
@app.route('/login/email', methods=['GET', 'POST'])
|
|
def emaillogin():
|
|
if current_user.is_authenticated:
|
|
return redirect(url_for('account') )
|
|
form = LoginForm()
|
|
|
|
if form.validate_on_submit():
|
|
user = User.query.filter_by(username=form.username.data).first()
|
|
if user is None or not user.checkPassword(form.password.data):
|
|
flash('invalid username or password')
|
|
return redirect(url_for('emaillogin'))
|
|
login_user(user, remember=form.remember_me.data)
|
|
return redirect(url_for('account'))
|
|
return render_template('emaillogin.html', title='Sign In', form=form)
|
|
|
|
|
|
@app.route('/register', methods=['GET', 'POST'])
|
|
def register():
|
|
if current_user.is_authenticated:
|
|
return redirect(url_for('account'))
|
|
form = RegistrationForm()
|
|
if form.validate_on_submit():
|
|
user = User(id=form.username.data,
|
|
username=form.username.data,
|
|
email=form.email.data)
|
|
user.setPassword(form.password.data)
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
flash('Congratulations, you are now a registered user!')
|
|
return redirect(url_for('emaillogin'))
|
|
return flask.render_template('register.html', title='Register', form=form)
|
|
|
|
@app.route("/test")
|
|
def testAPI():
|
|
if current_user.is_authenticated:
|
|
google.updateCalendars()
|
|
return redirect('/account')
|
|
|
|
|
|
|
|
@app.route("/login/google")
|
|
def googlelogin():
|
|
authorization_url = google.login()
|
|
|
|
return flask.redirect(authorization_url)
|
|
|
|
@app.route("/login/google/callback")
|
|
def callback():
|
|
session, credentials = google.verifyResponse()
|
|
userinfo = session.get('https://www.googleapis.com/userinfo/v2/me').json()
|
|
|
|
# Create a user in your db with the information provided
|
|
# by Google
|
|
|
|
# Doesn't exist? Add it to the database.
|
|
if not User.query.get(userinfo['id']):
|
|
newser = User(
|
|
id=userinfo['id'],
|
|
username=userinfo['name'],
|
|
email=userinfo['email'],
|
|
profile_pic=userinfo['picture'],
|
|
password_hash=""
|
|
)
|
|
db.session.add(newser)
|
|
db.session.commit()
|
|
|
|
user = User.query.get(userinfo['id'])
|
|
|
|
# Begin user session by logging the user in
|
|
print("login:" + user.id)
|
|
|
|
login_user(user)
|
|
# TODO currently not using the credentials anymore
|
|
if user.getGoogleCredentials() is None:
|
|
user.setGoogleCredentials(credentials)
|
|
return flask.redirect(flask.url_for('index'))
|
|
|
|
@app.route("/logout")
|
|
@login_required
|
|
def logout():
|
|
logout_user()
|
|
return redirect(url_for("index"))
|
|
|
|
def credentials_to_dict(credentials):
|
|
return {'token': credentials.token,
|
|
'refresh_token': credentials.refresh_token,
|
|
'token_uri': credentials.token_uri,
|
|
'client_id': credentials.client_id,
|
|
'client_secret': credentials.client_secret,
|
|
'scopes': credentials.scopes}
|
|
|
|
|
|
@app.route("/userinfo/<path:user>/calendarevents.json")
|
|
def downloader(user):
|
|
path = "/home/calendarwatch/userinfo/" + user + "/"
|
|
print(path, flush=True)
|
|
return flask.send_from_directory(path, "calendarevents.json")
|
|
|
|
# POST
|
|
|
|
@app.route('/calendar', methods = ['POST', 'DELETE'])
|
|
@login_required
|
|
def user():
|
|
if request.method == 'POST':
|
|
calId = request.json.get('calendar_id')
|
|
color = request.json.get('color', None)
|
|
toggle = request.json.get('toggle', None)
|
|
|
|
print(request.json, flush=True)
|
|
if color != None:
|
|
Calendar.updateCalendar(current_user.id, calId, color=color)
|
|
if toggle != None:
|
|
Calendar.updateCalendar(current_user.id, calId, toggle=toggle)
|
|
# toggle specific calendar of user
|
|
|
|
elif request.method == 'DELETE':
|
|
# do nothing
|
|
return 'NONE'
|
|
else:
|
|
# POST Error 405
|
|
print("405")
|
|
|
|
return 'OK'
|