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