author | Tero Marttila <terom@paivola.fi> |
Sun, 14 Sep 2014 04:58:01 +0300 | |
changeset 83 | f167c68d9f34 |
parent 80 | dc6e3fffcbe2 |
child 85 | aa437984ddc5 |
permissions | -rw-r--r-- |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
1 |
from django.conf import settings |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
2 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
3 |
import codecs |
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
4 |
import datetime |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
5 |
import logging; log = logging.getLogger('qmsk_www_pages.pages') |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
6 |
import os, os.path |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
7 |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
8 |
class NotFound (Exception): |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
9 |
pass |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
10 |
|
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
11 |
class Site (object): |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
12 |
@classmethod |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
13 |
def lookup (cls): |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
14 |
return cls( |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
15 |
root = settings.QMSK_WWW_PAGES_DIR, |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
16 |
name = settings.QMSK_WWW_PAGES_SITE, |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
17 |
) |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
18 |
|
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
19 |
def __init__ (self, root, name): |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
20 |
self.root = root |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
21 |
self.name = name |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
22 |
|
62 | 23 |
class Tree (object): |
63 | 24 |
INDEX = 'index' |
25 |
||
62 | 26 |
@classmethod |
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
27 |
def lookup (cls, site, parts): |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
28 |
""" |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
29 |
Returns Tree |
62 | 30 |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
31 |
Raises NotFound |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
32 |
""" |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
33 |
|
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
34 |
parents = ( ) |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
35 |
tree = cls(site.root, None, parents, site, |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
36 |
title = site.name, |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
37 |
) |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
38 |
|
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
39 |
for name in parts: |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
40 |
if name.startswith('.'): |
62 | 41 |
# evil |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
42 |
raise NotFound() |
62 | 43 |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
44 |
if not name: |
62 | 45 |
continue |
46 |
||
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
47 |
path = os.path.join(tree.path, name) |
62 | 48 |
|
49 |
if not os.path.exists(path): |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
50 |
raise NotFound() |
62 | 51 |
|
52 |
if not os.path.isdir(path): |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
53 |
raise NotFound() |
62 | 54 |
|
80 | 55 |
# title |
56 |
title = tree.item_title(name) |
|
57 |
||
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
58 |
parents += (tree, ) |
80 | 59 |
tree = cls(path, name, parents, site, |
60 |
title = title, |
|
61 |
) |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
62 |
|
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
63 |
return tree |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
64 |
|
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
65 |
def __init__ (self, path, name, parents, site, |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
66 |
title = None, |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
67 |
): |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
68 |
""" |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
69 |
path: filesystem path |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
70 |
name: subtree name, or None for root |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
71 |
parents: (Tree) |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
72 |
site: Site |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
73 |
""" |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
74 |
|
62 | 75 |
self.path = path |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
76 |
self.name = name |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
77 |
self.parents = parents |
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
78 |
self.site = site |
63 | 79 |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
80 |
self.title = title or name |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
81 |
|
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
82 |
def hierarchy (self): |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
83 |
""" |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
84 |
Yield Tree. |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
85 |
""" |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
86 |
|
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
87 |
for tree in self.parents: |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
88 |
yield tree |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
89 |
|
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
90 |
yield self |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
91 |
|
63 | 92 |
def url (self, tree=None, page=None): |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
93 |
path = '/'.join(tree.name for tree in self.hierarchy() if tree.name is not None) |
63 | 94 |
|
95 |
if path: |
|
96 |
path += '/' |
|
97 |
||
98 |
if tree: |
|
99 |
path = tree + '/' |
|
100 |
||
101 |
if page: |
|
102 |
path += page |
|
103 |
||
104 |
return path |
|
105 |
||
106 |
def scan (self): |
|
107 |
""" |
|
108 |
Scan for files in tree. |
|
109 |
""" |
|
110 |
||
111 |
for filename in os.listdir(self.path): |
|
112 |
if filename.startswith('.'): |
|
113 |
continue |
|
114 |
||
115 |
if '.' in filename: |
|
116 |
file_name, file_type = filename.rsplit('.', 1) |
|
117 |
else: |
|
118 |
file_name = filename |
|
119 |
file_type = None |
|
120 |
||
121 |
if not file_name: |
|
122 |
continue |
|
123 |
||
124 |
path = os.path.join(self.path, filename) |
|
125 |
||
126 |
yield path, file_name, file_type |
|
127 |
||
72 | 128 |
def item_title (self, name): |
129 |
""" |
|
130 |
Lookup item title if exists. |
|
131 |
""" |
|
132 |
||
133 |
title_path = os.path.join(self.path, name + '.title') |
|
134 |
||
135 |
log.info("%s: %s title_path=%s", self, name, title_path) |
|
136 |
||
137 |
if os.path.exists(title_path): |
|
138 |
return open(title_path).read().strip() |
|
139 |
else: |
|
140 |
return None |
|
141 |
||
63 | 142 |
def list (self): |
143 |
""" |
|
75 | 144 |
Lists all Trees and Pages for this Tree. |
145 |
||
146 |
Yields (name, url, page_type or None, title) |
|
63 | 147 |
""" |
148 |
||
72 | 149 |
for path, name, file_type in self.scan(): |
150 |
title = self.item_title(name) or name |
|
151 |
||
63 | 152 |
# trees |
153 |
if os.path.isdir(path): |
|
75 | 154 |
yield name, self.url(tree=name), None, title |
63 | 155 |
|
72 | 156 |
if name == self.INDEX: |
63 | 157 |
continue |
158 |
||
159 |
# pages |
|
160 |
if not file_type: |
|
161 |
continue |
|
162 |
||
163 |
if file_type not in TYPES: |
|
164 |
continue |
|
72 | 165 |
|
75 | 166 |
yield name, self.url(page=name), file_type, title |
167 |
||
168 |
def list_sorted (self): |
|
169 |
return sorted(list(self.list())) |
|
63 | 170 |
|
62 | 171 |
def page (self, name): |
172 |
""" |
|
173 |
Scans through tree looking for a matching page. |
|
174 |
||
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
175 |
Returns Page or None. |
62 | 176 |
""" |
63 | 177 |
|
178 |
if not name: |
|
179 |
name = self.INDEX |
|
80 | 180 |
title_default = self.title |
181 |
else: |
|
182 |
title_default = None |
|
62 | 183 |
|
83
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
184 |
parents = self.parents + (self, ) |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
185 |
|
63 | 186 |
for path, file_name, file_type in self.scan(): |
187 |
# match on name |
|
83
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
188 |
if file_name != name and (file_name + '.' + file_type != name): |
62 | 189 |
continue |
83
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
190 |
|
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
191 |
# redirects? |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
192 |
if os.path.islink(path): |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
193 |
target = os.readlink(path) |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
194 |
|
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
195 |
# XXX: this should be some kind of common code |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
196 |
if '.' in target: |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
197 |
target, target_type = target.rsplit('.', 1) |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
198 |
|
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
199 |
log.info("%s: %s -> %s", self, name, target) |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
200 |
|
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
201 |
return RedirectPage(path, name, self, parents, |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
202 |
target = target, |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
203 |
) |
62 | 204 |
|
63 | 205 |
# match on type |
206 |
if not file_type: |
|
207 |
continue |
|
208 |
||
62 | 209 |
page_type = TYPES.get(file_type) |
210 |
||
211 |
if not page_type: |
|
212 |
continue |
|
63 | 213 |
|
214 |
# out |
|
80 | 215 |
title = self.item_title(file_name) or title_default |
72 | 216 |
|
83
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
217 |
return page_type(path, name, self, parents, |
72 | 218 |
title = title, |
62 | 219 |
) |
220 |
||
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
221 |
class Page (object): |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
222 |
ENCODING = 'utf-8' |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
223 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
224 |
@classmethod |
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
225 |
def lookup (cls, site, page): |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
226 |
""" |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
227 |
Returns Page. |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
228 |
|
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
229 |
Raises NotFound |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
230 |
""" |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
231 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
232 |
log.info("page=%r", page) |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
233 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
234 |
if page: |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
235 |
parts = page.split('/') |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
236 |
else: |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
237 |
parts = [ ] |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
238 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
239 |
if parts: |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
240 |
page_name = parts.pop(-1) |
62 | 241 |
tree_parts = parts |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
242 |
else: |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
243 |
page_name = '' |
62 | 244 |
tree_parts = [] |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
245 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
246 |
# scan dir |
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
247 |
tree = Tree.lookup(site, tree_parts) |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
248 |
|
62 | 249 |
# scan page |
250 |
page = tree.page(page_name) |
|
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
251 |
|
62 | 252 |
if not page: |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
253 |
raise NotFound() |
62 | 254 |
|
255 |
return page |
|
256 |
||
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
257 |
def __init__ (self, path, name, tree, parents=(), encoding=ENCODING, title=None): |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
258 |
self.path = path |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
259 |
self.name = name |
62 | 260 |
self.tree = tree |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
261 |
self.parents = parents |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
262 |
|
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
263 |
self.encoding = encoding |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
264 |
self.title = title or name |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
265 |
|
69
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
266 |
def hierarchy (self): |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
267 |
""" |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
268 |
Yield (Tree, name) pairs |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
269 |
""" |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
270 |
|
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
271 |
parent = None |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
272 |
|
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
273 |
for tree in self.parents: |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
274 |
if parent: |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
275 |
yield parent, tree.name |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
276 |
|
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
277 |
parent = tree |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
278 |
|
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
279 |
yield parent, self.name |
4b8814ff6d6d
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
68
diff
changeset
|
280 |
|
64 | 281 |
def url (self): |
282 |
return self.tree.url(page=self.name) |
|
283 |
||
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
284 |
def open (self): |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
285 |
return codecs.open(self.path, encoding=self.encoding) |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
286 |
|
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
287 |
def stat (self): |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
288 |
return os.stat(self.path) |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
289 |
|
61 | 290 |
def breadcrumb (self): |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
291 |
for tree in self.tree.hierarchy(): |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
292 |
yield tree.url(), tree.title |
63 | 293 |
|
294 |
if self.name != self.tree.INDEX: |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
295 |
yield self.url(), self.title |
61 | 296 |
|
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
297 |
def modified (self): |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
298 |
return datetime.datetime.utcfromtimestamp(self.stat().st_mtime) |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
299 |
|
83
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
300 |
def redirect_page (self, request): |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
301 |
return None |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
302 |
|
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
303 |
def render_html (self, request): |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
304 |
raise NotImplementedError() |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
305 |
|
83
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
306 |
# TODO: tree redirects |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
307 |
class RedirectPage (Page): |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
308 |
def __init__ (self, path, name, tree, parents, |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
309 |
target, |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
310 |
**opts |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
311 |
) : |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
312 |
super(RedirectPage, self).__init__(path, name, tree, parents, **opts) |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
313 |
|
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
314 |
self.target = target |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
315 |
|
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
316 |
def redirect_page (self, request): |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
317 |
return os.path.normpath(self.tree.url() + '/' + self.target) |
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
318 |
|
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
319 |
class HTML_Page (Page): |
83
f167c68d9f34
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
80
diff
changeset
|
320 |
def render_html (self, request): |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
321 |
return self.open().read() |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
322 |
|
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
323 |
SITE = Site.lookup() |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
324 |
|
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
325 |
TYPES = { |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
326 |
'html': HTML_Page, |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
327 |
} |
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
328 |
|
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
329 |
def page (page): |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
330 |
return Page.lookup(SITE, page) |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
331 |