2020-04-22 22:15:21 +02:00
|
|
|
# Python standard libraries
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
# Third-party libraries
|
|
|
|
import flask
|
2020-04-23 19:11:23 +02:00
|
|
|
from flask import render_template, flash
|
2020-05-24 13:26:41 +02:00
|
|
|
from flask import Flask, redirect, request, url_for, jsonify
|
2020-04-22 22:15:21 +02:00
|
|
|
from flask_login import (
|
|
|
|
LoginManager,
|
|
|
|
current_user,
|
|
|
|
login_required,
|
|
|
|
login_user,
|
|
|
|
logout_user,
|
|
|
|
)
|
|
|
|
import requests
|
|
|
|
|
|
|
|
import server.googleHandler as google
|
|
|
|
|
2020-05-24 13:26:41 +02:00
|
|
|
from backend.Routine import Routine
|
2020-04-22 22:15:21 +02:00
|
|
|
from server import login_manager, app, db
|
2020-05-21 18:33:58 +02:00
|
|
|
from server.forms import LoginForm, RegistrationForm, DeviceForm
|
2020-05-27 20:06:43 +02:00
|
|
|
from database.models import User, Calendar, Device, GoogleToken
|
2020-04-22 22:15:21 +02:00
|
|
|
|
2020-05-10 20:31:42 +02:00
|
|
|
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
|
|
|
|
|
2020-04-22 22:15:21 +02:00
|
|
|
@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')
|
|
|
|
|
2020-05-21 15:38:22 +02:00
|
|
|
@app.route("/view")
|
|
|
|
def view():
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return flask.render_template('login.html')
|
|
|
|
else:
|
|
|
|
return (flask.render_template('view.html'))
|
2020-05-21 18:33:58 +02:00
|
|
|
|
|
|
|
@app.route("/devices", methods=['GET', 'POST'])
|
|
|
|
def devices():
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return flask.render_template('login.html')
|
2020-05-21 15:38:22 +02:00
|
|
|
|
2020-05-21 18:33:58 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-04-22 22:15:21 +02:00
|
|
|
@app.route("/calendar")
|
|
|
|
@login_required
|
|
|
|
def calendar():
|
|
|
|
calendars = google.calendarsFromDb()
|
|
|
|
return flask.render_template('calendar.html', calendars=calendars)
|
|
|
|
|
2020-04-23 19:11:23 +02:00
|
|
|
@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)
|
|
|
|
|
2020-05-21 18:33:58 +02:00
|
|
|
|
2020-04-23 19:11:23 +02:00
|
|
|
@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():
|
2020-05-28 00:05:53 +02:00
|
|
|
user = User(userid=form.username.data,
|
2020-04-23 19:11:23 +02:00
|
|
|
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'))
|
2020-05-21 18:33:58 +02:00
|
|
|
return flask.render_template('register.html', title='Register', form=form)
|
2020-04-23 19:11:23 +02:00
|
|
|
|
2020-05-22 10:47:28 +02:00
|
|
|
@app.route("/delete_account")
|
|
|
|
def deleteAccount():
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return redirect(url_for('account'))
|
2020-05-27 20:06:43 +02:00
|
|
|
# TODO fix google delete account
|
2020-05-28 00:05:53 +02:00
|
|
|
if current_user.google_token != None:
|
|
|
|
google.deleteAccount(current_user)
|
2020-05-27 20:56:23 +02:00
|
|
|
'''
|
2020-05-27 20:45:19 +02:00
|
|
|
for cal in current_user.calendars:
|
|
|
|
db.session.delete(cal)
|
|
|
|
for dev in current_user.devices:
|
|
|
|
db.session.delete(dev)
|
|
|
|
db.session.delete(current_user.google_token)
|
2020-05-27 20:56:23 +02:00
|
|
|
'''
|
2020-05-27 20:06:43 +02:00
|
|
|
db.session.delete(current_user)
|
2020-05-22 10:47:28 +02:00
|
|
|
db.session.commit()
|
2020-05-27 20:06:43 +02:00
|
|
|
logout_user()
|
2020-05-15 16:00:54 +02:00
|
|
|
|
2020-05-22 10:47:28 +02:00
|
|
|
return redirect(url_for('account'))
|
2020-05-15 16:00:54 +02:00
|
|
|
|
2020-04-22 22:15:21 +02:00
|
|
|
@app.route("/login/google")
|
2020-04-23 19:11:23 +02:00
|
|
|
def googlelogin():
|
2020-05-28 00:05:53 +02:00
|
|
|
if current_user.is_authenticated and current_user.google_token.refresh_token != None:
|
2020-05-24 13:26:41 +02:00
|
|
|
return redirect(url_for('account'))
|
|
|
|
|
2020-04-22 22:15:21 +02:00
|
|
|
authorization_url = google.login()
|
|
|
|
|
|
|
|
return flask.redirect(authorization_url)
|
|
|
|
|
|
|
|
@app.route("/login/google/callback")
|
|
|
|
def callback():
|
2020-04-24 19:54:56 +02:00
|
|
|
session, credentials = google.verifyResponse()
|
2020-04-22 22:15:21 +02:00
|
|
|
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.
|
2020-05-27 20:06:43 +02:00
|
|
|
if not db.session.query(User).filter(User.userid==userinfo['id']).first():
|
|
|
|
gc = GoogleToken(token=credentials.get("token"),
|
|
|
|
refresh_token=credentials.get("refresh_token"))
|
|
|
|
db.session.add(gc)
|
2020-04-22 22:15:21 +02:00
|
|
|
newser = User(
|
2020-05-27 20:06:43 +02:00
|
|
|
|
|
|
|
userid=userinfo['id'],
|
2020-04-22 22:15:21 +02:00
|
|
|
username=userinfo['name'],
|
|
|
|
email=userinfo['email'],
|
|
|
|
profile_pic=userinfo['picture'],
|
2020-05-27 20:06:43 +02:00
|
|
|
password_hash="",
|
|
|
|
google_token = gc
|
2020-04-22 22:15:21 +02:00
|
|
|
)
|
|
|
|
db.session.add(newser)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-05-27 20:06:43 +02:00
|
|
|
user = db.session.query(User).filter(User.userid==userinfo['id']).first()
|
2020-04-22 22:15:21 +02:00
|
|
|
|
|
|
|
# Begin user session by logging the user in
|
|
|
|
login_user(user)
|
2020-05-27 20:06:43 +02:00
|
|
|
|
2020-04-22 22:15:21 +02:00
|
|
|
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}
|
|
|
|
|
|
|
|
|
2020-05-24 13:26:41 +02:00
|
|
|
@app.route("/userinfo/<path:device>/calendarevents.json")
|
|
|
|
def downloader(device):
|
|
|
|
path = "/home/calendarwatch/userinfo/" + device + "/"
|
|
|
|
# return flask.send_from_directory(path, "calendarevents.json")
|
2020-05-27 20:36:05 +02:00
|
|
|
request_user = db.session.query(User).filter(User.userid==device).first()
|
|
|
|
print(device, flush=True)
|
2020-05-24 13:26:41 +02:00
|
|
|
if request_user == None:
|
|
|
|
return jsonify(kind="unregistered")
|
|
|
|
|
|
|
|
routine = Routine()
|
2020-05-27 20:36:05 +02:00
|
|
|
|
|
|
|
client_token = google.GC.build_credentials(request_user.google_token.token,
|
|
|
|
request_user.google_token.refresh_token)
|
|
|
|
calendarjson = routine.updateCalendar(request_user, client_token)
|
|
|
|
return jsonify(calendarjson)
|
2020-05-24 13:26:41 +02:00
|
|
|
|
|
|
|
@app.route("/devicefingerprint.json")
|
|
|
|
def generateDeviceFingerprint():
|
|
|
|
# Create Three Random Words
|
|
|
|
# check not in Device Database
|
|
|
|
# Save as new Device
|
|
|
|
# Send to User
|
|
|
|
return jsonify(deviceId="Carrot-Enamel-Storm")
|
2020-04-22 22:15:21 +02:00
|
|
|
|
|
|
|
# POST
|
|
|
|
|
|
|
|
@app.route('/calendar', methods = ['POST', 'DELETE'])
|
|
|
|
@login_required
|
|
|
|
def user():
|
|
|
|
if request.method == 'POST':
|
2020-05-18 23:52:18 +02:00
|
|
|
calId = request.json.get('calendar_id')
|
|
|
|
color = request.json.get('color', None)
|
|
|
|
toggle = request.json.get('toggle', None)
|
2020-04-22 22:15:21 +02:00
|
|
|
|
2020-05-18 23:52:18 +02:00
|
|
|
print(request.json, flush=True)
|
2020-04-22 22:15:21 +02:00
|
|
|
if color != None:
|
2020-05-27 20:36:05 +02:00
|
|
|
current_user.updateCalendar(calId, color=color)
|
2020-04-22 22:15:21 +02:00
|
|
|
if toggle != None:
|
2020-05-27 20:36:05 +02:00
|
|
|
current_user.updateCalendar(calId, toggle=toggle)
|
2020-04-22 22:15:21 +02:00
|
|
|
# toggle specific calendar of user
|
2020-05-18 23:52:18 +02:00
|
|
|
|
2020-04-22 22:15:21 +02:00
|
|
|
elif request.method == 'DELETE':
|
|
|
|
# do nothing
|
|
|
|
return 'NONE'
|
|
|
|
else:
|
|
|
|
# POST Error 405
|
|
|
|
print("405")
|
|
|
|
|
|
|
|
return 'OK'
|