Mercurial > hg > uploader
comparison uploader/handlers.py @ 13:b8c636b0b567
make work for non pastescript frameworks
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 05 Jul 2011 23:37:52 -0700 |
parents | 40b76777a880 |
children | 916d45d4f921 |
comparison
equal
deleted
inserted
replaced
12:f60ad139acc1 | 13:b8c636b0b567 |
---|---|
18 application_url = [ self.application_path ] | 18 application_url = [ self.application_path ] |
19 path = application_url + path | 19 path = application_url + path |
20 return '/'.join(path) | 20 return '/'.join(path) |
21 | 21 |
22 def redirect(self, location): | 22 def redirect(self, location): |
23 raise exc.HTTPSeeOther(location=location) | 23 return exc.HTTPSeeOther(location=location) |
24 | 24 |
25 class Get(Handler): | 25 class Get(Handler): |
26 | 26 |
27 form = """<html><body><form name="upload_form" method="post" enctype="multipart/form-data"> | 27 form = """<form name="upload_form" method="post" enctype="multipart/form-data"> |
28 <input type="file" name="file"/><input type="submit" value="upload"/></form></body></html>""" | 28 <input type="file" name="file"/><input type="submit" value="upload"/></form></body></html>""" |
29 | 29 |
30 @classmethod | 30 @classmethod |
31 def match(cls, app, request): | 31 def match(cls, app, request): |
32 if app.query_string and (app.query_string not in request.GET): | 32 if app.query_string and (app.query_string not in request.GET): |
33 return False | 33 return False |
34 return request.method == 'GET' | 34 return request.method == 'GET' |
35 | 35 |
36 def __call__(self): | 36 def __call__(self): |
37 return Response(content_type='text/html', body=self.form) | 37 form = "<html><body>" |
38 if 'uploaded' in self.request.GET: | |
39 form += '<div>%s uploaded successfully</div>' % self.request.GET['uploaded'] | |
40 form += self.form + '</body></html>' | |
41 return Response(content_type='text/html', body=form) | |
38 | 42 |
39 class Post(Handler): | 43 class Post(Handler): |
40 | 44 |
41 @classmethod | 45 @classmethod |
42 def match(cls, app, request): | 46 def match(cls, app, request): |
51 | 55 |
52 def __call__(self): | 56 def __call__(self): |
53 fin = self.request.POST['file'] | 57 fin = self.request.POST['file'] |
54 _path = os.path.join(self.app.directory, fin.filename) | 58 _path = os.path.join(self.app.directory, fin.filename) |
55 self.write(fin, _path) | 59 self.write(fin, _path) |
56 self.redirect(self.link('/')) | 60 return self.redirect(self.link('/?uploaded=' + fin.filename)) |
57 | 61 |
58 def path(directory, request): | 62 def path(directory, request): |
59 if os.sep == '/': | 63 if os.sep == '/': |
60 return os.path.join(directory, request.path_info.strip('/')) | 64 return os.path.join(directory, request.path_info.strip('/')) |
61 return os.path.join(directory, *request.path_info.strip('/').split('/')) | 65 return os.path.join(directory, *request.path_info.strip('/').split('/')) |
83 def __call__(self): | 87 def __call__(self): |
84 fin = self.request.POST['file'] | 88 fin = self.request.POST['file'] |
85 _path = path(self.app.directory, self.request) | 89 _path = path(self.app.directory, self.request) |
86 _path = os.path.join(_path, fin.filename) | 90 _path = os.path.join(_path, fin.filename) |
87 self.write(fin, _path) | 91 self.write(fin, _path) |
88 self.redirect(self.link(self.request.path_info)) | 92 return self.redirect(self.link(self.request.path_info)) |
89 | 93 |