changeset 622:dc35e9882390

GenshiTutorial: add Atom feeds.
author cmlenz
date Thu, 30 Aug 2007 22:49:48 +0000
parents d218020fb92a
children 9c89e22516b4
files examples/tutorial/geddit/controller.py examples/tutorial/geddit/static/layout.css examples/tutorial/geddit/templates/index.html examples/tutorial/geddit/templates/index.xml examples/tutorial/geddit/templates/info.html examples/tutorial/geddit/templates/info.xml
diffstat 6 files changed, 80 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/examples/tutorial/geddit/controller.py
+++ b/examples/tutorial/geddit/controller.py
@@ -20,6 +20,18 @@
         self.data = data
 
     @cherrypy.expose
+    @template.output('index.xml', method='xml')
+    def feed(self, id=None):
+        if id:
+            link = self.data.get(id)
+            if not link:
+                raise cherrypy.NotFound()
+            return template.render('info.xml', link=link)
+        else:
+            links = sorted(self.data.values(), key=operator.attrgetter('time'))
+            return template.render(links=links)
+
+    @cherrypy.expose
     @template.output('index.html')
     def index(self):
         links = sorted(self.data.values(), key=operator.attrgetter('time'))
@@ -27,8 +39,8 @@
 
     @cherrypy.expose
     @template.output('info.html')
-    def info(self, code):
-        link = self.data.get(code)
+    def info(self, id):
+        link = self.data.get(id)
         if not link:
             raise cherrypy.NotFound()
         return template.render(link=link)
@@ -54,8 +66,8 @@
 
     @cherrypy.expose
     @template.output('comment.html')
-    def comment(self, code, cancel=False, **data):
-        link = self.data.get(code)
+    def comment(self, id, cancel=False, **data):
+        link = self.data.get(id)
         if not link:
             raise cherrypy.NotFound()
         if cherrypy.request.method == 'POST':
--- a/examples/tutorial/geddit/static/layout.css
+++ b/examples/tutorial/geddit/static/layout.css
@@ -30,3 +30,11 @@
 ul.comments blockquote { color: #333; font-style: normal; margin: 0;
   padding: 0;
 }
+.action:link, .action:visited { background: #f3f3f3; border: 1px outset #ddd;
+  color: #666; font-size: 90%; padding: 0 .3em;
+}
+.action:link:hover, .action:visited:hover { background: #e8e8e8;
+  border-color: #aaa; color: #000; text-decoration: none;
+}
+
+form .error { color: #b00; }
--- a/examples/tutorial/geddit/templates/index.html
+++ b/examples/tutorial/geddit/templates/index.html
@@ -5,10 +5,12 @@
   <xi:include href="layout.html" />
   <head>
     <title>News</title>
+    <link rel="alternate" type="application/atom+xml" title="Geddit News"
+          href="${url('/feed/')}" />
   </head>
   <body>
     <h1>News</h1>
-    <p><a href="${url('/submit/')}">Submit new link</a></p>
+    <p><a class="action" href="${url('/submit/')}">Submit new link</a></p>
 
     <ol py:if="links" class="links">
       <li py:for="link in links">
new file mode 100644
--- /dev/null
+++ b/examples/tutorial/geddit/templates/index.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom"
+      xmlns:py="http://genshi.edgewall.org/">
+
+  <title>Geddit</title>
+  <id href="${url('/')}"/>
+  <link rel="alternate" href="${url('/')}" type="text/html"/>
+  <link rel="self" href="${url('/feed/')}" type="application/atom+xml"/>
+  <updated>${links[0].time.isoformat()}</updated>
+
+  <entry py:for="link in reversed(links)">
+    <title>${link.url}</title>
+    <link rel="alternate" href="${link.url}" type="text/html"/>
+    <id>${url('/info/%s/' % link.id)}</id>
+    <author>
+      <name>${link.username}</name>
+    </author>
+    <updated>${link.time.isoformat()}</updated>
+    <summary>${link.title}</summary>
+  </entry>
+
+</feed>
--- a/examples/tutorial/geddit/templates/info.html
+++ b/examples/tutorial/geddit/templates/info.html
@@ -5,14 +5,18 @@
   <xi:include href="layout.html" />
   <head>
     <title>${link.title}</title>
+    <link rel="alternate" type="application/atom+xml" title="Geddit: ${link.title}"
+          href="${url('/feed/%s/' % link.id)}" />
   </head>
   <body>
     <h1>${link.title}</h1>
     <a href="${link.url}">${link.url}</a><br />
     posted by ${link.username} at ${link.time.strftime('%x %X')}<br />
-    <a href="${url('/comment/%s/' % link.id)}">comment</a>
+
+    <p><a class="action" href="${url('/comment/%s/' % link.id)}">comment</a></p>
+
     <ul py:if="link.comments" class="comments">
-      <li py:for="comment in link.comments">
+      <li py:for="idx, comment in enumerate(link.comments)" id="comment$idx">
         <strong>${comment.username}</strong>
         at ${comment.time.strftime('%x %X')}
         <blockquote>${comment.content}</blockquote>
new file mode 100644
--- /dev/null
+++ b/examples/tutorial/geddit/templates/info.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom"
+      xmlns:py="http://genshi.edgewall.org/">
+
+  <title>Geddit: ${link.title}</title>
+  <id href="${url('/info/%s/' % link.id)}"/>
+  <link rel="alternate" href="${url('/info/%s/' % link.id)}" type="text/html"/>
+  <link rel="self" href="${url('/feed/%s/' % link.id)}" type="application/atom+xml"/>
+  <updated py:with="time=link.comments and link.comments)[-1].time or link.time">
+    ${time.isoformat()}
+  </updated>
+
+  <entry py:for="idx, comment in enumerate(reversed(link.comments))">
+    <title>Comment ${len(link.comments) - idx} on “${link.title}”</title>
+    <link rel="alternate" href="${url('/info/%s/' % link.id)}#comment${idx}"
+          type="text/html"/>
+    <id>${url('/info/%s/' % link.id)}#comment${idx}</id>
+    <author>
+      <name>${comment.username}</name>
+    </author>
+    <updated>${comment.time.isoformat()}</updated>
+    <summary>${comment.content}</summary>
+  </entry>
+
+</feed>
Copyright (C) 2012-2017 Edgewall Software