changeset 3:56ab6b90cd1a

[mq]: post
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 21:29:57 -0700
parents 2464e2051b78
children dd1c4916cbcd
files example/avatar.jpg simplewiki/dispatcher.py simplewiki/handlers.py simplewiki/templates/index.html
diffstat 4 files changed, 34 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
Binary file example/avatar.jpg has changed
--- a/simplewiki/dispatcher.py	Tue Sep 07 21:00:46 2010 -0700
+++ b/simplewiki/dispatcher.py	Tue Sep 07 21:29:57 2010 -0700
@@ -5,7 +5,7 @@
 
 import os
 
-from handlers import GenshiRenderer, Index
+from handlers import GenshiRenderer, Index, Post
 
 from genshi.template import TemplateLoader
 from paste.fileapp import FileApp
@@ -30,7 +30,7 @@
         assert self.directory and os.path.exists(self.directory), "Must specify an existing directory"
 
         # request handlers
-        self.handlers = [ GenshiRenderer, Index ]
+        self.handlers = [ Post, GenshiRenderer, Index ]
 
         # template loader
         self.template_dirs = self.template_dirs.split()
--- a/simplewiki/handlers.py	Tue Sep 07 21:00:46 2010 -0700
+++ b/simplewiki/handlers.py	Tue Sep 07 21:29:57 2010 -0700
@@ -116,3 +116,29 @@
         return Response(content_type='text/html',
                         body=template.generate(**self.data).render('html'))
 
+class Post(Handler):
+    methods = set(['POST']) # methods to listen to
+
+    def __init__(self, app, request):
+        Handler.__init__(self, app, request)
+        if 'file' not in request.POST:
+            raise HandlerMatchException
+        self.file = self.request.POST['file']
+        if not getattr(self.file, 'filename', None):
+            raise HandlerMatchException
+        self.location = request.path_info.rstrip('/')
+        path = os.path.join(self.app.directory, *self.request.environ['path'])
+        if os.path.isdir(path):
+            self.directory = path
+            self.filename = os.path.join(self.directory, self.file.filename)
+            self.location += '/' + self.file.filename
+        else:
+            self.directory = os.path.dirname(path)
+            self.filename = path
+        
+        f = file(self.filename, 'wb')
+        f.write(self.file.file.read())
+        f.close()
+        
+    def __call__(self):
+        self.redirect(self.location)
--- a/simplewiki/templates/index.html	Tue Sep 07 21:00:46 2010 -0700
+++ b/simplewiki/templates/index.html	Tue Sep 07 21:29:57 2010 -0700
@@ -12,5 +12,11 @@
   <li py:if="request.path_info != '/'"><a href="..">..</a></li>
   <li py:for="f in files"><a href="${f}">${f}</a></li>
 </ul>
+<form method="post" enctype="multipart/form-data">
+<p>Upload a file:</p>
+<input type="file" name="file"/>
+<input type="submit"/>
+</form>
+
 </body>
 </html>