diff simplewiki/handlers.py @ 5:b2fbb4f982da default tip

[mq]: edit
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 22:58:11 -0700
parents dd1c4916cbcd
children
line wrap: on
line diff
--- a/simplewiki/handlers.py	Tue Sep 07 22:39:15 2010 -0700
+++ b/simplewiki/handlers.py	Tue Sep 07 22:58:11 2010 -0700
@@ -125,20 +125,24 @@
         if 'file' not in request.POST:
             raise HandlerMatchException
         self.file = self.request.POST['file']
-        if not getattr(self.file, 'filename', None):
-            raise HandlerMatchException
+        filename = getattr(self.file, 'filename', '')
+        if filename:
+            content = self.file.file.read()
+        else:
+            content = self.file
+
         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
+            self.filename = os.path.join(self.directory, filename)
+            self.location += '/' + filename
         else:
             self.directory = os.path.dirname(path)
             self.filename = path
         
         f = file(self.filename, 'wb')
-        f.write(self.file.file.read())
+        f.write(content)
         f.close()
         
     def __call__(self):
@@ -156,3 +160,29 @@
     def __call__(self):
         return FileApp(self.file)
         
+class EditView(Handler):
+    methods = set(['GET'])
+    template = 'edit.html'
+
+    def __init__(self, app, request):
+        if 'edit' not in request.GET:
+            raise HandlerMatchException
+        
+        Handler.__init__(self, app, request)
+
+        self.file = os.path.join(self.app.directory, *request.environ['path'])
+        if not os.path.exists(self.file):
+            raise HandlerMatchException
+        self.data = { 'request': request,
+                      'link': self.link,
+                      'file': '/' + '/'.join(request.environ['path']),
+                      'content': file(self.file).read()
+                      }
+
+    def __call__(self):
+        return getattr(self, self.request.method.title())()
+
+    def Get(self):
+        template = self.app.loader.load(self.template)
+        return Response(content_type='text/html',
+                        body=template.generate(**self.data).render('html'))