Large changes in the seperation of backend and google handler

- backend now takes care of all the recoloring, and communication with database
- google handler takes care of the entire communication with google
- colors selected on the front-end are now translated to the watch

- Calendars in the database now directly save the color the user has set
- only if the event has a different color than the calendar (event color from google is not 0)
  is the event color from google used.
- No more passing around of google color ids, hex colors all the way
This commit is contained in:
2020-05-30 23:05:46 +02:00
parent 98b09bb778
commit 70197ee393
5 changed files with 85 additions and 84 deletions

View File

@ -1,8 +1,8 @@
import google.oauth2.credentials
import google_auth_oauthlib.flow
import googleapiclient.discovery
from googleapiclient.discovery import build
import backend.caltojson as caltojson
from oauthlib.oauth2 import WebApplicationClient
import flask
@ -23,7 +23,8 @@ from flask_login import (
import requests
from database.models import Calendar as dbCalendar
from server import db
from backend import Calendar, Event
# Configuration
class GoogleClient():
def __init__(self):
@ -60,6 +61,7 @@ class GoogleClient():
GC = GoogleClient()
# stuff for OAuth login
def login():
# Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
@ -115,59 +117,82 @@ def deleteAccount(user):
print(result, flush=True)
return
class Calendar:
def __init__(self, name, calendarId, toggle='False', color="#000000"):
self.name = name
self.color = color
self.toggle=toggle
self.calendarId = calendarId
# TODO move this to database
def calendarsFromDb():
pyCalendars = []
for calendar in current_user.calendars:
name = (calendar.name[:16] + '..') if len(calendar.name)> 18 else calendar.name
calendarId = calendar.calendar_id
toggle = calendar.toggle
color = calendar.color
def fetchCalendarEvents(user, calendars, startDate, endDate):
pyCalendars.append(Calendar(name, calendarId, toggle, color))
client_token = GC.build_credentials(user.google_token.token,
user.google_token.refresh_token)
credentials = google.oauth2.credentials.Credentials(**client_token)
return pyCalendars
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'])
# a = flask.session['credentials']
# print(a, flush=True)
# print(current_user.getGoogleCredentials(), flush=True)
if current_user.google_token == None:
return
service = build(GC.API_SERVICE_NAME, GC.API_VERSION, credentials=credentials)
all_events = []
for calendar in calendars:
if calendar.toggle == "True":
event_result = service.events().list(calendarId=calendar.calendar_id,
timeMin=startDate,
timeMax=endDate,
maxResults=10,
singleEvents=True,
orderBy='startTime').execute()
for event in event_result.get('items', []):
# create simple event
name = event.get('summary', '(no Title)')
start = event['start'].get('dateTime')
end = event['end'].get('dateTime')
newEvent = Event(name, start, end)
# handle weird colors from google
color = event.get('colorId')
if color == None:
newEvent.calendarHex = calendar.color
newEvent.evnetColorId = None
else:
newEvent.eventColorId = color
all_events.append(newEvent)
colors = service.colors().get().execute()
return all_events, colors
def fetchCalendars():
# get client api service
client_token = GC.build_credentials(current_user.google_token.token,
current_user.google_token.refresh_token)
credentials = google.oauth2.credentials.Credentials(**client_token)
calendars = caltojson.getCalendarList(credentials)
for calendar in calendars:
if not current_user.hasCalendar(calendar.calendarId):
c = dbCalendar(calendar_id=calendar.calendarId,
name = calendar.summary,
toggle = "False",
color = calendar.color)
db.session.add(c)
current_user.calendars.append(c)
# 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.
# TODO add save updated token to database here
current_user.google_token.token = credentials.token
db.session.commit()
service = build(GC.API_SERVICE_NAME, GC.API_VERSION, credentials=credentials)
# get all calendars and put them into Calendar Class
page_token = None
calendars = []
while True:
calendar_list = service.calendarList().list(pageToken=page_token).execute()
for calendar in calendar_list['items']:
calendars.append(Calendar(name=calendar['summary'],
calendarId=calendar['id'],
color=calendar['colorId']))
page_token = calendar_list.get('nextPageToken')
if not page_token:
break
colors = service.colors().get().execute()
return calendars, colors, credentials.token
def getUserCredentials(user):
credentials = GC.build_credentials(user.google_token.token,
user.google_token.refresh_token)
googleCreds = google.oauth2.credentials.Credentials(**credentials)
return googleCreds
def credentials_to_dict(credentials):