blob: 577cde612329f0b511415045f0cd979cc0895c1c [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Rushabh Mehtaa494b882012-12-07 12:44:45 +05303
4from __future__ import unicode_literals
5"""
6Generate RSS feed for blog
7"""
8
9rss = u"""<?xml version="1.0" encoding="UTF-8" ?>
10<rss version="2.0">
11<channel>
12 <title>%(title)s</title>
13 <description>%(description)s</description>
14 <link>%(link)s</link>
15 <lastBuildDate>%(modified)s</lastBuildDate>
16 <pubDate>%(modified)s</pubDate>
17 <ttl>1800</ttl>
18 %(items)s
19</channel>
20</rss>"""
21
22rss_item = u"""
23<item>
24 <title>%(title)s</title>
25 <description>%(content)s</description>
26 <link>%(link)s</link>
27 <guid>%(name)s</guid>
Rushabh Mehtac08e1032013-03-15 11:36:43 +053028 <pubDate>%(published_on)s</pubDate>
Rushabh Mehtaa494b882012-12-07 12:44:45 +053029</item>"""
30
31def generate():
32 """generate rss feed"""
Anand Doshi488a8212013-05-22 15:51:47 +053033 import os, urllib
34 import webnotes
Rushabh Mehtaa494b882012-12-07 12:44:45 +053035 from webnotes.model.doc import Document
Anand Doshi488a8212013-05-22 15:51:47 +053036 from webnotes.utils import escape_html
Rushabh Mehtaa494b882012-12-07 12:44:45 +053037
38 host = (os.environ.get('HTTPS') and 'https://' or 'http://') + os.environ.get('HTTP_HOST')
39
40 items = ''
41 blog_list = webnotes.conn.sql("""\
Rushabh Mehtac08e1032013-03-15 11:36:43 +053042 select page_name as name, published_on, modified, title, content from `tabBlog Post`
Rushabh Mehtaa494b882012-12-07 12:44:45 +053043 where ifnull(published,0)=1
Rushabh Mehtac08e1032013-03-15 11:36:43 +053044 order by published_on desc limit 20""", as_dict=1)
Rushabh Mehtaa494b882012-12-07 12:44:45 +053045
46 for blog in blog_list:
Anand Doshi488a8212013-05-22 15:51:47 +053047 blog.link = urllib.quote(host + '/' + blog.name + '.html')
48 blog.content = escape_html(blog.content or "")
Rushabh Mehtaa494b882012-12-07 12:44:45 +053049
50 items += rss_item % blog
51
52 modified = max((blog['modified'] for blog in blog_list))
53
54 ws = Document('Website Settings', 'Website Settings')
55 return (rss % {
56 'title': ws.title_prefix,
57 'description': ws.description or (ws.title_prefix + ' Blog'),
58 'modified': modified,
59 'items': items,
60 'link': host + '/blog.html'
61 }).encode('utf-8', 'ignore')