adds calendars to sql database, calls backend to remap calendars to color accurate information

This commit is contained in:
Raphael Maenle 2020-04-15 17:43:11 +00:00
parent 19bbf53282
commit 04942268a6
3 changed files with 97 additions and 21 deletions

76
app.py
View File

@ -19,8 +19,9 @@ import requests
# Internal imports # Internal imports
from database.db import init_db_command from database.db import init_db_command
from database.user import User from database.user import User
from database.user import dbCalendar
import caltojson import backend.caltojson as caltojson
import google.oauth2.credentials import google.oauth2.credentials
import google_auth_oauthlib.flow import google_auth_oauthlib.flow
@ -74,6 +75,7 @@ def account():
@app.route("/account") @app.route("/account")
def index(): def index():
if current_user.is_authenticated: if current_user.is_authenticated:
updateCalendars()
return (flask.render_template('account.html', return (flask.render_template('account.html',
username = current_user.name, email = current_user.email, profile_img=current_user.profile_pic username = current_user.name, email = current_user.email, profile_img=current_user.profile_pic
) )
@ -85,39 +87,71 @@ def get_google_provider_cfg():
return requests.get(GOOGLE_DISCOVERY_URL).json() return requests.get(GOOGLE_DISCOVERY_URL).json()
class Calendar: class Calendar:
def __init__(self, name, color): def __init__(self, name, toggle=0, color="#000000"):
self.name = name self.name = name
self.color = color self.color = color
if toggle == 0:
self.toggle = False
else:
self.toggle = True
def calendarsFromDb():
calendars = dbCalendar.getCalendars(current_user.id)
pyCalendars = []
for calendar in calendars:
name = calendar[2]
calId = calendar[1]
toggle = calendar[3]
color = calendar[4]
pyCalendars.append(Calendar(name, toggle, color))
return pyCalendars
@app.route("/calendar") @app.route("/calendar")
@login_required @login_required
def calendar(): def calendar():
ca1 = Calendar("Hightower", "#30ff30") calendars = calendarsFromDb()
ca2 = Calendar("Toast", "#66e230")
calendars = [ca1, ca2]
return flask.render_template('calendar.html', calendars=calendars) return flask.render_template('calendar.html', calendars=calendars)
def getCalendarJson():
if 'credentials' not in flask.session:
return flask.redirect('login/google')
@app.route('/test') # Load credentials from the session.
def test_api_request(): credentials = google.oauth2.credentials.Credentials(
if 'credentials' not in flask.session: **flask.session['credentials'])
return flask.redirect('login/google') todaysCal = caltojson.generateJsonFromCalendarEntries(credentials)
with open('./userinfo/' + current_user.id + '/calendarevents.json', 'w') as outfile:
json.dump(todaysCal, outfile)
return todaysCal
def updateCalendars():
if 'credentials' not in flask.session:
return flask.redirect('login/google')
# Load credentials from the session. # Load credentials from the session.
credentials = google.oauth2.credentials.Credentials( credentials = google.oauth2.credentials.Credentials(
**flask.session['credentials']) **flask.session['credentials'])
todaysCal = caltojson.generateJsonFromCalendarEntries(credentials)
# Save credentials back to session in case access token was refreshed.
# ACTION ITEM: In a production app, you likely want to save these
# credentials in a persistent database instead.
flask.session['credentials'] = credentials_to_dict(credentials)
with open('./userinfo/' + current_user.id + '/calendarevents.json', 'w') as outfile: calendars = caltojson.getCalendarList(credentials)
json.dump(todaysCal, outfile)
return flask.jsonify(todaysCal) for calendar in calendars:
if dbCalendar.getCalendar(current_user.id, calendar.calendarId) == None:
dbCalendar.create(current_user.id, calendar.calendarId, calendar.summary, calendar.color)
print("updated Calendars")
# Save credentials back to session in case access token was refreshed.
# ACTION ITEM: In a production app, you likely want to save these
# credentials in a persistent database instead.
flask.session['credentials'] = credentials_to_dict(credentials)
@app.route("/login/google") @app.route("/login/google")
def login(): def login():

View File

@ -4,3 +4,11 @@ CREATE TABLE user (
email TEXT UNIQUE NOT NULL, email TEXT UNIQUE NOT NULL,
profile_pic TEXT NOT NULL profile_pic TEXT NOT NULL
); );
CREATE TABLE calendar (
usr_id TEXT NOT NULL,
calendar_id TEXT PRIMARY KEY,
name TEXT NOT NULL,
toggle INT NOT NULL,
color TEXT
);

View File

@ -36,3 +36,37 @@ class User(UserMixin):
Path(f"userinfo/{id_}").mkdir(parents=True, exist_ok=True) Path(f"userinfo/{id_}").mkdir(parents=True, exist_ok=True)
class dbCalendar():
def __init__(self, id_, name, toggle, color):
self.usr_id = id_
self.name = name
self.toggle = toggle
self.color = color
@staticmethod
def getCalendars(user_id):
db = get_db()
calendars = db.execute(
"SELECT * FROM calendar WHERE usr_id = ?", (user_id,)
).fetchall()
return calendars
@staticmethod
def getCalendar(user_id, calendar_id):
db = get_db()
calendar = db.execute(
"SELECT * FROM calendar WHERE usr_id = ? AND calendar_id = ?", (user_id, calendar_id,)
).fetchone()
if not calendar:
return None
@staticmethod
def create(user_id, calendar_id, name, color, toggle = False):
db = get_db()
db.execute(
"INSERT INTO calendar (usr_id, calendar_id, name, toggle, color) "
"VALUES (?, ?, ?, ?, ?)",
(user_id, calendar_id, name, toggle, color),
)
db.commit()