def get(self, *args, **kwargs): # get url to redirect to after updating next_url = self.request.get('next_url', default_value='/') # check if next_url is relative or absolute if bool(urlparse.urlparse(next_url).scheme): # abort if absolute url is given return self.abort(400, detail="Only relative URLs allowed") # make sure user is logged in user = users.get_current_user() if user is None: # redirect user to login page if not logged in login_url = users.create_login_url(self.request.path_qs) return self.redirect(login_url) # get or create user entity in datastore user_key = 'user-' + user.user_id() user_entity = User.get_or_insert(user_key) # check if the user entity has credentials if not user_entity.has_credentials(): # create flow to begin oauth dance flow = user_entity.get_flow() flow.params['state'] = self.request.path_qs # get oauth2 redirect url auth_url = flow.step1_get_authorize_url() # redirect to El Goog return self.redirect(auth_url) # check if the user has a fully populated profile if not user_entity.has_profile: try: profile = self.call_api(user_entity.access_token) except UnauthorisedError: # refresh the auth token if not authorised user_entity.refresh_auth() profile = self.call_api(user_entity.access_token) # save profile to user model self.update_profile(user_entity, profile, user.email()) # check if the user has a fully populated profile after updating, if # not we'll dump the user out to a page telling them we couldn't # authorise or retrieve their profile if not user_entity.has_profile: redirect_url = self.uri_for('profile-update-error') return self.redirect(redirect_url) # return to the url we came here from initially return self.redirect(next_url)