gdritter repos frony-ritter-designs / 4a9508e
Switch user-facing portions to flask Getty Ritter 4 years ago
1 changed file(s) with 357 addition(s) and 377 deletion(s). Collapse all Expand all
+357
-377
frony.py less more
88 import sys
99
1010 web.template.ALLOWED_AST_NODES.append('Constant')
11
12 app = flask.Flask(__name__)
1113
1214 class Templates:
1315 renderer = pystache.Renderer()
6668 return new_func
6769
6870
69 class index:
70 @main
71 def GET(self):
72 try:
73 pg = db.get_page('main')
74 return 'Frony Ritter Designs', markdown.markdown(pg.text)
75 except:
76 raise web.notfound()
77
78
79 class contact:
80 @main
81 def GET(self):
82 return 'Contact Information', 'Send me an email at my email address.'
83
84
85 class design:
86 @main
87 def GET(self, id, slug=None):
88 design = db.get_design(int(id))
89 print(design)
90 return 'Designs', Templates.design_page(design)
91
92 def POST(self, id, slug='electric-boogaloo'):
93 try:
94 id = int(id)
95 except:
96 raise web.redirect('/')
97 input = dict(web.input())
98 if ('title' in input and
99 'description' in input and
100 'category' in input and
101 'tags' in input):
102 db.put_design(id,
103 input['title'],
104 input['description'],
105 input['category'])
106 db.process_tag_list(id, input['tags'])
107 if not slug:
108 slug = slugify(input['title'])
109 raise web.redirect('/edit/design/{0:05}/slug/'.format(id, slug))
110
111
112 class all_designs:
113
114 @main
115 def GET(self):
116 winput = web.input(page=0)
117 page = int(winput['page'])
118 min, max = db.max_page_ranges()
119 data = db.get_all(offset=page)
120 return ('Designs',
121 render.design_list(
122 (render.design_tile(name, pics, snip(desc), id)
123 for name, pics, desc, cat, id in data),
124 page,
125 max,
126 page > min,
127 page < max))
128
129
130 class category:
131
132 @main
133 def GET(self, cat):
134 winput = web.input(page=0)
135 page = int(winput['page'])
136 data = db.get_designs_by_category(cat, page)
137 min, max = db.max_page_range_for_category(cat)
138 return (cat.capitalize(),
139 render.by_category_list(
140 cat,
141 (render.design_tile(name, pics, 0, id)
142 for name, pics, desc, cat, id in data),
143 page,
144 page > min,
145 page < (max - 1)))
146
147
148 class category_tag:
149
150 @main
151 def GET(self, cat, tag):
152 winput = web.input(page=0)
153 page = int(winput['page'])
154 data = db.get_designs_by_category_and_tag(cat, tag, page)
155 min, max = db.max_page_range_for_category(cat)
156 return (cat.capitalize(),
157 render.by_category_list(
158 cat,
159 (render.design_tile(name, pics, 0, id)
160 for name, pics, desc, cat, id in data),
161 page,
162 page > min,
163 page < (max - 1)))
164
165
166 class tag:
167
168 @main
169 def GET(self, tag):
170 winput = web.input(page=0)
171 page = int(winput['page'])
172 data = db.get_designs_by_tag(tag, page)
173 min, max = db.max_page_range_for_tag(tag)
174 pretty_tag = ' '.join(w.capitalize()
175 for w in tag.split('_'))
176 return (pretty_tag,
177 render.by_tag_list(
178 tag,
179 (render.design_tile(name, pics, 0, id)
180 for name, pics, desc, cat, id in data),
181 page,
182 page > min,
183 page < max))
184
185
186 class all_categories:
187
188 @main
189 def GET(self):
190 categories = db.all_categories()
191 return 'Category', render.select_category(categories)
192
193
194 class all_tags:
195
196 @main
197 def GET(self):
198 pretty = lambda t: ' '.join(w.capitalize()
199 for w in t.split('_'))
200 tags = (
201 (t, pretty(t), db.num_for_tag(t))
202 for t in sorted(db.get_all_tags()))
203 return 'All Tags', render.select_tag(tags)
204
205
206 class page:
207
208 @main
209 def GET(self, name):
210 try:
211 pg = db.get_page(name)
212 return (pg.title,
213 markdown.markdown(pg.text))
214 except:
215 raise web.notfound()
216
217
218 class admin:
219
220 class edit_all_designs:
221
222
223 @main
224 def GET(self):
225 winput = web.input(page=0)
226 page = int(winput['page'])
227 min, max = db.max_page_ranges()
228 data = db.get_all(offset=page)
229 return ('Edit Mode',
230 render.edit_design_list(
231 ((name, pics, id) for name, pics, _, _, id in data),
232 page,
233 page > min,
234 page < max))
235
236 def POST(self):
237 id = db.new_design()
238 raise web.redirect('/edit/design/{0:05}/'.format(
239 id))
240
241 class edit_design:
242
243
244 @main
245 def GET(self, id, slug=None):
246 try:
247 id = int(id)
248 design = db.get_design(id)
249 tags = ' '.join(db.get_tags_for_design(id))
250 (name, images,
251 description,
252 cat, id) = design
253 return name, render.edit_design(
254 name, images, description, cat, id, tags)
255 except:
256 raise web.notfound()
257
258 class delete_design:
259
260
261 @main
262 def GET(self, id):
263 try:
264 id = int(id)
265 except:
266 raise web.notfound()
267 design = db.get_design(id)
268 return ('Delete',
269 render.delete_design(*design))
270
271
272 def POST(self, id):
273 try:
274 id = int(id)
275 except:
276 raise web.notfound()
277 db.delete_design(id)
278 raise web.redirect('/edit/design/')
279
280 class add_photo:
281
282
283 @main
284 def GET(self):
285 input = web.input()
286 if 'id' in input:
287 return ('Add Photo For Design {0}'.format(input.id),
288 render.photo_upload(input.id))
289 else:
290 return ('Add Generic Photo', render.photo_upload(-1))
291
292
293 def POST(self):
294 input = web.input(file={})
295 try:
296 id = int(input['id'])
297 except:
298 raise web.redirect('/design/')
299 if 'file' in input:
300 photo_id = db.add_photo(input['file'], id)
301 if id == -1:
302 raise web.redirect('/edit/view-photo/{0:09}/'.format(photo_id))
303 else:
304 raise web.redirect('/edit/design/{0:05}/'.format(id))
305
306 class all_files:
307
308
309 @main
310 def GET(self):
311 files = db.all_files()
312 return ('All Uploaded Files', render.all_files(files))
313
314 class add_file:
315
316
317 @main
318 def GET(self):
319 return ('Add File', render.file_upload())
320
321
322 def POST(self):
323 input = web.input(file={})
324 if 'file' in input:
325 file_id = db.add_photo(input['file'])
326 raise web.redirect('/edit/file-list/')
327
328 class modify_photo:
329
330
331 @main
332 def GET(self, id):
333 design_id = web.input().design_id
334 return ('Delete Photo',
335 render.photo_delete(id, design_id))
336
337
338 def POST(self, id):
339 input = web.input()
340 design_id = int(input['design_id'])
341 db.delete_photo(id)
342 raise web.redirect('/edit/design/{0:05}/'.format(
343 design_id))
344
345 class view_all_photos:
346
347
348 @main
349 def GET(self):
350 winput = web.input(page=0)
351 page = int(winput['page'])
352 min, max = db.max_photo_page_ranges()
353 data = db.get_all_photos(offset=page)
354 return ('Photos',
355 render.photo_list((render.photo_tile(p) for p in data),
356 page, max, page > min, page < max))
357
358 class view_photo:
359
360
361 @main
362 def GET(self, id):
363 photo = db.get_photo_by_id(id)
364 if photo:
365 return ('Viewing Photo {0}'.format(id),
366 render.view_photo(id, photo))
367 else:
368 raise web.notfound()
369
370 class edit_page_list:
371
372
373 @main
374 def GET(self):
375 return ('Edit Page List',
376 render.edit_pages(db.get_page_list()))
377
378 class edit_page:
379
380
381 @main
382 def GET(self, name):
383 return (
384 'Edit Page {0}'.format(name),
385 render.edit_page(name, db.get_page(name)))
386
387
388 def POST(self, name):
389 input = web.input()
390 text = input.get('text', '')
391 title = input.get('title', name.capitalize())
392 db.set_page(name, title, text)
393 raise web.redirect('/edit/pages/{0}/'.format(name))
394
395 class edit_about:
396
397
398 @main
399 def GET(self):
400 return (
401 'Edit About',
402 render.edit_about(db.get_about_text()))
403
404
405 def POST(self):
406 input = web.input()
407 text = input.get('text', '')
408 db.set_about_text(text)
409 raise web.redirect('/edit/about/')
410
411
412 urls = (
413
414 # viewing urls
415 '/', index,
416 '/design/?', all_designs,
417 '/design/([^/]*)/([^/]*)/?$', design,
418 '/design/([^/]*)/?$', design,
419 '/category/?', all_categories,
420 '/category/([^/]*)/?', category,
421 '/category/([^/]*)/tag/([^/]*)/?', category_tag,
422 '/tag/?', all_tags,
423 '/tag/([^/]*)/?', tag,
424
425 # editing urls
426 '/edit/design/?', admin.edit_all_designs,
427 '/edit/design/([^/]*)/?', admin.edit_design,
428 '/edit/design/([^/]*)/([^/]*)/?', admin.edit_design,
429 '/edit/delete/([^/]*)/?', admin.delete_design,
430 '/edit/photo/?', admin.add_photo,
431 '/edit/photo/([^/]*)/?', admin.modify_photo,
432 '/edit/view-photo/?', admin.view_all_photos,
433 '/edit/view-photo/([^/]*)/?', admin.view_photo,
434 '/edit/file-list/?', admin.all_files,
435 '/edit/file/?', admin.add_file,
436 '/edit/pages/?', admin.edit_page_list,
437 '/edit/pages/([^/]*)/?', admin.edit_page,
438 '/edit/about/?', admin.edit_about,
439
440 # catch-all for other pages
441 '/([^/]*)/?', page
442
443 )
444
445 app = web.application(urls, globals())
71 @app.route('/')
72 @main
73 def index():
74 pg = db.get_page('main')
75 return 'Frony Ritter Designs', markdown.markdown(pg.text)
76
77
78 @app.route('/design/<id>/<slug>/')
79 @main
80 def get_design(id, slug=None):
81 design = db.get_design(int(id))
82 return 'Designs', Templates.design_page(design)
83
84 # @app.route('/design/<id>/<slug>', method=['POST'])
85 # def POST(self, id, slug='electric-boogaloo'):
86 # try:
87 # id = int(id)
88 # except:
89 # raise web.redirect('/')
90 # input = dict(web.input())
91 # if ('title' in input and
92 # 'description' in input and
93 # 'category' in input and
94 # 'tags' in input):
95 # db.put_design(id,
96 # input['title'],
97 # input['description'],
98 # input['category'])
99 # db.process_tag_list(id, input['tags'])
100 # if not slug:
101 # slug = slugify(input['title'])
102 # raise web.redirect('/edit/design/{0:05}/slug/'.format(id, slug))
103
104
105 @app.route('/design/')
106 @main
107 def get_all_designs():
108 page = int(flask.request.args.get('page') or 0)
109 min, max = db.max_page_ranges()
110 data = db.get_all(offset=page)
111 return ('Designs',
112 render.design_list(
113 (render.design_tile(name, pics, snip(desc), id)
114 for name, pics, desc, cat, id in data),
115 page,
116 max,
117 page > min,
118 page < max))
119
120
121
122 @app.route('/category/')
123 @main
124 def get_all_categories():
125 categories = db.all_categories()
126 return 'Category', render.select_category(categories)
127
128 @app.route('/category/<cat>/')
129 @main
130 def get_category(cat):
131 page = int(flask.request.args.get('page') or 0)
132 data = db.get_designs_by_category(cat, page)
133 min, max = db.max_page_range_for_category(cat)
134 return (cat.capitalize(),
135 render.by_category_list(
136 cat,
137 (render.design_tile(name, pics, 0, id)
138 for name, pics, desc, cat, id in data),
139 page,
140 page > min,
141 page < (max - 1)))
142
143 @app.route('/category/<cat>/tag/<tag>/')
144 @main
145 def get_category_with_tag(cat, tag):
146 page = int(flask.request.args.get('page') or 0)
147 data = db.get_designs_by_category_and_tag(cat, tag, page)
148 min, max = db.max_page_range_for_category(cat)
149 return (cat.capitalize(),
150 render.by_category_list(
151 cat,
152 (render.design_tile(name, pics, 0, id)
153 for name, pics, desc, cat, id in data),
154 page,
155 page > min,
156 page < (max - 1)))
157
158
159 @app.route('/tag/')
160 @main
161 def get_all_tags():
162 pretty = lambda t: ' '.join(w.capitalize()
163 for w in t.split('_'))
164 tags = (
165 (t, pretty(t), db.num_for_tag(t))
166 for t in sorted(db.get_all_tags()))
167 return 'All Tags', render.select_tag(tags)
168
169 @app.route('/tag/<tag>/')
170 @main
171 def get_tag(tag):
172 page = int(flask.request.args.get('page') or 0)
173 data = db.get_designs_by_tag(tag, page)
174 min, max = db.max_page_range_for_tag(tag)
175 pretty_tag = ' '.join(w.capitalize()
176 for w in tag.split('_'))
177 return (pretty_tag,
178 render.by_tag_list(
179 tag,
180 (render.design_tile(name, pics, 0, id)
181 for name, pics, desc, cat, id in data),
182 page,
183 page > min,
184 page < max))
185
186
187 @app.route('/<name>/')
188 @main
189 def page(name):
190 try:
191 pg = db.get_page(name)
192 return (pg.title,
193 markdown.markdown(pg.text))
194 except:
195 raise web.notfound()
196
197
198 # class admin:
199
200 # class edit_all_designs:
201
202
203 # @main
204 # def GET(self):
205 # winput = web.input(page=0)
206 # page = int(winput['page'])
207 # min, max = db.max_page_ranges()
208 # data = db.get_all(offset=page)
209 # return ('Edit Mode',
210 # render.edit_design_list(
211 # ((name, pics, id) for name, pics, _, _, id in data),
212 # page,
213 # page > min,
214 # page < max))
215
216 # def POST(self):
217 # id = db.new_design()
218 # raise web.redirect('/edit/design/{0:05}/'.format(
219 # id))
220
221 # class edit_design:
222
223
224 # @main
225 # def GET(self, id, slug=None):
226 # try:
227 # id = int(id)
228 # design = db.get_design(id)
229 # tags = ' '.join(db.get_tags_for_design(id))
230 # (name, images,
231 # description,
232 # cat, id) = design
233 # return name, render.edit_design(
234 # name, images, description, cat, id, tags)
235 # except:
236 # raise web.notfound()
237
238 # class delete_design:
239
240
241 # @main
242 # def GET(self, id):
243 # try:
244 # id = int(id)
245 # except:
246 # raise web.notfound()
247 # design = db.get_design(id)
248 # return ('Delete',
249 # render.delete_design(*design))
250
251
252 # def POST(self, id):
253 # try:
254 # id = int(id)
255 # except:
256 # raise web.notfound()
257 # db.delete_design(id)
258 # raise web.redirect('/edit/design/')
259
260 # class add_photo:
261
262
263 # @main
264 # def GET(self):
265 # input = web.input()
266 # if 'id' in input:
267 # return ('Add Photo For Design {0}'.format(input.id),
268 # render.photo_upload(input.id))
269 # else:
270 # return ('Add Generic Photo', render.photo_upload(-1))
271
272
273 # def POST(self):
274 # input = web.input(file={})
275 # try:
276 # id = int(input['id'])
277 # except:
278 # raise web.redirect('/design/')
279 # if 'file' in input:
280 # photo_id = db.add_photo(input['file'], id)
281 # if id == -1:
282 # raise web.redirect('/edit/view-photo/{0:09}/'.format(photo_id))
283 # else:
284 # raise web.redirect('/edit/design/{0:05}/'.format(id))
285
286 # class all_files:
287
288
289 # @main
290 # def GET(self):
291 # files = db.all_files()
292 # return ('All Uploaded Files', render.all_files(files))
293
294 # class add_file:
295
296
297 # @main
298 # def GET(self):
299 # return ('Add File', render.file_upload())
300
301
302 # def POST(self):
303 # input = web.input(file={})
304 # if 'file' in input:
305 # file_id = db.add_photo(input['file'])
306 # raise web.redirect('/edit/file-list/')
307
308 # class modify_photo:
309
310
311 # @main
312 # def GET(self, id):
313 # design_id = web.input().design_id
314 # return ('Delete Photo',
315 # render.photo_delete(id, design_id))
316
317
318 # def POST(self, id):
319 # input = web.input()
320 # design_id = int(input['design_id'])
321 # db.delete_photo(id)
322 # raise web.redirect('/edit/design/{0:05}/'.format(
323 # design_id))
324
325 # class view_all_photos:
326
327
328 # @main
329 # def GET(self):
330 # winput = web.input(page=0)
331 # page = int(winput['page'])
332 # min, max = db.max_photo_page_ranges()
333 # data = db.get_all_photos(offset=page)
334 # return ('Photos',
335 # render.photo_list((render.photo_tile(p) for p in data),
336 # page, max, page > min, page < max))
337
338 # class view_photo:
339
340
341 # @main
342 # def GET(self, id):
343 # photo = db.get_photo_by_id(id)
344 # if photo:
345 # return ('Viewing Photo {0}'.format(id),
346 # render.view_photo(id, photo))
347 # else:
348 # raise web.notfound()
349
350 # class edit_page_list:
351
352
353 # @main
354 # def GET(self):
355 # return ('Edit Page List',
356 # render.edit_pages(db.get_page_list()))
357
358 # class edit_page:
359
360
361 # @main
362 # def GET(self, name):
363 # return (
364 # 'Edit Page {0}'.format(name),
365 # render.edit_page(name, db.get_page(name)))
366
367
368 # def POST(self, name):
369 # input = web.input()
370 # text = input.get('text', '')
371 # title = input.get('title', name.capitalize())
372 # db.set_page(name, title, text)
373 # raise web.redirect('/edit/pages/{0}/'.format(name))
374
375 # class edit_about:
376
377
378 # @main
379 # def GET(self):
380 # return (
381 # 'Edit About',
382 # render.edit_about(db.get_about_text()))
383
384
385 # def POST(self):
386 # input = web.input()
387 # text = input.get('text', '')
388 # db.set_about_text(text)
389 # raise web.redirect('/edit/about/')
390
391
392 # urls = (
393
394 # # viewing urls
395 # '/', index,
396 # '/design/?', all_designs,
397 # '/design/([^/]*)/([^/]*)/?$', design,
398 # '/design/([^/]*)/?$', design,
399 # '/category/?', all_categories,
400 # '/category/([^/]*)/?', category,
401 # '/category/([^/]*)/tag/([^/]*)/?', category_tag,
402 # '/tag/?', all_tags,
403 # '/tag/([^/]*)/?', tag,
404
405 # # editing urls
406 # '/edit/design/?', admin.edit_all_designs,
407 # '/edit/design/([^/]*)/?', admin.edit_design,
408 # '/edit/design/([^/]*)/([^/]*)/?', admin.edit_design,
409 # '/edit/delete/([^/]*)/?', admin.delete_design,
410 # '/edit/photo/?', admin.add_photo,
411 # '/edit/photo/([^/]*)/?', admin.modify_photo,
412 # '/edit/view-photo/?', admin.view_all_photos,
413 # '/edit/view-photo/([^/]*)/?', admin.view_photo,
414 # '/edit/file-list/?', admin.all_files,
415 # '/edit/file/?', admin.add_file,
416 # '/edit/pages/?', admin.edit_page_list,
417 # '/edit/pages/([^/]*)/?', admin.edit_page,
418 # '/edit/about/?', admin.edit_about,
419
420 # # catch-all for other pages
421 # '/([^/]*)/?', page
422
423 # )
424
425 # app = web.application(urls, globals())
446426 # # web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
447427
448428 if __name__ == '__main__':