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

This commit is contained in:
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
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():