annotate uploader/handlers.py @ 7:a8c60480fce0

make query_string argument more universal
author k0s <k0scist@gmail.com>
date Sun, 27 Dec 2009 18:45:28 -0500
parents d2990750e5d6
children 619516d8c9ff
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
2
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
27 form = """<html><body><form name="upload_form" method="post" enctype="multipart/form-data">
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
28 <input type="file" name="file"/><input type="submit" value="upload"/></form></body></html>"""
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
29
0
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
30 @classmethod
2
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
31 def match(cls, app, request):
7
a8c60480fce0 make query_string argument more universal
k0s <k0scist@gmail.com>
parents: 6
diff changeset
32 if app.query_string and (app.query_string not in request.GET):
a8c60480fce0 make query_string argument more universal
k0s <k0scist@gmail.com>
parents: 6
diff changeset
33 return False
0
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
34 return request.method == 'GET'
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 def __call__(self):
2
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
37 return Response(content_type='text/html', body=self.form)
0
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
38
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
39 class Post(Handler):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
40
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
41 @classmethod
2
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
42 def match(cls, app, request):
0
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
43 return request.method == 'POST'
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
44
2
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
45 def write(self, fin, path):
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
46 assert os.sep not in fin.filename
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
47 assert '..' not in fin.filename
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
48 fout = file(path, 'w')
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
49 fout.write(fin.file.read())
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
50 fout.close()
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
51
0
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
52 def __call__(self):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
53 fin = self.request.POST['file']
2
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
54 _path = os.path.join(self.app.directory, fin.filename)
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
55 self.write(self, fin, _path)
0
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
56 self.redirect(self.link('/'))
2
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
57
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
58 def path(directory, request):
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
59 if os.sep == '/':
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
60 return os.path.join(directory, request.path_info.strip('/'))
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
61 return os.path.join(directory, *request.path_info.strip('/').split('/'))
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
62
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
63 class SubpathGet(Get):
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
64
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
65 @classmethod
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
66 def match(cls, app, request):
7
a8c60480fce0 make query_string argument more universal
k0s <k0scist@gmail.com>
parents: 6
diff changeset
67 if not Get.match(cls, app, request):
2
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
68 return False
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
69 _path = path(app.directory, request)
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
70 if os.path.exists(_path) and os.path.isdir(_path):
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
71 return True
0
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
72
2
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
73 class SubpathPost(Post):
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
74
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
75 @classmethod
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
76 def match(cls, app, request):
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
77 if request.method != 'POST':
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
78 return False
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
79 _path = path(app.directory, request)
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
80 if os.path.exists(_path) and os.path.isdir(_path):
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
81 return True
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
82
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
83 def __call__(self):
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
84 fin = self.request.POST['file']
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
85 _path = path(self.app.directory, request)
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
86 _path = os.path.join(path, fin.filename)
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
87 self.write(self, fin, _path)
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
88 self.redirect(self.link(request.path_info))
0b5fce452087 include handling of subpaths
k0s <k0scist@gmail.com>
parents: 0
diff changeset
89