def not_found(self, req): """ Use this when you KNOW you should reply with a 404. Otherwise, just return None out of the route method and let the NotFound handler catch the request. """ #Determine what we return based on request type if req.is_AJAX: resp = Response.plain("404 NOT FOUND") if req.is_JSON: resp = Response.json(dict( reply_timestamp=datetime.datetime.now(), message="404 NOT FOUND '{0}'".format(req.line_one), success=False)) else: resp = Response.tmpl(self.four_zero_four_template) resp.status = '404 NOT FOUND' return resp
def prompt_for_login(self, req): # if ajax request, relative redirect won't work well # throw 401 error for now until we can figure out # better way to do it. # 401 error will be caught by jquery ajax error handler if req.is_AJAX: resp = Response.plain("You have to log in first!") resp.status = '401 UNAUTHORIZED' if req.is_JSON: resp = Response.json(dict( reply_timestamp=datetime.datetime.now(), message="You have to log in first!", success=False)) resp.status = '401 UNAUTHORIZED' else: resp = Response.relative_redirect('/login', 'You have to log in first!') # Redirect back to the page the person is hitting right now. resp.set_redirect_cookie(req.address_bar) return resp
def only_allow_superusers(handle_method, self, req): """ Add this to a handle method like this:: @Handler.only_allow_superusers def handle(self, req): ... And then, if the request isn't from a signed-in superuser, they'll get a JSON reply below. If the request is from a signed-in superuser, then your handle method is normal. """ if not req.user or not req.user.is_superuser: return Response.json(dict( message="Sorry, superusers only!", success=False, reply_timestamp=datetime.datetime.now())) else: return handle_method(self, req)
def not_found(self, req): """ Use this when you KNOW you should reply with a 404. Otherwise, just return None out of the route method and let the NotFound handler catch the request. """ #Determine what we return based on request type if req.is_AJAX: resp = Response.plain("404 NOT FOUND") else: resp = Response.tmpl(self.four_zero_four_template) resp.status = '404 NOT FOUND' return resp
def f(req): resp = Response.relative_redirect('/login', message) resp.set_redirect_cookie(redirect_location) return resp
def __call__(self, environ, start_response): """ This is the WSGI app interface. Every time a request hits gunicorn, this method fires. """ try: req = self.request_class( self.pgconn, self.config_wrapper, environ) # TODO: Figure out if there is some more elegant approach to # making the request object visible in the template. self.jinja2_environment.globals['request'] = req self.jinja2_environment.globals['req'] = req log.info('Got request {0} {1}'.format( req.REQUEST_METHOD, req.path_and_qs)) handle_function = self.dispatch(req) resp = handle_function(req) if not isinstance(resp, Response): raise Exception("Handler didn't return a response object!") # Maybe if the route method returned self, rather than # self.handle, at this point, we could do a test like # # if callable(getattr(h, 'after_handle', None)) # # and then all this stuff could be defined on the # handler.Handler class rather than in here. # TODO: make this happen as an automatic side effect of # reading the data, so that there is absolutely no risk at # all of forgetting to do this. if req.news_message_cookie_popped: resp.mark_news_message_as_expired() # Update the signed-in user's session expires column. if req.user: new_expires_time = req.session.maybe_update_session_expires_time( self.pgconn) self.pgconn.commit() start_response(resp.status, resp.headers) log.info('Replying with status %s.\n' % resp.status) if resp.status.startswith('30'): log.info('Redirecting to {0}.'.format(resp.headers)) return resp.body except Exception as ex: self.pgconn.rollback() log.critical(ex, exc_info=1) if self.cw.launch_debugger_on_error: raise else: log.critical('address bar: {0}'.format(req.address_bar)) log.critical('post body: {0}'.format(req.wz_req.form)) log.critical(environ) if req.is_JSON: resp = Response.json(dict( reply_timestamp=datetime.datetime.now(), message="Error encountered '{0}'".format(ex), success=False)) resp.status = '500 ERROR' start_response(resp.status, resp.headers) log.info('Replying with status %s.\n' % resp.status) return resp.body else: start_response( '500 ERROR', [('Content-Type', 'text/html; charset=utf-8')], sys.exc_info()) s = self.error_page.render() return [s.encode('utf8')]