Commit | Line | Data |
---|---|---|
3614ca83 | 1 | from . import dispatch, proto, env |
7bcaef26 | 2 | from .sp import xhtml |
ef04ec87 | 3 | h = xhtml.cons() |
dc7e5d54 FT |
4 | |
5 | __all__ = ["skeleton", "skelfor", "setskel", "usererror"] | |
6 | ||
7 | class skeleton(object): | |
3162cb89 FT |
8 | def page(self, req, title, *content): |
9 | return xhtml.forreq(req, h.html(self.head(req, title), h.body(*content))) | |
dc7e5d54 | 10 | |
3162cb89 | 11 | def head(self, req, title): |
ef04ec87 | 12 | return xhtml.head(title=title) |
dc7e5d54 | 13 | |
3162cb89 FT |
14 | def error(self, req, message, *detail): |
15 | return self.page(req, message, h.h1(message), h.p(*detail)) | |
dc7e5d54 | 16 | |
3162cb89 FT |
17 | def message(self, req, message, *detail): |
18 | return self.page(req, message, h.h1(message), h.p(*detail)) | |
dc7e5d54 | 19 | |
1f61bf31 | 20 | defskel = env.var(skeleton()) |
dc7e5d54 FT |
21 | |
22 | def getskel(req): | |
1f61bf31 | 23 | return [defskel.val] |
dc7e5d54 FT |
24 | def skelfor(req): |
25 | return req.item(getskel)[0] | |
26 | def setskel(req, skel): | |
27 | req.item(getskel)[0] = skel | |
28 | ||
29 | class usererror(dispatch.restart): | |
ef04ec87 | 30 | def __init__(self, message, *detail): |
c33f2d6c | 31 | super().__init__() |
dc7e5d54 FT |
32 | self.message = message |
33 | self.detail = detail | |
34 | ||
35 | def handle(self, req): | |
3162cb89 | 36 | return skelfor(req).error(req, self.message, *self.detail) |
dc7e5d54 FT |
37 | |
38 | class message(dispatch.restart): | |
7bcaef26 | 39 | def __init__(self, message, *detail): |
c33f2d6c | 40 | super().__init__() |
dc7e5d54 FT |
41 | self.message = message |
42 | self.detail = detail | |
43 | ||
44 | def handle(self, req): | |
98cc090c | 45 | return skelfor(req).message(req, self.message, *self.detail) |
dc7e5d54 FT |
46 | |
47 | class httperror(usererror): | |
9bc70dab | 48 | def __init__(self, status, message=None, detail=None): |
dc7e5d54 FT |
49 | if message is None: |
50 | message = proto.statusinfo[status][0] | |
51 | if detail is None: | |
ef04ec87 | 52 | detail = (proto.statusinfo[status][1],) |
7bcaef26 | 53 | super().__init__(message, *detail) |
dc7e5d54 FT |
54 | self.status = status |
55 | ||
56 | def handle(self, req): | |
57 | req.status(self.status, self.message) | |
c33f2d6c | 58 | return super().handle(req) |
dc7e5d54 FT |
59 | |
60 | class notfound(httperror): | |
61 | def __init__(self): | |
c33f2d6c | 62 | return super().__init__(404) |
4bd573f9 FT |
63 | |
64 | class redirect(dispatch.restart): | |
d0034dee FT |
65 | bases = {"url": proto.requrl, |
66 | "script": proto.scripturl, | |
67 | "site": proto.siteurl} | |
68 | ||
9bc70dab | 69 | def __init__(self, url, status=303, base="url"): |
c33f2d6c | 70 | super().__init__() |
4bd573f9 FT |
71 | self.url = url |
72 | self.status = status | |
d0034dee FT |
73 | self.bases[base] |
74 | self.base = base | |
4bd573f9 FT |
75 | |
76 | def handle(self, req): | |
77 | req.status(self.status, "Redirect") | |
d0034dee | 78 | req.ohead["Location"] = proto.appendurl(self.bases[self.base](req), self.url) |
a7b35f84 | 79 | req.ohead["Content-Length"] = 0 |
4bd573f9 | 80 | return [] |
1864be32 FT |
81 | |
82 | class unmodified(dispatch.restart): | |
83 | def handle(self, req): | |
84 | req.status(304, "Not Modified") | |
85 | req.ohead["Content-Length"] = "0" | |
86 | return [] |