Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 1 | import webnotes |
| 2 | |
| 3 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 4 | def get_blog_list(args=None): |
| 5 | """ |
| 6 | args = { |
| 7 | 'limit_start': 0, |
| 8 | 'limit_page_length': 10, |
| 9 | } |
| 10 | """ |
| 11 | import webnotes |
| 12 | |
| 13 | if not args: args = webnotes.form_dict |
| 14 | |
| 15 | query = """\ |
| 16 | select |
| 17 | cache.name as name, cache.html as content, |
| 18 | blog.owner as owner, blog.creation as published, |
| 19 | blog.title as title |
| 20 | from `tabWeb Cache` cache, `tabBlog` blog |
| 21 | where cache.doc_type = 'Blog' and blog.page_name = cache.name |
| 22 | order by published desc, name asc""" |
| 23 | |
| 24 | from webnotes.widgets.query_builder import add_limit_to_query |
| 25 | query, args = add_limit_to_query(query, args) |
| 26 | |
| 27 | result = webnotes.conn.sql(query, args, as_dict=1) |
| 28 | |
| 29 | # strip html tags from content |
| 30 | import webnotes.utils |
| 31 | import website.web_cache |
| 32 | |
| 33 | for res in result: |
| 34 | from webnotes.utils import global_date_format, get_fullname |
| 35 | res['full_name'] = get_fullname(res['owner']) |
| 36 | res['published'] = global_date_format(res['published']) |
| 37 | res['content'] = split_blog_content(res['content']) |
| 38 | res['content'] = res['content'][:1000] |
| 39 | |
| 40 | return result |
| 41 | |
| 42 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 43 | def get_recent_blog_list(args=None): |
| 44 | """ |
| 45 | args = { |
| 46 | 'limit_start': 0, |
| 47 | 'limit_page_length': 5, |
| 48 | 'name': '', |
| 49 | } |
| 50 | """ |
| 51 | import webnotes |
| 52 | |
| 53 | if not args: args = webnotes.form_dict |
| 54 | |
| 55 | query = """\ |
| 56 | select name, title, left(content, 100) as content |
| 57 | from tabBlog |
| 58 | where ifnull(published,0)=1 and |
| 59 | name!=%(name)s order by creation desc""" |
| 60 | |
| 61 | from webnotes.widgets.query_builder import add_limit_to_query |
| 62 | query, args = add_limit_to_query(query, args) |
| 63 | |
| 64 | result = webnotes.conn.sql(query, args, as_dict=1) |
| 65 | |
| 66 | # strip html tags from content |
| 67 | import webnotes.utils |
| 68 | for res in result: |
| 69 | res['content'] = webnotes.utils.strip_html(res['content']) |
| 70 | |
| 71 | return result |
| 72 | |
| 73 | @webnotes.whitelist(allow_guest=True) |
| 74 | def add_comment(args=None): |
| 75 | """ |
| 76 | args = { |
| 77 | 'comment': '', |
| 78 | 'comment_by': '', |
| 79 | 'comment_by_fullname': '', |
| 80 | 'comment_doctype': '', |
| 81 | 'comment_docname': '', |
| 82 | 'page_name': '', |
| 83 | } |
| 84 | """ |
| 85 | import webnotes |
| 86 | |
| 87 | if not args: args = webnotes.form_dict |
| 88 | |
| 89 | import webnotes.widgets.form.comments |
| 90 | comment = webnotes.widgets.form.comments.add_comment(args) |
| 91 | |
| 92 | # since comments are embedded in the page, clear the web cache |
| 93 | import website.web_cache |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 94 | website.web_cache.clear_cache(args.get('page_name'), |
| 95 | args.get('comment_doctype'), args.get('comment_docname')) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 96 | |
Anand Doshi | 4cf55c5 | 2012-07-13 02:16:22 +0530 | [diff] [blame] | 97 | # loads fresh blog into cache |
| 98 | get_blog_content(args.get('page_name')) |
| 99 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 100 | import webnotes.utils |
| 101 | |
| 102 | comment['comment_date'] = webnotes.utils.pretty_date(comment['creation']) |
Anand Doshi | 3d53e86 | 2012-07-12 20:12:40 +0530 | [diff] [blame] | 103 | template_args = { 'comment_list': [comment], 'template': 'html/comment.html' } |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 104 | |
| 105 | # get html of comment row |
Anand Doshi | 3d53e86 | 2012-07-12 20:12:40 +0530 | [diff] [blame] | 106 | comment_html = website.web_cache.build_html(template_args) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 107 | |
| 108 | return comment_html |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 109 | |
Anand Doshi | 200c443 | 2012-07-13 01:14:52 +0530 | [diff] [blame] | 110 | def get_blog_content(blog_page_name): |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 111 | import website.web_cache |
| 112 | content = website.web_cache.get_html(blog_page_name) |
| 113 | |
| 114 | content = split_blog_content(content) |
| 115 | |
| 116 | import webnotes.utils |
| 117 | content = webnotes.utils.escape_html(content) |
| 118 | |
| 119 | return content |
| 120 | |
| 121 | def split_blog_content(content): |
| 122 | content = content.split("<!-- begin blog content -->") |
| 123 | content = len(content) > 1 and content[1] or content[0] |
| 124 | |
| 125 | content = content.split("<!-- end blog content -->") |
| 126 | content = content[0] |
| 127 | |
| 128 | return content |