summaryrefslogtreecommitdiff
path: root/docs/tutorials/lxmlgraph/step03
diff options
context:
space:
mode:
authorPaul Everitt <paul@agendaless.com>2008-07-24 17:14:16 +0000
committerPaul Everitt <paul@agendaless.com>2008-07-24 17:14:16 +0000
commit1a7b14de4bc89ef0fd162ef8e62d01926e42b54e (patch)
tree8d9ccc8187ac29eb80ac310c6f82c17d548cdf54 /docs/tutorials/lxmlgraph/step03
parentc1a6ef6f751a16b7a352bc3fdad13d4b5b73f212 (diff)
downloadpyramid-1a7b14de4bc89ef0fd162ef8e62d01926e42b54e.tar.gz
pyramid-1a7b14de4bc89ef0fd162ef8e62d01926e42b54e.tar.bz2
pyramid-1a7b14de4bc89ef0fd162ef8e62d01926e42b54e.zip
Add tutorial sections
Diffstat (limited to 'docs/tutorials/lxmlgraph/step03')
-rw-r--r--docs/tutorials/lxmlgraph/step03/myapp/__init__.py1
-rw-r--r--docs/tutorials/lxmlgraph/step03/myapp/configure.zcml17
-rw-r--r--docs/tutorials/lxmlgraph/step03/myapp/default.pt8
-rw-r--r--docs/tutorials/lxmlgraph/step03/myapp/models.py41
-rw-r--r--docs/tutorials/lxmlgraph/step03/myapp/samplemodel.xml5
-rw-r--r--docs/tutorials/lxmlgraph/step03/myapp/views.py10
-rw-r--r--docs/tutorials/lxmlgraph/step03/myapp/xsltview.xsl12
-rw-r--r--docs/tutorials/lxmlgraph/step03/run.py8
8 files changed, 102 insertions, 0 deletions
diff --git a/docs/tutorials/lxmlgraph/step03/myapp/__init__.py b/docs/tutorials/lxmlgraph/step03/myapp/__init__.py
new file mode 100644
index 000000000..792d60054
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step03/myapp/__init__.py
@@ -0,0 +1 @@
+#
diff --git a/docs/tutorials/lxmlgraph/step03/myapp/configure.zcml b/docs/tutorials/lxmlgraph/step03/myapp/configure.zcml
new file mode 100644
index 000000000..ef340c283
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step03/myapp/configure.zcml
@@ -0,0 +1,17 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+ xmlns:bfg="http://namespaces.repoze.org/bfg">
+
+ <include package="repoze.bfg" />
+
+ <bfg:view
+ for=".models.IMyModel"
+ view=".views.zpt_default_view"
+ />
+
+ <bfg:view
+ for=".models.IMyModel"
+ view=".views.xslt_view"
+ name="xsltview.html"
+ />
+
+</configure>
diff --git a/docs/tutorials/lxmlgraph/step03/myapp/default.pt b/docs/tutorials/lxmlgraph/step03/myapp/default.pt
new file mode 100644
index 000000000..3cc98ef92
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step03/myapp/default.pt
@@ -0,0 +1,8 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:tal="http://xml.zope.org/namespaces/tal">
+ <head></head>
+ <body>
+ <h1>My template is viewing item: ${name}</h1>
+ <p>The node has a tag name of: ${node.tag}.</p>
+ </body>
+</html>
diff --git a/docs/tutorials/lxmlgraph/step03/myapp/models.py b/docs/tutorials/lxmlgraph/step03/myapp/models.py
new file mode 100644
index 000000000..3c03de1a9
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step03/myapp/models.py
@@ -0,0 +1,41 @@
+from zope.interface import implements
+from zope.interface import Attribute
+from zope.interface import Interface
+from lxml import etree
+
+class IMyModel(Interface):
+ __name__ = Attribute('Name of the model instance')
+
+class BfgElement(etree.ElementBase):
+ """Handle access control and getitem behavior"""
+
+ implements(IMyModel)
+
+ @property
+ def __name__(self):
+ return self.xpath("@name")[0]
+
+ def __getitem__(self, child_name):
+ xp = "*[@name='%s']" % child_name
+ matches = self.xpath(xp)
+ if len(matches) == 0:
+ raise KeyError('No child found for %s' % child_name)
+ elif len(matches) > 1:
+ raise KeyError('More than one child for %s' % child_name)
+ else:
+ return matches[0]
+
+def get_root(environ):
+ # Setup the custom parser with our BfgElement behavior
+ parser_lookup = etree.ElementDefaultClassLookup(element=BfgElement)
+ parser = etree.XMLParser()
+ parser.set_element_class_lookup(parser_lookup)
+
+ # Now load the XML file
+ xmlstring = open("myapp/samplemodel.xml").read()
+ root = etree.XML(xmlstring, parser)
+
+ return root
+
+
+
diff --git a/docs/tutorials/lxmlgraph/step03/myapp/samplemodel.xml b/docs/tutorials/lxmlgraph/step03/myapp/samplemodel.xml
new file mode 100644
index 000000000..83678e5e9
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step03/myapp/samplemodel.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<site>
+ <document name="a"/>
+ <document name="b"/>
+</site>
diff --git a/docs/tutorials/lxmlgraph/step03/myapp/views.py b/docs/tutorials/lxmlgraph/step03/myapp/views.py
new file mode 100644
index 000000000..c77eca8fe
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step03/myapp/views.py
@@ -0,0 +1,10 @@
+from repoze.bfg.template import render_template_to_response
+from repoze.bfg.template import render_transform_to_response
+
+def zpt_default_view(context, request):
+ return render_template_to_response("default.pt",
+ name=context.__name__,
+ node=context)
+
+def xslt_view(context, request):
+ return render_transform_to_response("xsltview.xsl", context)
diff --git a/docs/tutorials/lxmlgraph/step03/myapp/xsltview.xsl b/docs/tutorials/lxmlgraph/step03/myapp/xsltview.xsl
new file mode 100644
index 000000000..4d759b15b
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step03/myapp/xsltview.xsl
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
+ <xsl:template match="/document">
+ <html>
+ <head/>
+ <body>
+ <h1>My template is viewing item: <xsl:value-of select="@name"/></h1>
+ <p>The node has a name of: <xsl:value-of select="name()"/>.</p>
+ </body>
+ </html>
+ </xsl:template>
+</xsl:stylesheet>
diff --git a/docs/tutorials/lxmlgraph/step03/run.py b/docs/tutorials/lxmlgraph/step03/run.py
new file mode 100644
index 000000000..1eac209dc
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step03/run.py
@@ -0,0 +1,8 @@
+from paste import httpserver
+
+from repoze.bfg import make_app
+from myapp.models import get_root
+import myapp
+
+app = make_app(get_root, myapp)
+httpserver.serve(app, host='0.0.0.0', port='5432')