X-Git-Url: https://projects.mako.cc/source/yourule/blobdiff_plain/2b6309dc7c65b8849fcd9aa7aae834b014ab5a10..ef2e46ac992612652be950ae4aee0e306dc660f5:/yourule.py diff --git a/yourule.py b/yourule.py index c5ffdb7..d18e1e6 100755 --- a/yourule.py +++ b/yourule.py @@ -1,21 +1,58 @@ #!/usr/bin/env python +# YouRule: Onscreen Ruler Generator +# +# Copyright (C) 2007 Benjamin Mako Hill +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the Affero General Public License as published +# by the Free Software Foundation, either version 1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the Affero General Public License +# along with this program. If not, see +# . + + from __future__ import division import web import sys, os, re from storm.locals import * + +# recalculate the path based on the location of the file +sys.path.append(os.path.dirname(__file__)) from svgruler import SVGRuler +from jinja import Environment, FileSystemLoader +jinja_env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>', + loader=FileSystemLoader(os.path.dirname(__file__) + "/templates/")) + +def render(filename, vars={}): + if re.match(r'.+\.\css$', filename): + web.header("Content-Type","text/css; charset=utf-8") + else: + web.header("Content-Type","text/html; charset=utf-8") + + tmpl = jinja_env.get_template(filename + '.tmpl') + vars['homepath'] = web.ctx.homepath + vars['ctx'] = web.ctx + print tmpl.render(vars) + # the url map for the application urls = ( '/?', 'index', '/ruler_([0-9\.]+)px_([0-9\.]+)([A-Za-z]+).(svg|png|jpg)', 'ruler_img', '/show/(.*(svg|png|jpg))', 'show_ruler', '/gallery(.*)', 'gallery', '/delete/(\d+)', 'delete', - '/undelete/(\d+)', 'undelete') + '/undelete/(\d+)', 'undelete', + '/style.css', 'css') -database = create_database("sqlite:%s/db/yourule.db" % - os.path.dirname(__file__)) +database = create_database("mysql:yourule") store = Store(database) @@ -26,7 +63,7 @@ class Ruler(object): unit_width = Float() model = Unicode() units = Unicode() - show = Int() + visible = Int() cm_in_ratio = 0.3937 def __init__(self, **kw): @@ -57,8 +94,7 @@ class Ruler(object): class index: def GET(self): - web.header("Content-Type","text/html; charset=utf-8") - web.render('index.tmpl') + render('index', locals()) def POST(self): input = web.input() @@ -69,7 +105,7 @@ class index: pixel_width = input['pixel_width'] unit_width = input['unit_width'] units = input['units'] - web.render('index.tmpl') + render('index', locals()) else: ruler = Ruler(pixel_width = input['pixel_width'], unit_width = input['unit_width'], @@ -79,7 +115,6 @@ class index: class show_ruler: def GET(self, ruler_url, ext): - web.debug('test test') if web.input().has_key('fromgallery'): fromgallery = True else: @@ -87,9 +122,7 @@ class show_ruler: other_unit, other_unit_url = get_other_unit(ruler_url) - web.header("Content-Type","text/html; charset=utf-8") - web.render('show_ruler.tmpl') - + render('show_ruler', locals()) class ruler_img: def GET(self, pixel_width=None, unit_width=None, units=None, ext=None): @@ -127,9 +160,15 @@ class gallery: if ruler_url: pixel_width, unit_width, units = process_ruler_url(ruler_url)[0:3] - rulers = store.find(Ruler, Ruler.show == 1) - rulers.order_by(Ruler.model) - web.render('gallery.tmpl') + new_rulers = store.find(Ruler, Ruler.visible == 1)#.order_by(Ruler.model) + + rulers = [] + for ruler in new_rulers: + rulers.append(ruler) + + #rulers = map(lambda x: x, rulers) + + render('gallery', locals()) def POST(self, ruler_url): input = web.input() @@ -152,14 +191,14 @@ class gallery: store.add(new_ruler) store.commit() - rulers = store.find(Ruler, Ruler.show == 1) - rulers.order_by(Ruler.model) - web.render('gallery.tmpl') + rulers = store.find(Ruler, Ruler.visible == 1) + #rulers.order_by(Ruler.model) + render('gallery', locals()) class delete: def GET(self, id): ruler = store.get(Ruler, int(id)) - ruler.show = 0 + ruler.visible = 0 store.commit() web.redirect('/gallery') @@ -167,10 +206,14 @@ class delete: class undelete: def GET(self, id): ruler = store.get(Ruler, int(id)) - ruler.show = 1 + ruler.visible = 1 store.commit() web.redirect('/gallery') +class css: + def GET(self): + render('style.css') + def get_other_unit(url): pixel_width, unit_width, units = process_ruler_url(url)[0:3] @@ -187,7 +230,6 @@ def get_other_unit(url): new_ruler = Ruler(pixel_width=pixel_width, unit_width=unit_width, units=units) - web.debug(units) return(units, new_ruler.url()) @@ -211,12 +253,10 @@ def validate_input(input): return(errormsg) -# render the site template here so that i can use it later -web.render('site.tmpl', None, True, 'site') web.webapi.internalerror = web.debugerror if __name__ == "__main__": web.run(urls, globals(), web.reloader) -#application = web.wsgifunc(web.webpyfunc(urls, globals())) +application = web.wsgifunc(web.webpyfunc(urls, globals()))