forked from terrysunhh/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
274 lines (206 loc) · 7.34 KB
/
controller.py
File metadata and controls
274 lines (206 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Blog handler for urls
version 1.0
history:
2013-6-19 dylanninin@gmail.com init
"""
import os
import config
from __init__ import entryService
render = config.render
web = config.web
config = config.blogconfig
class Index(object):
"""
Index Handler for /?
example:
/
request the index of this blog and list the first page of all
entries of this blog from newest to oldest
the default page size is 5 which is configured with config.limit
/?start=1
equivalent with /
/?start=1&limit=10
request the second page with 10 entries
template:
template/index.html
reference:
config.py, model.py, service.py
"""
def GET(self):
params = web.input(start=config.start, limit=config.limit)
limit = int(params.limit)
start = int(params.start)
params = entryService.search(entryService.types.index, config.index_url, '', start, limit)
return render.index(params)
class Entry(object):
"""
Entry Handler for /blog(.*)
example:
/blog
the same as request /
/blog/
the same as request /blog/
/blog/2013/06/20/webpy_introduction.html
request entry with this url else error will be responsed
template:
template/entry.html, template/index.html
reference:
config.py, model.py, service.py
"""
def GET(self, url):
if not url in ['', '/']:
url = config.entry_url + url
params = entryService.find_by_url(entryService.types.entry, url)
if params.entry == None:
print "error 2"
raise web.notfound(render.error(params))
else:
return render.entry(params)
params = entryService.search(entryService.types.index, url)
return render.index(params)
class Archive(object):
"""
Archive Handler for /archive(.*)
example:
/archive
request the archive of all posted entries on this blog
/archive/
the same as /archive
/archive/2013
request the archive of all posted entries on 2013
/archive/2013/
the same as /archive/2013
/archive/2013/06
request the archive of all posted entries on 2013/06
/archive/2013/06/
the same as /archive/2013/06
/archive/2013/06/20
request the archive of all posted entries 2013/06/20
/archive/2013/06/20
the same as /archive/2013/06/20
template:
template/archive.html
reference:
config.py, model.py, service.py
"""
def GET(self, url):
url = config.archive_url + url
params = entryService.archive(entryService.types.entry, url)
if params.entries == None:
raise web.notfound(render.error(params))
return render.archive(params)
class About(object):
"""
Page Handler for /about.html
template:
template/entry.html
reference:
config.py, model.py, service.py
"""
def GET(self):
url = config.about_url
params = entryService.find_by_url(entryService.types.page, url)
if params.entry == None:
raise web.notfound(render.error(params))
return render.entry(params)
class Subscribe(object):
"""
Subscribe Handler for /atom.xml
#TODO: FIXME: find related entries
template:
template/subscribe.xml
reference:
config.py, model.py, service.py
"""
def GET(self):
params = entryService.search(entryService.types.index, config.subscribe_url)
web.header('Content-Type', 'text/xml')
return render.atom(params)
class Search(object):
"""
Search Handler for /search?type=type&value=value&start=start&limit=limit
example:
/search?type=query&value=input&start=1&limit=5
/search?type=tag&value=webpy&start=1&limit=5
/search?type=category&value=python
template:
template/search.html
reference:
config.py, model.py, service.py
"""
def GET(self, url):
params = web.input(type=entryService.types.query, value='', \
start=config.start, limit=config.limit)
limit = int(params.limit)
start = int(params.start)
url = '%s/?type=%s&value=%s&start=%d&limit=%d' % (config.search_url, params.type, params.value, start, limit)
params = entryService.search(params.type, url, params.value, start, limit)
if not params.entries == None:
return render.search(params)
raise web.notfound(render.error(params))
class Raw(object):
"""
Raw Handler for /raw(.+)
example:
/raw
request the archive of raw formats of all posted entries on this blog
/raw/
the same as /raw/
/raw/2013/06/20/webpy_introduction.md
request the raw content with url 2013/06/20/webpy_introduction.md
and usually the rendered html url is 2013/06/20/webpy_introduction.html
/raw/about.md
request the raw content with url about.md its rendered html url is /abouts.html
reference:
config.py, model.py, service.py
"""
def GET(self, url):
url = config.raw_url + url
raw = entryService.find_raw(url)
if not raw == None:
web.header('Context-Type', 'text/plain')
web.header('Content-Encoding', 'utf-8')
return raw
params = entryService.archive(entryService.types.raw, url)
if params.entries == None:
raise web.notfound(render.error(params))
return render.archive(params)
class Image(object):
"""
favicon.ico handler
reference:
config.py
"""
def GET(self):
url = config.favicon_url
name = url.lstrip('/')
ext = name.split('.')[-1]
cType = {
"png": "images/png",
"jpg": "images/jpeg",
"gif": "images/gif",
"ico": "images/x-icon"
}
if name in os.listdir(config.static_dir):
web.header('Content-Type', cType[ext])
return open('%s/%s' % (config.static_dir, name), 'rb').read()
params = entryService.error(url)
raise web.notfound(render.error(params))
class Error(object):
"""
Error Handler for any other url
template:
template/error.html
reference:
config.py, model.py, service.py
"""
def GET(self, url):
params = entryService.error(url)
# return render.error(params)
raise web.notfound(render.error(params))
if __name__ == "__main__":
import doctest
doctest.testmod()