Initial RSS+Atom feeds.

This commit is contained in:
Raoul Snyman 2010-01-26 15:44:26 +02:00
parent 30b11b1cc4
commit e3cef7a4fd

View File

@ -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()