def index(request): access_token = request.session.get('access_token', None) if access_token is None: # if access_token is NOT in session, kickoff the oauth exchange client = Client() url = client.authorization_url( client_id=os.environ.get('STRAVA_CLIENT_ID', None), redirect_uri='http://' + settings.ALLOWED_HOSTS[0] + '/authorization' # redirect_uri='http://127.0.0.1:8000/authorization' ) context = {'auth_url': url} return render(request, 'index.html', context) # otherwise, load authorized user client = Client(access_token=access_token) athlete_id = request.session.get('athlete_id', None) if athlete_id is None: # if athlete is NOT already in session, get athlete # (attempting to reduce calls to the API) athlete = client.get_athlete() request.session['athlete_id'] = athlete.id request.session['firstname'] = athlete.firstname try: Athlete.objects.get(strava_id=athlete.id) except Athlete.DoesNotExist: new_athlete = Athlete() new_athlete.strava_id = athlete.id new_athlete.first_name = athlete.firstname new_athlete.last_name = athlete.lastname new_athlete.city = athlete.city new_athlete.state = athlete.state new_athlete.country = athlete.country new_athlete.save() return render(request, 'welcome_landing.html')