1. Christian Scholz
  2. quantumcore.dynamicpage

Commits

Christian Scholz  committed a7ac745

added a second example with Ajax dynamic loading

  • Participants
  • Parent commits 23d84da
  • Branches default

Comments (0)

Files changed (7)

File docs/example2/__init__.py

  • Ignore whitespace
Empty file added.

File docs/example2/document.py

View file
  • Ignore whitespace
+document = {
+    'title' : 'Title of the document',
+    'subtitle' : 'How I learned to love Python',
+    'content' : 'This is the content of this page. And I like it',
+    'author' : 'Christian Scholz',
+}

File docs/example2/main.py

View file
  • Ignore whitespace
+import wsgiref.simple_server
+from webob.exc import HTTPNotFound
+from page import render, ajax
+
+class Application(object):
+    """a WSGI application"""
+
+    def __call__(self, environ, start_response):
+        path = environ['PATH_INFO']
+        if path=="/":
+            return render(environ, start_response)
+        elif path=="/edit":
+            return render(environ, start_response, 'edit')
+        elif path=="/ajax/editform":
+            return ajax('editform', environ, start_response)
+        return HTTPNotFound()(environ, start_response)
+
+app = Application()
+wsgiref.simple_server.make_server('', 8080, app).serve_forever()

File docs/example2/page.py

View file
  • Ignore whitespace
+from quantumcore.dynamicpage import Page, PageVariant
+from quantumcore.dynamicpage import TemplateStore
+from quantumcore.dynamicpage import commands
+
+from document import document
+
+
+tmpls = TemplateStore()
+tmpls.add_from_package('master', __name__, 'templates/main.pt')
+tmpls.add_from_package('content', __name__, 'templates/content.pt')
+tmpls.add_from_package('editform', __name__, 'templates/editform.pt')
+
+class MainVariant(PageVariant):
+    """the main display of a page with a document and byline"""
+    
+    def byline(self):
+        """return the content for the byline"""
+        return u"Written by %s" %document['author']
+    
+    def content(self):
+        """return the main document content"""
+        return tmpls.get("content")(document=document)
+        
+class EditformVariant(MainVariant):
+    """a variant of the main page which renders the edit form for the content"""
+    
+    def content(self):
+        return tmpls.get("editform")(document=document)
+        
+class MainPage(Page):
+    """the page class binds the variants together"""
+    
+    default_template = "master"
+    default_variant = 'main'
+    
+    variants = {
+        'main' : MainVariant(),
+        'edit' : EditformVariant(),
+    }
+    
+    def editform(self, environ, start_response):
+        form = self.render_slot("content","edit")
+        
+        cmds = commands.Commands()
+        cmds.append(commands.remove('contents',form))
+        return cmds.render_wsgi(environ, start_response)
+        
+
+def render(environ, start_response, variant="main", **kw):
+    data = MainPage(tmpls).render_wsgi(environ, start_response, variant, **kw)
+    return data
+
+def ajax(action, environ, start_response, variant="main", **kw):
+    page = MainPage(tmpls)
+    
+    # find the method with the name <action>
+    m = getattr(page, action)
+    
+    # call the action with WSGI stuff
+    return m(environ, start_response)
+

File docs/example2/templates/content.pt

View file
  • Ignore whitespace
+<div>
+CONTENT
+</div>

File docs/example2/templates/editform.pt

View file
  • Ignore whitespace
+<div>
+EDITFORM
+</div>

File docs/example2/templates/main.pt

View file
  • Ignore whitespace
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+
+<html>
+  <head>
+      <title>Test</title>
+      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
+
+      
+      <style>
+        body { background: #f0f0f0; font-family: Helvetica, Arial, sans-serif; }
+        #container {margin: 0 auto; width: 750px;}
+        #footer {clear: both;}
+        #content {background: #fff; padding: 10px;}
+        #editbutton {display: inline-block; 
+                    padding: 3px 10px 3px 10px; 
+                    margin: 10px 0;
+                    text-decoration: none;
+                    color: #420;
+                    font-weight: bold;
+                    background: #fa0; 
+                    -webkit-box-shadow: 0px 0px 5px rgba(0,0,0,0.75);
+                    border: 2px solid #a20;}
+      </style>
+      <script>
+      //<![CDATA[
+      
+        // jQuery plugin
+        
+        (function($) {
+          $.dynamicpage = {};
+          $.dynamicpage.commands = {};
+          $.dynamicpage.process = function (data) {
+            for (var i=0; i<data.length; i++) {
+              action = data[i];
+              $.dynamicpage.commands[action.command](action.payload);
+            };
+          };
+        
+          $.dynamicpage.commands.replace_inner = function(payload) {
+              $("#"+payload.id).html(payload.content);
+          };         
+
+          $.dynamicpage.commands.call = function(payload) {
+              window[payload.name](payload.data);
+          };
+          
+          $.dynamicpage.commands.append = function(payload) {
+            var node = $(payload.content);
+            if (payload.slideDown || payload.fadeIn) {
+              node.hide();
+            }
+            $("#"+payload.id).append(node);
+            if (payload.slideDown) {
+              node.slideDown();
+            } else if (payload.fadeIn) {
+              node.fadeIn();
+            }
+          };
+          
+          $.dynamicpage.commands.remove = function(payload) {
+            $("#"+payload.id).remove();
+          }
+
+        })(jQuery);
+        
+        // our own code
+                
+        $(document).ready(function() {
+          $('#editbutton').click(function() {
+            $.getJSON('/ajax/editform', $.dynamicpage.process)
+            return false;
+          })
+        })
+        
+      //]]>        
+      </script>
+  </head>
+
+  <body>
+    <div id="container">
+        <div id="header">
+            <h1>Example Application</h1>
+        </div>
+        <div id="main">
+
+            <div id="contents">
+                <tal:block replace="structure page['content']" />
+            </div>
+
+            <a href="#" id="editbutton">Edit</a>
+        </div>
+
+        <div id="footer">
+          <tal:block replace="structure page['byline']" />
+        </div>
+    </div>
+  </body>
+</html>