From 1ac5a8ea0be4c6d7291f6e71f92e0830f9e27ae2 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 11 Jul 2012 18:42:30 +0530 Subject: [PATCH] Fixes related to rss feed --- py/webnotes/auth.py | 2 +- py/webnotes/cms/feed.py | 45 +++++++++++++++------------- py/webnotes/model/utils.py | 2 +- py/webnotes/utils/__init__.py | 20 ++++++++++++- py/webnotes/widgets/form/comments.py | 6 ++-- py/webnotes/widgets/query_builder.py | 14 +++++++++ 6 files changed, 64 insertions(+), 25 deletions(-) diff --git a/py/webnotes/auth.py b/py/webnotes/auth.py index 67951cb6a2..d37cba3f60 100644 --- a/py/webnotes/auth.py +++ b/py/webnotes/auth.py @@ -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() diff --git a/py/webnotes/cms/feed.py b/py/webnotes/cms/feed.py index eab3007871..458ee2c27b 100644 --- a/py/webnotes/cms/feed.py +++ b/py/webnotes/cms/feed.py @@ -43,37 +43,42 @@ rss_item = u""" %(content)s %(link)s %(name)s - %(modified)s + %(published)s """ 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('<', '<').replace('>', '>') \ No newline at end of file + 'title': ws.title_prefix, + 'description': ws.description or (ws.title_prefix + ' Blog'), + 'modified': modified, + 'items': items, + 'link': host + '/blog.html' + }).encode('utf-8', 'ignore') \ No newline at end of file diff --git a/py/webnotes/model/utils.py b/py/webnotes/model/utils.py index dadfe63d93..4a74d4aa0b 100644 --- a/py/webnotes/model/utils.py +++ b/py/webnotes/model/utils.py @@ -311,4 +311,4 @@ def peval_doclist(txt): else: return eval(txt) - return uncommonify_doclist(eval(txt)) + return uncommonify_doclist(eval(txt)) \ No newline at end of file diff --git a/py/webnotes/utils/__init__.py b/py/webnotes/utils/__init__.py index ed0bf9002d..d28b281d88 100644 --- a/py/webnotes/utils/__init__.py +++ b/py/webnotes/utils/__init__.py @@ -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 = { + "&": "&", + '"': """, + "'": "'", + ">": ">", + "<": "<", + } + + return "".join(html_escape_table.get(c,c) for c in text) def get_doctype_label(dt=None): """ diff --git a/py/webnotes/widgets/form/comments.py b/py/webnotes/widgets/form/comments.py index b51fbe7e86..89f0bffd73 100644 --- a/py/webnotes/widgets/form/comments.py +++ b/py/webnotes/widgets/form/comments.py @@ -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(): diff --git a/py/webnotes/widgets/query_builder.py b/py/webnotes/widgets/query_builder.py index e2bd1fe75f..fea940c0ea 100644 --- a/py/webnotes/widgets/query_builder.py +++ b/py/webnotes/widgets/query_builder.py @@ -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 \ No newline at end of file