|
1 |
|
2 """ |
|
3 Handling page requests |
|
4 """ |
|
5 |
|
6 # for filesystem ops |
|
7 import os, os.path |
|
8 import time |
|
9 |
|
10 from lib import http, handler, template, config |
|
11 |
|
12 import menu |
|
13 |
|
14 class PageError (http.ResponseError) : |
|
15 """ |
|
16 Error looking up/handling a page |
|
17 """ |
|
18 |
|
19 pass |
|
20 |
|
21 # XXX: should inherit from PageInfo |
|
22 class Page (handler.RequestHandler) : |
|
23 """ |
|
24 This object represents the information about our attempt to render some specific page |
|
25 """ |
|
26 |
|
27 def __init__ (self, fs, url, path, basename, url_tail, charset='utf8') : |
|
28 """ |
|
29 Initialize the page at the given location |
|
30 |
|
31 @param fs the FilesysteMapper |
|
32 @param url the URL leading to this page |
|
33 @param path the filesystem path to this page's file |
|
34 @param basename the filesystem name of this page's file, without the file extension |
|
35 @param url_trail trailing URL for this page |
|
36 @param charset file charset |
|
37 """ |
|
38 |
|
39 # store |
|
40 self.fs = fs |
|
41 self.url = url |
|
42 self.path = path |
|
43 self.basename = basename |
|
44 self.url_tail = url_tail |
|
45 self.charset = charset |
|
46 |
|
47 # sub-init |
|
48 self._init() |
|
49 |
|
50 def _init (self) : |
|
51 """ |
|
52 Do initial data loading, etc |
|
53 """ |
|
54 |
|
55 pass |
|
56 |
|
57 @property |
|
58 def title (self) : |
|
59 """ |
|
60 Return the page's title |
|
61 |
|
62 Defaults to the retreiving the page title from page_list, or basename in Titlecase. |
|
63 """ |
|
64 |
|
65 # lookup in PageTree |
|
66 page_info = self.fs.tree.get_page(self.url) |
|
67 |
|
68 # fallback to titlecase |
|
69 if page_info : |
|
70 title = page_info.title |
|
71 |
|
72 else : |
|
73 title = self.basename.title() |
|
74 |
|
75 return title |
|
76 |
|
77 @property |
|
78 def content (self) : |
|
79 """ |
|
80 Return the page content as a string |
|
81 """ |
|
82 |
|
83 abstract |
|
84 |
|
85 @property |
|
86 def modified (self) : |
|
87 """ |
|
88 Returns the page modification timestamp |
|
89 """ |
|
90 |
|
91 # stat |
|
92 timestamp = os.stat(self.path).st_mtime |
|
93 |
|
94 return time.strftime(config.DATETIME_FMT, time.gmtime(timestamp)) |
|
95 |
|
96 def handle_request (self, request) : |
|
97 """ |
|
98 Renders the fs's layout template with this page + menu |
|
99 """ |
|
100 |
|
101 # render the template |
|
102 response_data = template.render(self.fs.template, |
|
103 request = request, |
|
104 site_root_url = request.get_script_dir(), |
|
105 site_page_url = request.get_page_prefix(), |
|
106 page = self, |
|
107 menu = menu.Menu(self.fs, self), |
|
108 ) |
|
109 |
|
110 # return the response |
|
111 return http.Response(response_data) |
|
112 |
|
113 class HTMLPage (Page) : |
|
114 """ |
|
115 A simple .html page that's just passed through directly |
|
116 """ |
|
117 |
|
118 @property |
|
119 def content (self) : |
|
120 """ |
|
121 Opens the .html file, reads and returns contents |
|
122 """ |
|
123 |
|
124 return open(self.path, 'rb').read().decode(self.charset) |
|
125 |
|
126 class TemplatePage (Page) : |
|
127 """ |
|
128 A template that's rendered using our template library |
|
129 """ |
|
130 |
|
131 @property |
|
132 def content (self) : |
|
133 """ |
|
134 Loads the .tmpl file, and renders it |
|
135 """ |
|
136 |
|
137 return template.TemplateLoader.render_file(self.path, |
|
138 page_tree = self.fs.tree, |
|
139 ) |
|
140 |