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