From e3cef7a4fd4b1582a9e5e700918b592185bcaf46 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Tue, 26 Jan 2010 15:44:26 +0200 Subject: [PATCH] Initial RSS+Atom feeds. --- scribeengine/controllers/feed.py | 55 ++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/scribeengine/controllers/feed.py b/scribeengine/controllers/feed.py index 5a86c5c..7eb96c7 100644 --- a/scribeengine/controllers/feed.py +++ b/scribeengine/controllers/feed.py @@ -1,16 +1,57 @@ import logging -from pylons import request, response, session, tmpl_context as c -from pylons.controllers.util import abort, redirect_to +from feedformatter import Feed +import time -from scribeengine.lib.base import BaseController, render +from scribeengine.lib.base import * +from scribeengine.model.meta import Session +from scribeengine.model import Post, Variable log = logging.getLogger(__name__) class FeedController(BaseController): + def _generate_feed(self): + blog_title = Session.query(Variable).get(u'blog title').value + blog_slogan = Session.query(Variable).get(u'blog slogan').value + blog_link = request.environ[u'HTTP_HOST'] + if blog_link.endswith(u'/'): + blog_link = blog_link[:-1] + posts = Session.query(Post)\ + .filter_by(status=u'published')\ + .order_by(Post.created.desc())\ + .all() + # Create the feed + feed = Feed() + # Set the feed/channel level properties + feed.feed[u'title'] = blog_title + feed.feed[u'link'] = blog_link + u'/' + #feed.feed[u'author'] = blog_author + feed.feed[u'description'] = blog_slogan + for post in posts: + # Create an item + item = {} + item[u'title'] = post.title + item[u'link'] = blog_link + h.url_for_post(post) + item[u'author'] = post.user.nick + item[u'description'] = post.body + item[u'pubDate'] = post.created.timetuple() + #item[u'guid'] = "1234567890" + # Add item to feed + feed.items.append(item) + return feed + def index(self): - # Return a rendered template - #return render('/feed.mako') - # or, return a response - return 'Hello World' + h.redirect_to('/') + + def rss(self, id=u'2.0'): + feed = self._generate_feed() + if id == u'1.0': + return feed.format_rss1_string() + else: + return feed.format_rss2_string() + + def atom(self): + feed = self._generate_feed() + return feed.format_atom_string() +