changeset 5:b2fbb4f982da default tip

[mq]: edit
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 22:58:11 -0700
parents dd1c4916cbcd
children
files example/hello.html simplewiki/dispatcher.py simplewiki/handlers.py
diffstat 3 files changed, 45 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/example/hello.html	Tue Sep 07 22:39:15 2010 -0700
+++ b/example/hello.html	Tue Sep 07 22:58:11 2010 -0700
@@ -1,8 +1,8 @@
-<html>
-<head>
-<title>Hello, world!</title>
-</head>
-<body>
-Hello, world!
-</body>
-</html>
+<html>
+<head>
+<title>Hello, world!</title>
+</head>
+<body>
+Hello, worldz!
+</body>
+</html>
--- a/simplewiki/dispatcher.py	Tue Sep 07 22:39:15 2010 -0700
+++ b/simplewiki/dispatcher.py	Tue Sep 07 22:58:11 2010 -0700
@@ -5,7 +5,7 @@
 
 import os
 
-from handlers import GenshiRenderer, Index, Post, FileServer
+from handlers import GenshiRenderer, Index, Post, FileServer, EditView
 
 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 = [ Post, GenshiRenderer, Index, FileServer ]
+        self.handlers = [ Post, EditView, GenshiRenderer, Index, FileServer ]
 
         # template loader
         self.template_dirs = self.template_dirs.split()
--- 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'))