Mercurial > hg > uploader
annotate uploader/handlers.py @ 11:40b76777a880
typos
author | egj@socialplanning.org |
---|---|
date | Sun, 10 Jan 2010 18:44:58 +0000 |
parents | f8faf3cabb02 |
children | b8c636b0b567 |
rev | line source |
---|---|
0 | 1 import os |
2 from urlparse import urlparse | |
3 from webob import Response, exc | |
4 | |
5 class Handler(object): | |
6 def __init__(self, app, request): | |
7 self.app = app | |
8 self.request = request | |
9 self.application_path = urlparse(request.application_url)[2] | |
10 | |
11 def link(self, path=(), permanant=False): | |
12 if isinstance(path, basestring): | |
13 path = [ path ] | |
14 path = [ i.strip('/') for i in path ] | |
15 if permanant: | |
16 application_url = [ self.request.application_url ] | |
17 else: | |
18 application_url = [ self.application_path ] | |
19 path = application_url + path | |
20 return '/'.join(path) | |
21 | |
22 def redirect(self, location): | |
23 raise exc.HTTPSeeOther(location=location) | |
24 | |
25 class Get(Handler): | |
26 | |
2 | 27 form = """<html><body><form name="upload_form" method="post" enctype="multipart/form-data"> |
28 <input type="file" name="file"/><input type="submit" value="upload"/></form></body></html>""" | |
29 | |
0 | 30 @classmethod |
2 | 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 | 34 return request.method == 'GET' |
35 | |
36 def __call__(self): | |
2 | 37 return Response(content_type='text/html', body=self.form) |
0 | 38 |
39 class Post(Handler): | |
40 | |
41 @classmethod | |
2 | 42 def match(cls, app, request): |
0 | 43 return request.method == 'POST' |
44 | |
2 | 45 def write(self, fin, path): |
46 assert os.sep not in fin.filename | |
47 assert '..' not in fin.filename | |
48 fout = file(path, 'w') | |
49 fout.write(fin.file.read()) | |
50 fout.close() | |
51 | |
0 | 52 def __call__(self): |
53 fin = self.request.POST['file'] | |
2 | 54 _path = os.path.join(self.app.directory, fin.filename) |
11 | 55 self.write(fin, _path) |
0 | 56 self.redirect(self.link('/')) |
2 | 57 |
58 def path(directory, request): | |
59 if os.sep == '/': | |
60 return os.path.join(directory, request.path_info.strip('/')) | |
61 return os.path.join(directory, *request.path_info.strip('/').split('/')) | |
62 | |
63 class SubpathGet(Get): | |
64 | |
65 @classmethod | |
66 def match(cls, app, request): | |
9 | 67 if not Get.match(app, request): |
2 | 68 return False |
69 _path = path(app.directory, request) | |
70 if os.path.exists(_path) and os.path.isdir(_path): | |
71 return True | |
0 | 72 |
2 | 73 class SubpathPost(Post): |
74 | |
75 @classmethod | |
76 def match(cls, app, request): | |
77 if request.method != 'POST': | |
78 return False | |
79 _path = path(app.directory, request) | |
80 if os.path.exists(_path) and os.path.isdir(_path): | |
81 return True | |
82 | |
83 def __call__(self): | |
84 fin = self.request.POST['file'] | |
10 | 85 _path = path(self.app.directory, self.request) |
86 _path = os.path.join(_path, fin.filename) | |
87 self.write(fin, _path) | |
88 self.redirect(self.link(self.request.path_info)) | |
2 | 89 |