changeset 11:8a7731b2126a

include a post method, a nav bar, and some jquery fun
author k0s <k0scist@gmail.com>
date Sat, 07 Nov 2009 22:49:46 -0500
parents ffdfb79b30bf
children 224486ff2d7f
files genshi_view/template/+package+/dispatcher.py genshi_view/template/+package+/factory.py_tmpl genshi_view/template/+package+/handlers.py genshi_view/template/+package+/templates/index.html
diffstat 4 files changed, 61 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/genshi_view/template/+package+/dispatcher.py	Fri Nov 06 16:58:51 2009 -0500
+++ b/genshi_view/template/+package+/dispatcher.py	Sat Nov 07 22:49:46 2009 -0500
@@ -1,5 +1,6 @@
 """
-request dispatcher
+request dispatcher:
+data persisting across requests should go here
 """
 
 import os
@@ -14,7 +15,8 @@
 class Dispatcher(object):
 
     ### class level variables
-    defaults = { 'auto_reload': 'False'}
+    defaults = { 'auto_reload': 'False',
+                 'name': 'anonymous' }
 
     def __init__(self, **kw):
 
@@ -28,21 +30,22 @@
         # static files
         self.htdocs = resource_filename(__name__, 'static')
 
-        # template loader # to move???
+        # template loader
         templates_dir = resource_filename(__name__, 'templates')
         self.loader = TemplateLoader(templates_dir,
                                      auto_reload=self.auto_reload)
 
     ### methods dealing with HTTP
     def __call__(self, environ, start_response):
+
+        # get a request object
         request = Request(environ)
 
-        # serve static files
-        # TODO: put into a handler
-        filename = os.path.join(self.htdocs, request.path_info)
-        if request.path_info != '/' and os.path.exists(filename):
-            fileapp = FileApp(filename)
-            return fileapp(environ, start_response)
+        # get the path 
+        path = request.path_info.strip('/').split('/')
+        if path == ['']:
+            path = []
+        request.environ['path'] = path
 
         # match the request to a handler
         for h in self.handlers:
@@ -52,6 +55,13 @@
         else:
             handler = exc.HTTPNotFound
 
+        # add navigation links to handler [example]
+        handler.data.setdefault('links', [])
+        for h in self.handlers:
+            handler.data['links'].append((handler.link(h.handler_path), 
+                                          h.__name__))
+
+        # get response
         res = handler()
         return res(environ, start_response)
                                 
--- a/genshi_view/template/+package+/factory.py_tmpl	Fri Nov 06 16:58:51 2009 -0500
+++ b/genshi_view/template/+package+/factory.py_tmpl	Sat Nov 07 22:49:46 2009 -0500
@@ -7,7 +7,6 @@
 
 class PassthroughFileserver(object):
     """serve files if they exist"""
-    # XXX needed???
 
     def __init__(self, app, directory):
         self.app = app
--- a/genshi_view/template/+package+/handlers.py	Fri Nov 06 16:58:51 2009 -0500
+++ b/genshi_view/template/+package+/handlers.py	Sat Nov 07 22:49:46 2009 -0500
@@ -1,3 +1,8 @@
+"""
+request handlers:
+these are instantiated for every request, then called
+"""
+
 from urlparse import urlparse
 from webob import Response, exc
 
@@ -17,13 +22,15 @@
             return None
     
     def __init__(self, app, request):
+
+        # check the method
         if request.method not in self.methods:
             raise HandlerMatchException
-        self.path = request.path_info.strip('/').split('/')
-        if self.path == ['']:
-            self.path = []
-        if self.path[:len(self.handler_path)] != self.handler_path:
+
+        # check the path
+        if request.environ['path'][:len(self.handler_path)] != self.handler_path:
             raise HandlerMatchException
+
         self.app = app
         self.request = request
         self.application_path = urlparse(request.application_url)[2]
@@ -46,13 +53,29 @@
 
     def __init__(self, app, request):
         Handler.__init__(self, app, request)
-        self.data = { 'request': request }
+        self.data = { 'request': request,
+                      'link': self.link }
 
-    def __call__(self):        
+    def __call__(self):
+        return getattr(self, self.request.method.title())()
+
+    def Get(self):
+        # needs to have self.template set
         template = self.app.loader.load(self.template)
         return Response(content_type='text/html',
-                        body=template.generate(**self.data).render())
+                        body=template.generate(**self.data).render('html'))
 
 class Index(GenshiHandler):
     template = 'index.html'
+    methods=set(['GET', 'POST'])
 
+    def __init__(self, app, request):
+        GenshiHandler.__init__(self, app, request)
+
+    def Get(self):
+        self.data['name'] = self.request.remote_user or self.app.name
+        return GenshiHandler.Get(self)
+
+    def Post(self):
+        self.app.name = self.request.POST.get('name', self.app.name)
+        self.redirect(self.link(self.handler_path))
--- a/genshi_view/template/+package+/templates/index.html	Fri Nov 06 16:58:51 2009 -0500
+++ b/genshi_view/template/+package+/templates/index.html	Sat Nov 07 22:49:46 2009 -0500
@@ -2,13 +2,21 @@
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"
-      xmlns:py="http://genshi.edgewall.org/">
+      xmlns:py="http://genshi.edgewall.org/"
+      xmlns:xi="http://www.w3.org/2001/XInclude">
 <head>
 <title>Hello world</title>
-<!-- TODO: jquery fun -->
+<script src="${link('jquery.js')}"></script>
+<script type="text/javascript">
+$(document).ready(function(){
+$(".text-input").click(function(){
+$(this).replaceWith('<form method="post"><input type="text" name="name" value="${name}"/><input type="submit" value="Go!"/></form>');
+});
+});
+</script>
 </head>
 <body>
-<!-- TODO: xi:include -->
-Hello ${request.remote_user or 'world'}!
+<xi:include href="navigation.html" />
+Hello <span class="text-input">${name}</span>!
 </body>
 </html>