1 from . import dispatch, proto, env
5 __all__ = ["skeleton", "skelfor", "setskel", "usererror"]
7 class skeleton(object):
8 def page(self, req, title, *content):
9 return xhtml.forreq(req, h.html(self.head(req, title), h.body(*content)))
11 def head(self, req, title):
12 return xhtml.head(title=title)
14 def error(self, req, message, *detail):
15 return self.page(req, message, h.h1(message), h.p(*detail))
17 def message(self, req, message, *detail):
18 return self.page(req, message, h.h1(message), h.p(*detail))
20 defskel = env.var(skeleton())
25 return req.item(getskel)[0]
26 def setskel(req, skel):
27 req.item(getskel)[0] = skel
29 class usererror(dispatch.restart):
30 def __init__(self, message, *detail):
32 self.message = message
35 def handle(self, req):
36 return skelfor(req).error(req, self.message, *self.detail)
38 class message(dispatch.restart):
39 def __init__(self, message, *detail):
41 self.message = message
44 def handle(self, req):
45 return skelfor(req).message(req, self.message, *self.detail)
47 class httperror(usererror):
48 def __init__(self, status, message=None, detail=None):
50 message = proto.statusinfo[status][0]
52 detail = (proto.statusinfo[status][1],)
53 super().__init__(message, *detail)
56 def handle(self, req):
57 req.status(self.status, self.message)
58 return super().handle(req)
60 class notfound(httperror):
62 return super().__init__(404)
64 class redirect(dispatch.restart):
65 bases = {"url": proto.requrl,
66 "script": proto.scripturl,
67 "site": proto.siteurl}
69 def __init__(self, url, status=303, base="url"):
76 def handle(self, req):
77 req.status(self.status, "Redirect")
78 req.ohead["Location"] = proto.appendurl(self.bases[self.base](req), self.url)
79 req.ohead["Content-Length"] = 0
82 class unmodified(dispatch.restart):
83 def handle(self, req):
84 req.status(304, "Not Modified")
85 req.ohead["Content-Length"] = "0"