From 04942268a697d806d9f77f56755ac4d8dfeb0534 Mon Sep 17 00:00:00 2001 From: raphael Date: Wed, 15 Apr 2020 17:43:11 +0000 Subject: [PATCH] adds calendars to sql database, calls backend to remap calendars to color accurate information --- app.py | 76 ++++++++++++++++++++++++++++++++------------- database/schema.sql | 8 +++++ database/user.py | 34 ++++++++++++++++++++ 3 files changed, 97 insertions(+), 21 deletions(-) diff --git a/app.py b/app.py index fc6a3ef..fd476f6 100644 --- a/app.py +++ b/app.py @@ -19,8 +19,9 @@ import requests # Internal imports from database.db import init_db_command from database.user import User +from database.user import dbCalendar -import caltojson +import backend.caltojson as caltojson import google.oauth2.credentials import google_auth_oauthlib.flow @@ -74,6 +75,7 @@ def account(): @app.route("/account") def index(): if current_user.is_authenticated: + updateCalendars() return (flask.render_template('account.html', 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() class Calendar: - def __init__(self, name, color): + def __init__(self, name, toggle=0, color="#000000"): self.name = name 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") @login_required def calendar(): - ca1 = Calendar("Hightower", "#30ff30") - ca2 = Calendar("Toast", "#66e230") - calendars = [ca1, ca2] + calendars = calendarsFromDb() return flask.render_template('calendar.html', calendars=calendars) +def getCalendarJson(): + if 'credentials' not in flask.session: + return flask.redirect('login/google') -@app.route('/test') -def test_api_request(): - if 'credentials' not in flask.session: - return flask.redirect('login/google') + # Load credentials from the session. + credentials = google.oauth2.credentials.Credentials( + **flask.session['credentials']) + 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. - credentials = google.oauth2.credentials.Credentials( - **flask.session['credentials']) + credentials = google.oauth2.credentials.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: - json.dump(todaysCal, outfile) - return flask.jsonify(todaysCal) + calendars = caltojson.getCalendarList(credentials) + + 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") def login(): diff --git a/database/schema.sql b/database/schema.sql index 5c71573..5846c52 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -4,3 +4,11 @@ CREATE TABLE user ( email TEXT UNIQUE 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 +); diff --git a/database/user.py b/database/user.py index 539fb2e..4033423 100644 --- a/database/user.py +++ b/database/user.py @@ -36,3 +36,37 @@ class User(UserMixin): 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()