2738f8f5ff2878c8a3224f1611c69845e62f6075
[yourule] / ruler_web.py
1 #!/usr/bin/env python
2
3 from __future__ import division
4 import web
5 import sys, os, re
6 from storm.locals import *
7 from ruler import RulerImage
8
9 # the url map for the application
10 urls = ( '/', 'index',
11          '/ruler_([0-9\.]+)px_([0-9\.]+)([A-Za-z]+).(svg|png|jpg)', 'ruler_img',
12          '/show/(.*(svg|png|jpg))', 'ruler',
13          '/gallery(.*)', 'gallery',
14          '/delete/(\d+)', 'delete',
15          '/undelete/(\d+)', 'undelete')
16
17 database = create_database("sqlite:ruler_web.db")
18 store = Store(database)
19
20 class SavedRuler(object):
21     __storm_table__ = "gallery"
22     id = Int(primary=True)
23     pixel_width = Float()
24     unit_width = Float()
25     model = Unicode()
26     units = Unicode()
27     show = Int()
28
29     def __init__(self, **kw):
30         self.pixel_width = kw['pixel_width']
31         self.unit_width = kw['unit_width']
32         self.units = kw['units']
33         self.model = kw['model']
34
35     def cm_width(self):
36         if self.units == 'centimeters':
37             return self.unit_width
38         elif self.units == 'inches':
39             return(self.unit_width * 2.54)
40
41     def in_width(self):
42         if self.units == 'inches':
43             return self.unit_width
44         elif self.units == 'centimeters':
45             return(self.unit_width * 0.3937)
46
47
48 class index:
49     def GET(self):
50         web.header("Content-Type","text/html; charset=utf-8")
51         web.render('index.tmpl')
52
53     def POST(self):
54         input = web.input()
55
56         pixel_width = input['pixel_width']
57         unit_width = input['unit_width']
58         units = input['units']
59         ruler_url = 'ruler_%spx_%s%s.png' % (pixel_width, unit_width, units)
60         web.redirect('/show/%s' % ruler_url)
61
62 class ruler:
63     def GET(self, ruler_url, ext):
64         web.debug('test test')
65         if web.input().has_key('fromgallery'):
66             fromgallery = True
67         else:
68             fromgallery = False
69
70         other_unit, other_unit_url = get_other_units(ruler_url)
71
72         web.header("Content-Type","text/html; charset=utf-8")
73         web.render('show_ruler.tmpl')
74
75
76 class ruler_img:
77     def GET(self, pixel_width=None, unit_width=None, units=None, ext=None):
78
79         # TODO check to see if it's a format that we support
80
81         # set ruler height to be 200 px always
82         pixel_width = float(pixel_width)
83         unit_width = float(unit_width)
84
85         ruler_height = 200
86
87         scale = pixel_width / unit_width
88         ruler_length = int(unit_width)
89
90         ruler = Ruler(scale, units, ruler_height, ruler_length)
91
92         # print the header
93         if ext == 'svg': ext = 'svg+xml'
94         web.header("Content-Type", "image/%s" % ext)
95
96         if ext == 'svg+xml':
97             sys.stdout.write(ruler.output())
98         else:
99             pin, pout = os.popen2('convert -size %sx%s - %s:-' % \
100                                   (pixel_width, ruler_height, ext))
101
102             pin.write(ruler.output())
103             pin.close()
104             sys.stdout.write(pout.read())
105
106 class gallery:
107     def GET(self, ruler_url):
108
109         if ruler_url:
110             pixel_width, unit_width, units = process_ruler_url(ruler_url)[0:3]
111
112         rulers = store.find(SavedRuler, SavedRuler.show == 1)
113         rulers.order_by(SavedRuler.model)
114
115         web.render('gallery.tmpl')
116
117     def POST(self):
118         input = web.input()
119
120         new_ruler = SavedRuler(pixel_width=float(input['pixel_width']),
121                                unit_width=float(input['unit_width']),
122                                units=unicode(input['units']),
123                                model=unicode(input['model']))
124
125         store.add(new_ruler)
126         store.commit()
127
128         web.redirect('gallery')
129
130 class delete:
131     def GET(self, id):
132         ruler = store.get(SavedRuler, int(id))
133         ruler.show = 0
134         store.commit()
135         web.redirect('/gallery')
136
137
138 class undelete:
139     def GET(self, id):
140         ruler = store.get(SavedRuler, int(id))
141         ruler.show = 1
142         store.commit()
143         web.redirect('/gallery')
144
145 def get_other_units(url):
146     pixel_width, unit_width, units = process_ruler_url(url)[0:3]
147     ruler = SavedRuler(pixel_width=float(pixel_width),
148                        unit_width=float(unit_width),
149                        units=unicode(units),
150                        model=unicode(model))
151
152 def process_ruler_url(url):
153     url = re.sub(r'^/?(ruler.*)$', r'\1', url)
154     return(re.match( r'ruler_([\d\.]+)px_([\d\.]+)(\w+).(png|svg|jpg)',
155            url).groups())
156
157 # render the site template here so that i can use it later
158 web.render('site.tmpl', None, True, 'site')
159
160 web.webapi.internalerror = web.debugerror
161 if __name__ == "__main__":
162     web.run(urls, globals(), web.reloader)
163

Benjamin Mako Hill || Want to submit a patch?