RSS on Turbogears 2.4

14 views
Skip to first unread message

Mikhail

unread,
Apr 4, 2020, 11:48:20 AM4/4/20
to TurboGears
Hello!
I continue to port my project from some old version of Turbogears to 2.4.

To generate RSS feed I've used webhelpers.feedgenerator which seems outdated. Newer WebHelpers2 have not built in feed generators and recommend to use Mako templates.

So my decision was to use Kajiki engine we already have to generate feed. The issue is Kajiki refuses to process this required xml part:

<?xml version="1.0" encoding="utf-8" ?>

on top of the template file with error

kajiki.xml_template.XMLTemplateParseError

kajiki.xml_template.XMLTemplateParseError: [/home/uni/projects/cafealpha-tg23/venv/cafealpha/cafealpha/templates/rssfeed.xhtml:1] XML or text declaration not at start of entity
        
        
     --> <!DOCTYPE kajiki SYSTEM "kajiki.dtd"><?xml version="1.0" encoding="utf-8" ?>
         <rss version="2.0">
           <channel>


Setting first tag to <!DOCTYPE xml> doesn't help.

Does anyone know right way to generate RSS or Atom feed using latest TurboGears?


Mikhail

unread,
Apr 10, 2020, 8:51:12 AM4/10/20
to TurboGears
Continuation of the story.

I've looked into few feed generating libraries but they either seems outdated or too large and complex for my needs.
So for now I just decided to generate very simple RSS XML manually:

    @expose()
    def rssfeed(self):
        feed = u'<?xml version="1.0" encoding="utf-8"?>' \
               '<rss version="2.0">'\
               '<channel>' \
               '<title>Кафе Альфа — новости</title>' \
               '<link>http://127.0.0.1:8000</link>' \
               '<description>Новости команды Кафе Альфа</description><language>ru</language>'
        feed += '<lastBuildDate>{0}</lastBuildDate>'.format(datetime.today())
        for news in DBSession.query(News).order_by(News.id.desc()).limit(5).all():  # last 5 news
            item = u'<item>' \
                   u'<title>{title}</title>' \
                   u'<link>{link}</link>' \
                   u'<description>{description}</description>' \
                   u'<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">{author}</dc:creator>' \
                   u'<pubDate>{date}</pubDate>' \
                   u'</item>'.format(title=news.title, link=news.url, description=news.text,
                                     author=news.author.display_name, date=news.date)
            feed += item
        feed += u'</channel></rss>'
        return feed




суббота, 4 апреля 2020 г., 18:48:20 UTC+3 пользователь Mikhail написал:
Reply all
Reply to author
Forward
0 new messages