This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
mandaye/mandaye/response.py

53 lines
1.9 KiB
Python

from mako.lookup import TemplateLookup
from mako.template import Template
from mandaye.config import template_directory
from mandaye.http import HTTPResponse, HTTPHeader
mylookup = TemplateLookup(directories=[template_directory])
def serve_template(templatename, **kwargs):
mytemplate = mylookup.get_template(templatename)
return mytemplate.render(**kwargs)
def _302(location, cookies=None):
headers = HTTPHeader({'Location': [location],
'Content-Type': ['text/html']})
msg = "Reload the page to get source for: %s" % location
return HTTPResponse(302, 'Found', headers, msg, cookies)
def _401(msg):
title='Unauthorized'
headers = HTTPHeader({'Content-Type': ['text/html']})
return HTTPResponse(401, 'Unauthorized', headers,
serve_template("response.html", title=title, body=msg))
def _404(url):
title = "Not Found"
body = "The requested URL %s was not found on this server." % url
headers = HTTPHeader({'Content-Type': ['text/html']})
return HTTPResponse(404, 'Not found', headers,
serve_template("response.html", title=title, body=body))
def _500(msg=None):
if not msg:
msg = "The server encountered an internal error or misconfiguration \
and was unable to completeyour request.Please contact the server \
administrator and inform them of the time the error occurred, and \
anything you might have done that may have caused the error. \
More information about this error may be availablein the server error log."
title = 'Internal Server Error'
headers = HTTPHeader({'Content-Type': ['text/html']})
return HTTPResponse(500, 'Internal Server Error', headers,
serve_template("response.html", title=title, body=msg))
def _502(msg):
title = 'Bad Gateway'
headers = HTTPHeader({'Content-Type': ['text/html']})
return HTTPResponse(502, 'Bad Gateway', headers,
serve_template("response.html", title=title, body=msg))