diff --git a/Routine.py b/Routine.py index 2e01338..b4219ee 100644 --- a/Routine.py +++ b/Routine.py @@ -1,6 +1,7 @@ from server import db -from server.models import User +from server.models import User, Calendar from backend import caltojson +import os import google.oauth2.credentials import json @@ -19,14 +20,29 @@ class Routine: if credentials is None: continue - credentials = google.oauth2.credentials.Credentials(**credentials) - calendarjson = caltojson.generateJsonFromCalendarEntries(credentials) - - user.setJson(calendarjson) - - with open('userinfo/'+ user.id +'/calendarevents.json', 'w', encoding='utf-8') as f: - json.dump(calendarjson, f, ensure_ascii=False, indent=4) + visualCals = [] + calendars = db.session.query(Calendar).filter(Calendar.usr_id==user.id).all() + for calendar in calendars: + if calendar.toggle == 'True': + visualCals.append(calendar.name) + googleCreds = google.oauth2.credentials.Credentials(**credentials) + calendarjson = caltojson.generateJsonFromCalendarEntries(visualCals, googleCreds) + user.setJson(calendarjson) + print(calendarjson) + directory = 'userinfo/' + user.id + '/' + if not os.path.exists(directory): + os.makedirs(directory) + with open(directory + 'calendarevents.json', 'w', encoding='utf-8') as f: + json.dump(calendarjson, f, ensure_ascii=False, indent=4) + +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} if __name__ == "__main__": routine = Routine() diff --git a/caltojson.py b/caltojson.py index e54ad87..6412a31 100644 --- a/caltojson.py +++ b/caltojson.py @@ -12,7 +12,6 @@ from google.auth.transport.requests import Request SCOPES = ["https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/calendar.readonly", "openid"] API_SERVICE_NAME = 'calendar' API_VERSION = 'v3' -visibleList = ['Hightower', 'Home', 'Office', 'Life', 'Social', 'Grey'] class Event: def __init__(self, name_, start_, end_): @@ -116,17 +115,17 @@ def getCalendars(service): return calendars -def purgeCalendars(calendars): +def purgeCalendars(calendars, visibleCalendars): purged = [] for calendar in calendars: - if calendar.summary in visibleList: + if calendar.summary in visibleCalendars: purged.append(calendar) return purged -def getCalendarEvents(service, startDate, endDate): +def getCalendarEvents(service, visibleCalendars, startDate, endDate): calendars = getCalendars(service) - calendars = purgeCalendars(calendars) + calendars = purgeCalendars(calendars, visibleCalendars) all_events = [] @@ -237,7 +236,7 @@ def getCalendarList(credentials = None): calendar.color = fromColorIdGetColor(calendar.color, colormap, colors) return calendars -def generateJsonFromCalendarEntries(credentials = None): +def generateJsonFromCalendarEntries(visibleCalendars, credentials = None): # create service if credentials == None: @@ -249,7 +248,7 @@ def generateJsonFromCalendarEntries(credentials = None): today = now.replace(hour=0, minute=0, second = 0).isoformat() tomorrow = now.replace(hour=23, minute=59, second=59).isoformat() # + '+01:00' - allEvents = getCalendarEvents(service, today, tomorrow) + allEvents = getCalendarEvents(service, visibleCalendars, today, tomorrow) colorizeEvents(allEvents, service) # if not events: