calendarwatch_backend/Routine.py
Raphael Maenle 804ab33a6d updates color generation
- hopefully fixes a small bug that fucks up colors
- fixes generation when saving calendar colors
2020-05-17 22:58:13 +02:00

49 lines
1.7 KiB
Python

from server import db
from server.models import User, Calendar
from backend import caltojson
import os
import google.oauth2.credentials
import json
class Routine:
def start(self, time=10):
self.updateCalendars()
def updateCalendars(self):
users = User.query.all()
for user in users:
# check google:
credentials = user.getGoogleCredentials()
if credentials is None:
continue
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)
print(credentials_to_dict(googleCreds), flush=True)
calendarjson = caltojson.generateJsonFromCalendarEntries(visualCals, googleCreds)
user.setJson(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()
routine.start()