Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | """ |
| 6 | Generate RSS feed for blog |
| 7 | """ |
| 8 | |
| 9 | rss = 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 | |
| 22 | rss_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 Mehta | c08e103 | 2013-03-15 11:36:43 +0530 | [diff] [blame] | 28 | <pubDate>%(published_on)s</pubDate> |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 29 | </item>""" |
| 30 | |
| 31 | def generate(): |
| 32 | """generate rss feed""" |
Anand Doshi | 488a821 | 2013-05-22 15:51:47 +0530 | [diff] [blame] | 33 | import os, urllib |
| 34 | import webnotes |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 35 | from webnotes.model.doc import Document |
Anand Doshi | 488a821 | 2013-05-22 15:51:47 +0530 | [diff] [blame] | 36 | from webnotes.utils import escape_html |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 37 | |
| 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 Mehta | c08e103 | 2013-03-15 11:36:43 +0530 | [diff] [blame] | 42 | select page_name as name, published_on, modified, title, content from `tabBlog Post` |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 43 | where ifnull(published,0)=1 |
Rushabh Mehta | c08e103 | 2013-03-15 11:36:43 +0530 | [diff] [blame] | 44 | order by published_on desc limit 20""", as_dict=1) |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 45 | |
| 46 | for blog in blog_list: |
Anand Doshi | 488a821 | 2013-05-22 15:51:47 +0530 | [diff] [blame] | 47 | blog.link = urllib.quote(host + '/' + blog.name + '.html') |
| 48 | blog.content = escape_html(blog.content or "") |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 49 | |
| 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') |