annotate uploader/handlers.py @ 0:827f7577f940

initial commit of file upload widget
author k0s <k0scist@gmail.com>
date Sat, 21 Nov 2009 15:29:03 -0500
parents
children 0b5fce452087
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
1 import os
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
2 from urlparse import urlparse
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
3 from webob import Response, exc
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
4
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
5 class Handler(object):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
6 def __init__(self, app, request):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
7 self.app = app
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
8 self.request = request
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
9 self.application_path = urlparse(request.application_url)[2]
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
10
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
11 def link(self, path=(), permanant=False):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
12 if isinstance(path, basestring):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
13 path = [ path ]
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
14 path = [ i.strip('/') for i in path ]
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
15 if permanant:
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
16 application_url = [ self.request.application_url ]
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
17 else:
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
18 application_url = [ self.application_path ]
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
19 path = application_url + path
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
20 return '/'.join(path)
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
21
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
22 def redirect(self, location):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
23 raise exc.HTTPSeeOther(location=location)
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
24
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
25 class Get(Handler):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
26
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
27 @classmethod
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
28 def match(cls, request):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
29 return request.method == 'GET'
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
30
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
31 def __call__(self):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
32 retval = """<html><body><form name="upload_form" method="post" enctype="multipart/form-data">
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
33 <input type="file" name="file"/><input type="submit" value="upload"/></form></body></html>"""
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
34 return Response(content_type='text/html', body=retval)
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
35
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
36 class Post(Handler):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
37
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
38 @classmethod
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
39 def match(cls, request):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
40 return request.method == 'POST'
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
41
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
42 def __call__(self):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
43 fin = self.request.POST['file']
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
44 assert os.sep not in fin.filename
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
45 assert '..' not in fin.filename
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
46 fout = file(os.path.join(self.app.directory, fin.filename), 'w')
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
47 fout.write(fin.file.read())
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
48 fout.close()
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
49 self.redirect(self.link('/'))
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
50