def default_context(self): """ Returns a dictionary containing default context for all render calls """ context = { # add request and session objects to default context 'request': self.request, 'session': self.session, # helper funcs which can be used within templates 'format_timedelta': format_timedelta, 'is_admin': is_admin, 'len': len, 'uri_for': self.uri_for, } # check if user is logged in user = users.get_current_user() if user is not None: # get user from datastore user_key = 'user-' + user.user_id() user_entity = User.get_by_id(user_key) # add user to context context['user'] = user_entity else: context['user'] = None # return default context return context
def dispatch(self): """ Override dispatch() to provide simple auth support """ # make sure user is logged in user = users.get_current_user() if user is None: # redirect user to login page if not logged in redirect_url = users.create_login_url(self.request.path_qs) return self.redirect(redirect_url) # get user entity from datastore user_entity_id = 'user-' + user.user_id() user_entity = User.get_by_id(user_entity_id) # ensure user entity exists and has complete profile if user_entity is None or not user_entity.has_profile: redirect_url = self.uri_for('update-profile') return self.redirect(redirect_url) # we have valid auth so lets make the user_entity available class-wide self.user_entity = user_entity # Dispatch the request. TemplateHandler.dispatch(self)
def get(self): # make sure user is logged in user = users.get_current_user() if user is None: self.abort(401) # make sure this is a valid oauth2 callback code = self.request.get('code', default_value=None) if code is None: return self.abort(400, detail='Not a valid OAuth2 callback') # pull user entity from datastore and set credentials user_key = 'user-' + user.user_id() user_entity = User.get_by_id(user_key) user_entity.set_credentials(code) # redirect to specified URL redirect_url = self.request.get('state') return self.redirect(redirect_url)