Fixes related to rss feed

This commit is contained in:
Anand Doshi 2012-07-11 18:42:30 +05:30
parent f82d50c8ab
commit 1ac5a8ea0b
6 changed files with 64 additions and 25 deletions

View file

@ -38,7 +38,7 @@ class HTTPRequest:
if self.domain and self.domain.startswith('www.'):
self.domain = self.domain[4:]
webnotes.remote_ip = webnotes.get_env_vars('REMOTE_ADDR')
webnotes.remote_ip = webnotes.get_env_vars('REMOTE_ADDR')
# load cookies
webnotes.cookie_manager = CookieManager()

View file

@ -43,37 +43,42 @@ rss_item = u"""
<description>%(content)s</description>
<link>%(link)s</link>
<guid>%(name)s</guid>
<pubDate>%(modified)s</pubDate>
<pubDate>%(published)s</pubDate>
</item>"""
def generate():
"""generate rss feed"""
import webnotes, os
from webnotes.model.doc import Document
import webnotes.utils
host = (os.environ.get('HTTPS') and 'https://' or 'http://') + os.environ.get('HTTP_HOST')
items = ''
modified = None
for blog in webnotes.conn.sql("""select name, title, content, modified, page_name
from tabBlog
where ifnull(published,0)=1
order by modified desc limit 100""", as_dict=1):
blog['link'] = host + '/' + blog['page_name'] + '.html'
blog['content'] = scrub(blog['content'][:1000] or '')
if not modified:
modified = blog['modified']
blog_list = webnotes.conn.sql("""\
select
cache.name as name, cache.html as content,
cache.creation as published, cache.modified as modified,
(
select title from `tabBlog` blog
where blog.page_name = cache.name
) as title
from `tabWeb Cache` cache
where cache.doc_type = 'Blog'
order by creation desc limit 100""", as_dict=1)
for blog in blog_list:
blog['link'] = host + '/' + blog['name'] + '.html'
blog['content'] = webnotes.utils.escape_html((blog.get('content') or ''))
items += rss_item % blog
modified = max((blog['modified'] for blog in blog_list))
ws = Document('Website Settings', 'Website Settings')
return (rss % {
'title': ws.title_prefix,
'description': ws.description or (ws.title_prefix + ' Blog'),
'modified': modified,
'items': items,
'link': host + '/blog.html'
}).encode('utf-8', 'ignore')
def scrub(txt):
return txt.replace('<', '&lt;').replace('>', '&gt;')
'title': ws.title_prefix,
'description': ws.description or (ws.title_prefix + ' Blog'),
'modified': modified,
'items': items,
'link': host + '/blog.html'
}).encode('utf-8', 'ignore')

View file

@ -311,4 +311,4 @@ def peval_doclist(txt):
else:
return eval(txt)
return uncommonify_doclist(eval(txt))
return uncommonify_doclist(eval(txt))

View file

@ -283,7 +283,7 @@ def global_date_format(date):
if isinstance(date, basestring):
date = getdate(date)
return date.strftime('%d') + ' ' + month_name_full[int(date.strftime('%m'))] \
return cstr(cint(date.strftime('%d'))) + ' ' + month_name_full[int(date.strftime('%m'))] \
+ ' ' + date.strftime('%Y')
@ -655,6 +655,24 @@ def unesc(s, esc_chars):
esc_str = '\\' + c
s = s.replace(esc_str, c)
return s
def strip_html(text):
"""
removes anything enclosed in and including <>
"""
import re
return re.compile(r'<.*?>').sub('', text)
def escape_html(text):
html_escape_table = {
"&": "&amp;",
'"': "&quot;",
"'": "&apos;",
">": "&gt;",
"<": "&lt;",
}
return "".join(html_escape_table.get(c,c) for c in text)
def get_doctype_label(dt=None):
"""

View file

@ -42,10 +42,10 @@ def get_comments(doctype=None, docname=None, limit=5):
webnotes.response['n_comments'], webnotes.response['comment_list'] = nc, cl
@webnotes.whitelist(allow_guest=True)
def add_comment():
def add_comment(args=None):
"""add a new comment"""
import time
args = webnotes.form_dict
if not args: args = webnotes.form_dict
if args.get('comment'):
from webnotes.model.doc import Document
@ -60,6 +60,8 @@ def add_comment():
import startup.event_handlers
if hasattr(startup.event_handlers, 'comment_added'):
startup.event_handlers.comment_added(cmt)
return cmt.fields
@webnotes.whitelist()
def remove_comment():

View file

@ -346,3 +346,17 @@ def runquery_csv():
out['type'] = 'csv'
out['doctype'] = rep_name
def add_limit_to_query(query, args):
"""
Add limit condition to query
can be used by methods called in listing to add limit condition
"""
if args.get('limit_page_length'):
query += """
limit %(limit_start)s, %(limit_page_length)s"""
import webnotes.utils
args['limit_start'] = webnotes.utils.cint(args.get('limit_start'))
args['limit_page_length'] = webnotes.utils.cint(args.get('limit_page_length'))
return query, args