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, |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 20 | blog.title as title, (select count(name) from `tabComment` where |
| 21 | comment_doctype='Blog' and comment_docname=blog.name) as comments |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 22 | from `tabWeb Cache` cache, `tabBlog` blog |
| 23 | where cache.doc_type = 'Blog' and blog.page_name = cache.name |
| 24 | order by published desc, name asc""" |
| 25 | |
| 26 | from webnotes.widgets.query_builder import add_limit_to_query |
| 27 | query, args = add_limit_to_query(query, args) |
| 28 | |
| 29 | result = webnotes.conn.sql(query, args, as_dict=1) |
| 30 | |
| 31 | # strip html tags from content |
| 32 | import webnotes.utils |
| 33 | import website.web_cache |
| 34 | |
| 35 | for res in result: |
| 36 | from webnotes.utils import global_date_format, get_fullname |
| 37 | res['full_name'] = get_fullname(res['owner']) |
| 38 | res['published'] = global_date_format(res['published']) |
Anand Doshi | b3a4c09 | 2012-07-27 14:39:27 +0530 | [diff] [blame] | 39 | if not res['content']: |
| 40 | res['content'] = website.web_cache.get_html(res['name']) |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 41 | res['content'] = split_blog_content(res['content']) |
| 42 | res['content'] = res['content'][:1000] |
| 43 | |
| 44 | return result |
| 45 | |
| 46 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 47 | def get_recent_blog_list(args=None): |
| 48 | """ |
| 49 | args = { |
| 50 | 'limit_start': 0, |
| 51 | 'limit_page_length': 5, |
| 52 | 'name': '', |
| 53 | } |
| 54 | """ |
| 55 | import webnotes |
| 56 | |
| 57 | if not args: args = webnotes.form_dict |
| 58 | |
| 59 | query = """\ |
Anand Doshi | c7d5fc4 | 2012-07-31 19:15:01 +0530 | [diff] [blame] | 60 | select name, page_name, title, left(content, 100) as content |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 61 | from tabBlog |
| 62 | where ifnull(published,0)=1 and |
| 63 | name!=%(name)s order by creation desc""" |
| 64 | |
| 65 | from webnotes.widgets.query_builder import add_limit_to_query |
| 66 | query, args = add_limit_to_query(query, args) |
| 67 | |
| 68 | result = webnotes.conn.sql(query, args, as_dict=1) |
| 69 | |
| 70 | # strip html tags from content |
| 71 | import webnotes.utils |
| 72 | for res in result: |
| 73 | res['content'] = webnotes.utils.strip_html(res['content']) |
| 74 | |
| 75 | return result |
| 76 | |
| 77 | @webnotes.whitelist(allow_guest=True) |
| 78 | def add_comment(args=None): |
| 79 | """ |
| 80 | args = { |
| 81 | 'comment': '', |
| 82 | 'comment_by': '', |
| 83 | 'comment_by_fullname': '', |
| 84 | 'comment_doctype': '', |
| 85 | 'comment_docname': '', |
| 86 | 'page_name': '', |
| 87 | } |
| 88 | """ |
| 89 | import webnotes |
Rushabh Mehta | fa0e252 | 2012-08-13 11:09:21 +0530 | [diff] [blame] | 90 | import webnotes.utils, markdown2 |
| 91 | import webnotes.widgets.form.comments |
| 92 | import website.web_cache |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 93 | |
| 94 | if not args: args = webnotes.form_dict |
Anand Doshi | 25dd92d | 2012-08-17 23:36:11 +0530 | [diff] [blame] | 95 | args['comment'] = unicode(markdown2.markdown(args.get('comment') or '')) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 96 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 97 | comment = webnotes.widgets.form.comments.add_comment(args) |
| 98 | |
| 99 | # since comments are embedded in the page, clear the web cache |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 100 | website.web_cache.clear_cache(args.get('page_name'), |
| 101 | args.get('comment_doctype'), args.get('comment_docname')) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 102 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 103 | |
Anand Doshi | 7e657d7 | 2012-08-23 17:15:13 +0530 | [diff] [blame] | 104 | comment['comment_date'] = webnotes.utils.global_date_format(comment['creation']) |
Anand Doshi | 3d53e86 | 2012-07-12 20:12:40 +0530 | [diff] [blame] | 105 | template_args = { 'comment_list': [comment], 'template': 'html/comment.html' } |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 106 | |
| 107 | # get html of comment row |
Anand Doshi | 3d53e86 | 2012-07-12 20:12:40 +0530 | [diff] [blame] | 108 | comment_html = website.web_cache.build_html(template_args) |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 109 | |
| 110 | # notify commentors |
| 111 | commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where |
| 112 | comment_doctype='Blog' and comment_docname=%s and |
| 113 | ifnull(unsubscribed, 0)=0""", args.get('comment_docname'))] |
| 114 | |
| 115 | blog = webnotes.conn.sql("""select * from tabBlog where name=%s""", |
| 116 | args.get('comment_docname'), as_dict=1)[0] |
| 117 | |
| 118 | from webnotes.utils.email_lib.bulk import send |
Rushabh Mehta | c3de42c | 2012-08-13 11:31:27 +0530 | [diff] [blame] | 119 | send(recipients=list(set(commentors + [blog['owner']])), |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 120 | doctype='Comment', |
| 121 | email_field='comment_by', |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 122 | subject='New Comment on Blog: ' + blog['title'], |
Rushabh Mehta | fa0e252 | 2012-08-13 11:09:21 +0530 | [diff] [blame] | 123 | message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args) |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 124 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 125 | return comment_html |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 126 | |
Rushabh Mehta | 4b3b25e | 2012-08-03 14:10:59 +0530 | [diff] [blame] | 127 | @webnotes.whitelist(allow_guest=True) |
| 128 | def add_subscriber(): |
| 129 | """add blog subscriber to lead""" |
| 130 | full_name = webnotes.form_dict.get('your_name') |
| 131 | email = webnotes.form_dict.get('your_email_address') |
| 132 | name = webnotes.conn.sql("""select name from tabLead where email_id=%s""", email) |
| 133 | |
| 134 | from webnotes.model.doc import Document |
| 135 | if name: |
| 136 | lead = Document('Lead', name[0][0]) |
| 137 | else: |
| 138 | lead = Document('Lead') |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 139 | |
| 140 | if not lead.source: lead.source = 'Blog' |
Rushabh Mehta | 4b3b25e | 2012-08-03 14:10:59 +0530 | [diff] [blame] | 141 | lead.unsubscribed = 0 |
| 142 | lead.blog_subscriber = 1 |
| 143 | lead.lead_name = full_name |
| 144 | lead.email_id = email |
| 145 | lead.save() |
| 146 | |
Anand Doshi | 200c443 | 2012-07-13 01:14:52 +0530 | [diff] [blame] | 147 | def get_blog_content(blog_page_name): |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 148 | import website.web_cache |
| 149 | content = website.web_cache.get_html(blog_page_name) |
| 150 | |
| 151 | content = split_blog_content(content) |
| 152 | |
| 153 | import webnotes.utils |
| 154 | content = webnotes.utils.escape_html(content) |
| 155 | |
| 156 | return content |
| 157 | |
| 158 | def split_blog_content(content): |
| 159 | content = content.split("<!-- begin blog content -->") |
| 160 | content = len(content) > 1 and content[1] or content[0] |
| 161 | |
| 162 | content = content.split("<!-- end blog content -->") |
| 163 | content = content[0] |
| 164 | |
| 165 | return content |