def handleRequest(self, req): """This is the main request handling routine for all actions. It implements a strategy that sub classes should adhere to by overriding the individual methods of the strategy instead of this handler.""" config = getPyWebMvcConfig(req) try: try: self.prepareRequest(req) actionMapping = self.getActionMapping(req) self.checkAuthentication(req,actionMapping) if not self.authorized(req, actionMapping): return mod_apache.HTTP_FORBIDDEN forward = self.redirectMaybe(req, actionMapping) if not forward: forward = config.globalForwards[actionMapping.id] return self.doForward(forward, req, actionMapping) except: e = sys.exc_info()[1] ret = self.handleExceptionMaybe(req, e) if ret is None: raise else: return ret except ActionNotFoundException: return mod_apache.HTTP_NOT_FOUND except: if isinstance(sys.exc_info()[1], mod_apache.SERVER_RETURN): self.cleanupRequest(req) raise #no handler found or a non-compliant exception was raised. #let mod_python exception handling take over #TODO: change this to simple sets on the request and use req.parent to access req.err_headers_out.add("EXCEPTION",traceback.format_exc()) req.err_headers_out.add("FORMDATA",getFormData(req).encode("utf-8")) return mod_apache.HTTP_INTERNAL_SERVER_ERROR
def href(request, globalForwardId, data=None): """utility function to construct a (server-relative) URL from a global forward id. C{data} can be a list of C{(name, value)} tuples or a dict of C{name -> value} mappings.""" url = getPyWebMvcConfig(request).globalForwards[globalForwardId].getUrl() if data: query = "?" + urllib.urlencode(data) else: query = "" return url + query
def handleExceptionMaybe(self, req, e): """Local Forwards to an L{ErrorHandler<core.ErrorHandler>} for the exception C{e} based on the handlers specified in the application configuration. Returns C{None} when the exception is not handled. """ if isinstance(e, mod_apache.SERVER_RETURN): return None config = getPyWebMvcConfig(req) for handler in config.errorHandlers: if handler.shouldHandle(req, e): forward = handler.handle(req, e) return self.doForward(forward, req, forward.mapping) return None
def getBundle(self, req): config = getPyWebMvcConfig(req) return config.bundleManager.getBundle(req.locale)
def getRequestLocale(self, req): config = getPyWebMvcConfig(req) preferredLanguages = getPreferredLanguages(req) locale = config.bundleManager.negotiateLocale(preferredLanguages) return locale
def getActionMapping(self, req): """Retrieves the L{ActionMapping<core.ActionMapping>} for the given request and raises L{KeyError} when not found.""" config = getPyWebMvcConfig(req) path = self.getPath(req) return config.getActionMappingByPath(path)
def getConfig(self, req): """Retrieves the singleton L{configuration<core.PyWebMvcConfiguration>} object for the application. """ return getPyWebMvcConfig(req)