Mercurial > hg > uploader
comparison uploader/handlers.py @ 20:f1142bcce77a
add ability to log uploads
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 19 Jul 2011 08:08:58 -0700 |
parents | 4da97e040145 |
children | 183224756ded |
comparison
equal
deleted
inserted
replaced
19:4da97e040145 | 20:f1142bcce77a |
---|---|
1 import os | 1 import os |
2 import sys | |
2 from urlparse import urlparse | 3 from urlparse import urlparse |
3 from webob import Response, exc | 4 from webob import Response, exc |
4 | 5 |
5 class Handler(object): | 6 class Handler(object): |
6 def __init__(self, app, request): | 7 def __init__(self, app, request): |
43 if contents: | 44 if contents: |
44 contents.sort() | 45 contents.sort() |
45 form += '<div><i>Currently uploaded:<ul><li>' + '</li><li>'.join(contents) + '</li></ul></i></div>' | 46 form += '<div><i>Currently uploaded:<ul><li>' + '</li><li>'.join(contents) + '</li></ul></i></div>' |
46 else: | 47 else: |
47 form += '<div><i>No files in upload directory</i></div>' | 48 form += '<div><i>No files in upload directory</i></div>' |
49 log_file = self.app.log_file | |
50 if log_file and os.path.exists(log_file): | |
51 log_contents = file(log_file).read() | |
52 form += '<div>Upload log:<pre>%s</pre></div>' % log_contents | |
48 form += '</body></html>' | 53 form += '</body></html>' |
49 return Response(content_type='text/html', body=form) | 54 return Response(content_type='text/html', body=form) |
50 | 55 |
51 class Post(Handler): | 56 class Post(Handler): |
52 | 57 |
75 _path = _path.replace(i, '_') | 80 _path = _path.replace(i, '_') |
76 | 81 |
77 # write the file + redirect | 82 # write the file + redirect |
78 _path = os.path.join(self.app.directory, _path) | 83 _path = os.path.join(self.app.directory, _path) |
79 self.write(fin, _path) | 84 self.write(fin, _path) |
85 if self.app.log_file: | |
86 try: | |
87 f = file(self.app.log_file, 'a') | |
88 print >> f, fin.filename | |
89 f.close() | |
90 except Exception, e: | |
91 print >> sys.stderr, e | |
80 return self.redirect(self.link('/?uploaded=' + fin.filename)) | 92 return self.redirect(self.link('/?uploaded=' + fin.filename)) |
81 | 93 |
82 def path(directory, request): | 94 def path(directory, request): |
83 if os.sep == '/': | 95 if os.sep == '/': |
84 return os.path.join(directory, request.path_info.strip('/')) | 96 return os.path.join(directory, request.path_info.strip('/')) |