<s> from flask import redirect , session , g , url_for , request , flash , render_template , Blueprint <EOL> from talent_curator import app , _basedir <EOL> from talent_curator . database import db_session <EOL> from talent_curator . apps . google import oauth_client <EOL> from talent_curator . apps . profile import models <EOL> import os <EOL> import requests <EOL> REDIRECT_URI = '<STR_LIT>' <EOL> GOOGLE_USER_INFO = '<STR_LIT>' <EOL> GOOGLE_ACCESS_TOKEN = '<STR_LIT>' <EOL> logger = app . logger <EOL> main_blueprint = Blueprint ( '<STR_LIT>' , __name__ , <EOL> template_folder = os . path . join ( _basedir , '<STR_LIT>' ) ) <EOL> @ main_blueprint . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ main_blueprint . route ( '<STR_LIT>' ) <EOL> def login ( ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> callback = url_for ( '<STR_LIT>' , _external = True ) <EOL> logger . debug ( "<STR_LIT>" , callback ) <EOL> return oauth_client . authorize ( callback = callback ) <EOL> @ oauth_client . tokengetter <EOL> def get_google_token ( token = None ) : <EOL> return session . get ( GOOGLE_ACCESS_TOKEN ) <EOL> @ main_blueprint . route ( REDIRECT_URI ) <EOL> @ oauth_client . authorized_handler <EOL> def oauth_authorized ( resp ) : <EOL> logger . debug ( '<STR_LIT>' , resp ) <EOL> if resp is not None : <EOL> access_token = resp [ GOOGLE_ACCESS_TOKEN ] <EOL> session [ GOOGLE_ACCESS_TOKEN ] = access_token , '<STR_LIT>' <EOL> logger . debug ( '<STR_LIT>' + access_token ) <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' + access_token } <EOL> r = requests . get ( GOOGLE_USER_INFO , headers = headers ) <EOL> logger . debug ( "<STR_LIT>" , r . json ) <EOL> email_address = r . json [ '<STR_LIT:email>' ] <EOL> user = models . User . query . filter_by ( email = email_address ) . first ( ) <EOL> if user is None : <EOL> user = models . User ( email = email_address , name = r . json [ '<STR_LIT:name>' ] , <EOL> first_name = r . json [ '<STR_LIT>' ] , last_name = r . json [ '<STR_LIT>' ] ) <EOL> db_session . add ( user ) <EOL> db_session . commit ( ) <EOL> session [ '<STR_LIT>' ] = user . id <EOL> g . user = user <EOL> logger . debug ( '<STR_LIT>' , user ) <EOL> else : <EOL> logger . debug ( request . args ) <EOL> if request . args and request . args [ '<STR_LIT:error>' ] : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ main_blueprint . route ( '<STR_LIT>' ) <EOL> def logout ( ) : <EOL> session [ GOOGLE_ACCESS_TOKEN ] = None <EOL> session [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> g . user = None <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) </s>
