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