From f86f15069a936a60bd29c3028ebb105d48b28b4f Mon Sep 17 00:00:00 2001 From: raphael Date: Fri, 3 Apr 2020 10:18:16 +0000 Subject: [PATCH] Generates new google signin users in database and generates json for todays events - imports caltojson from calendarwatch_server - gets the user info from google api - gets user information and compares them to the user database - returns to login page and shows user information as well as 'api test' button - api test button uses 'calendarwatch_server' to get calendars and calculate todays events --- .gitignore | 4 ++-- login/app.py | 24 +++++++++++------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index cf7086d..548259c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # General stuff .swp .cache - +calendarevents.json # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] @@ -144,4 +144,4 @@ cython_debug/ # static files generated from Django application using `collectstatic` media -static \ No newline at end of file +static diff --git a/login/app.py b/login/app.py index 30f6928..bbb622b 100644 --- a/login/app.py +++ b/login/app.py @@ -20,6 +20,7 @@ import requests from db import init_db_command from user import User +import caltojson import google.oauth2.credentials import google_auth_oauthlib.flow @@ -40,10 +41,6 @@ GOOGLE_CLIENT_SECRET = "Hu_YWmKsVKUcLwyeINYzdKfZ" GOOGLE_DISCOVERY_URL = ( "https://accounts.google.com/.well-known/openid-configuration" ) - -API_SERVICE_NAME = 'calendar' -API_VERSION = 'v3' - # Flask app setup app = Flask(__name__) app.secret_key = os.environ.get("SECRET_KEY") or os.urandom(24) @@ -95,8 +92,7 @@ def test_api_request(): credentials = google.oauth2.credentials.Credentials( **flask.session['credentials']) - service = googleapiclient.discovery.build( - API_SERVICE_NAME, API_VERSION, credentials=credentials) + todaysCal = caltojson.generateJsonFromCalendarEntries(credentials) # Save credentials back to session in case access token was refreshed. @@ -104,7 +100,7 @@ def test_api_request(): # credentials in a persistent database instead. flask.session['credentials'] = credentials_to_dict(credentials) - return flask.jsonify("{}") + return flask.jsonify(todaysCal) @app.route("/login") def login(): @@ -151,8 +147,6 @@ def login(): @app.route("/login/callback") def callback(): - print("in callback") - # Specify the state when creating the flow in the callback so that it can # verified in the authorization server response. state = flask.session['state'] @@ -172,17 +166,21 @@ def callback(): flask.session['credentials'] = credentials_to_dict(credentials) session = flow.authorized_session() - print(session.get('https://www.googleapis.com/userinfo/v2/me').json()) + + userinfo = session.get('https://www.googleapis.com/userinfo/v2/me').json() # Create a user in your db with the information provided # by Google user = User( - id_=unique_id, name=users_name, email=users_email, profile_pic=picture + id_=userinfo['id'], + name=userinfo['name'], + email=userinfo['email'], + profile_pic=userinfo['picture'] ) # Doesn't exist? Add it to the database. - if not User.get(unique_id): - User.create(unique_id, users_name, users_email, picture) + if not User.get(user.id): + User.create(user.id, user.name, user.email, user.profile_pic) # Begin user session by logging the user in login_user(user)