2
|
1 |
|
|
2 |
import templates as _templates
|
|
3 |
|
|
4 |
class _Page (object) :
|
|
5 |
"""
|
|
6 |
A page is kind of like a controller, I guess
|
|
7 |
"""
|
|
8 |
|
|
9 |
# the page title, used in the HTML <title>
|
|
10 |
title = None
|
|
11 |
|
|
12 |
# the page name, used in the menu
|
|
13 |
name = None
|
|
14 |
|
|
15 |
# the page path, used in the URL
|
|
16 |
path = None
|
|
17 |
|
|
18 |
def __init__ (self, req, path_suffix) :
|
|
19 |
self.req = req
|
|
20 |
self.path_suffix = path_suffix
|
|
21 |
|
|
22 |
def _build_template (self, template_class) :
|
|
23 |
tpl = template_class(searchList=[self.req])
|
|
24 |
|
|
25 |
tpl.page_title = self.title
|
|
26 |
tpl.page_name = self.name
|
|
27 |
tpl.page_path = self.path
|
|
28 |
|
|
29 |
tpl.page_menu = root_menu
|
|
30 |
tpl.page = self
|
|
31 |
tpl.page_children = self.get_children()
|
|
32 |
|
|
33 |
return tpl
|
|
34 |
|
|
35 |
def get_children (self) :
|
|
36 |
"""
|
|
37 |
Returns a list of page objects that are children of this one. May return None if there are none
|
|
38 |
"""
|
|
39 |
|
|
40 |
return None
|
|
41 |
|
|
42 |
def get_response_code (self) :
|
|
43 |
"""
|
|
44 |
Returns the HTTP response code to be used
|
|
45 |
"""
|
|
46 |
|
|
47 |
return 200
|
|
48 |
|
|
49 |
def render_template (self) :
|
|
50 |
"""
|
|
51 |
Returns an instance of Cheetah.Template, prepopulated with whatever variables it needs, ready to be rendered
|
|
52 |
"""
|
|
53 |
|
|
54 |
abstract
|
|
55 |
|
|
56 |
class StaticHTML (_Page) :
|
|
57 |
# the path to the .html file
|
|
58 |
file_path = None
|
|
59 |
|
|
60 |
def __init__ (self, *args) :
|
|
61 |
super(StaticHTML, self).__init__(*args)
|
|
62 |
|
|
63 |
# open the .html and read in the contents
|
|
64 |
fh = open(self.file_path, "r")
|
|
65 |
|
|
66 |
self.file_data = fh.read()
|
|
67 |
|
|
68 |
fh.close()
|
|
69 |
|
|
70 |
def render_template (self) :
|
|
71 |
tpl = self._build_template(_templates.layout)
|
|
72 |
|
|
73 |
tpl.page_content = self.file_data
|
|
74 |
|
|
75 |
return tpl
|
|
76 |
|
|
77 |
def html_page (_file_path, _title, _name, _path) :
|
|
78 |
class _anon_html_page (StaticHTML) :
|
|
79 |
file_path = _file_path
|
|
80 |
title = _title
|
|
81 |
name = _name
|
|
82 |
path = _path
|
|
83 |
|
|
84 |
return _anon_html_page
|
|
85 |
|
|
86 |
class Main (_Page) :
|
|
87 |
"""
|
|
88 |
Main page with simple stuff
|
|
89 |
"""
|
|
90 |
|
|
91 |
title = "Main Page"
|
|
92 |
name = "Main"
|
|
93 |
path = ""
|
|
94 |
|
|
95 |
def render_template (self) :
|
|
96 |
return self._build_template(_templates.main)
|
|
97 |
|
|
98 |
|
|
99 |
class Error404 (_Page) :
|
|
100 |
title = "Error 404 - Not Found"
|
|
101 |
|
|
102 |
def get_response_code (self) :
|
|
103 |
return 404
|
|
104 |
|
|
105 |
def render_template (self) :
|
|
106 |
return self._build_template(_templates.error_404)
|
|
107 |
|
|
108 |
# load HTML pages
|
|
109 |
About = html_page("pages/about.html", "About", "About", "about")
|
|
110 |
|
|
111 |
pages = {
|
|
112 |
'main': Main,
|
|
113 |
'about': About,
|
|
114 |
}
|
|
115 |
|
|
116 |
root_menu = [
|
|
117 |
Main,
|
|
118 |
About
|
|
119 |
]
|
|
120 |
|
|
121 |
def find (req) :
|
|
122 |
"""
|
|
123 |
This finds the page to use for the given req and reuturns an instance of it
|
|
124 |
"""
|
|
125 |
|
|
126 |
for prefix, suffix in req.page_name_prefixes() :
|
|
127 |
if prefix in pages :
|
|
128 |
return pages[prefix](req, suffix)
|
|
129 |
|
|
130 |
return Error404(req, req.page_path)
|
|
131 |
|