def student_signin(service, access_token=None, access_token_secret=None, **kwarg): if service == 'twitter': response = twitter.get( 'https://api.twitter.com/1/account/verify_credentials.json', access_token=access_token, access_token_secret=access_token_secret ) data = response.content handle = data['screen_name'] serviceId = data['id_str'] name = data['name'] email = '' else: serviceId = kwarg['values']['facebook_id'] handle = None access_token = kwarg['values']['access_token'] name = kwarg['values']['name'] email = kwarg['values']['email'] # Determine if this user already exists in our database rows = db.query('SELECT * FROM student_client WHERE service = %s AND serviceId = %s', (service, serviceId)) if len(rows) is 0: # New student, create account guid = str(uuid()) db.query( 'INSERT INTO student_client (guid, name, email, service, handle, serviceId, oauthToken, oauthSecret) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', (guid, name, email, service, handle, serviceId, access_token, access_token_secret) )
def p_signin_twitter(access_token, access_token_secret): # Get some general information about this user response = twitter.get( 'https://api.twitter.com/1/account/verify_credentials.json', access_token=access_token, access_token_secret=access_token_secret ) data = response.content name = data['name'] twitter_id = data['id_str'] twitter_username = data['screen_name'] guid = '' # Determine if this user already exists in our database rows = db.query('SELECT * FROM psychic WHERE twitterId = %s', twitter_id) if len(rows) is 0: # New user, create account guid = str(uuid()) db.query( 'INSERT INTO psychic (guid, fullName, twitterUsername, twitterId, twitterAccessToken, twitterAccessTokenSecret) VALUES (%s, %s, %s, %s, %s, %s)', (guid, name, twitter_username, twitter_id, access_token, access_token_secret) ) else: # Existing user, set the guid guid = rows[0]['guid'] # Set info so we know who is signed in session['access_token'] = access_token session['access_token_secret'] = access_token_secret session['psychic_guid'] = guid # Issue the user a token token = str(uuid()) db.query('INSERT INTO access_token (guid, psychicGuid) VALUES (%s, %s)', (token, guid)) return token, guid