2020-04-24 20:23:47 +02:00
|
|
|
from server import db
|
2020-05-24 13:32:42 +02:00
|
|
|
from database.models import User, Calendar
|
2020-04-24 20:23:47 +02:00
|
|
|
from backend import caltojson
|
2020-05-15 16:00:11 +02:00
|
|
|
import os
|
2020-04-24 20:23:47 +02:00
|
|
|
|
|
|
|
import google.oauth2.credentials
|
|
|
|
import json
|
|
|
|
|
|
|
|
class Routine:
|
|
|
|
|
2020-05-27 20:35:46 +02:00
|
|
|
def updateCalendar(self, user, credentials):
|
2020-04-24 20:23:47 +02:00
|
|
|
# check google:
|
|
|
|
if credentials is None:
|
2020-05-24 13:25:15 +02:00
|
|
|
return
|
2020-04-24 20:23:47 +02:00
|
|
|
|
2020-05-15 16:00:11 +02:00
|
|
|
visualCals = []
|
2020-05-27 20:35:46 +02:00
|
|
|
for calendar in user.calendars:
|
2020-05-15 16:00:11 +02:00
|
|
|
if calendar.toggle == 'True':
|
2020-05-18 23:49:26 +02:00
|
|
|
visualCals.append(calendar.calendar_id)
|
2020-05-17 22:58:13 +02:00
|
|
|
googleCreds = google.oauth2.credentials.Credentials(**credentials)
|
2020-05-15 16:00:11 +02:00
|
|
|
calendarjson = caltojson.generateJsonFromCalendarEntries(visualCals, googleCreds)
|
2020-05-24 13:25:15 +02:00
|
|
|
'''
|
2020-05-15 16:00:11 +02:00
|
|
|
directory = 'userinfo/' + user.id + '/'
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
os.makedirs(directory)
|
|
|
|
with open(directory + 'calendarevents.json', 'w', encoding='utf-8') as f:
|
2020-04-24 20:23:47 +02:00
|
|
|
json.dump(calendarjson, f, ensure_ascii=False, indent=4)
|
2020-05-24 13:25:15 +02:00
|
|
|
'''
|
2020-05-27 20:35:46 +02:00
|
|
|
return calendarjson
|
2020-05-24 13:25:15 +02:00
|
|
|
|
|
|
|
|
2020-05-15 16:00:11 +02:00
|
|
|
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}
|
2020-04-24 20:23:47 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
routine = Routine()
|
|
|
|
routine.start()
|