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