Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 1 | # Copyright (c) 2012 Web Notes Technologies Pvt Ltd. |
| 2 | # License: GNU General Public License (v3). For more information see license.txt |
| 3 | |
Anand Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 4 | from __future__ import unicode_literals |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 5 | import webnotes |
Anand Doshi | baf4f64 | 2012-12-07 19:53:18 +0530 | [diff] [blame] | 6 | import website.utils |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 7 | |
| 8 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 9 | def get_blog_list(args=None): |
| 10 | """ |
| 11 | args = { |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 12 | 'start': 0, |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 13 | } |
| 14 | """ |
| 15 | import webnotes |
| 16 | |
| 17 | if not args: args = webnotes.form_dict |
| 18 | |
| 19 | query = """\ |
| 20 | select |
Anand Doshi | f437899 | 2012-12-07 19:59:25 +0530 | [diff] [blame] | 21 | name, page_name, content, owner, creation as creation, |
Rushabh Mehta | 87192da | 2012-12-06 16:45:00 +0530 | [diff] [blame] | 22 | title, (select count(name) from `tabComment` where |
Anand Doshi | 67327a3 | 2012-12-06 20:13:57 +0530 | [diff] [blame] | 23 | comment_doctype='Blog' and comment_docname=`tabBlog`.name) as comments |
Rushabh Mehta | 87192da | 2012-12-06 16:45:00 +0530 | [diff] [blame] | 24 | from `tabBlog` |
| 25 | where ifnull(published,0)=1 |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 26 | order by creation desc, name asc |
| 27 | limit %s, 5""" % args.start |
| 28 | |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 29 | result = webnotes.conn.sql(query, args, as_dict=1) |
| 30 | |
| 31 | # strip html tags from content |
| 32 | import webnotes.utils |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 33 | |
| 34 | for res in result: |
| 35 | from webnotes.utils import global_date_format, get_fullname |
| 36 | res['full_name'] = get_fullname(res['owner']) |
Rushabh Mehta | 87192da | 2012-12-06 16:45:00 +0530 | [diff] [blame] | 37 | res['published'] = global_date_format(res['creation']) |
Anand Doshi | b3a4c09 | 2012-07-27 14:39:27 +0530 | [diff] [blame] | 38 | if not res['content']: |
Anand Doshi | f437899 | 2012-12-07 19:59:25 +0530 | [diff] [blame] | 39 | res['content'] = website.utils.get_html(res['page_name']) |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 40 | res['content'] = split_blog_content(res['content']) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 41 | |
| 42 | return result |
| 43 | |
| 44 | @webnotes.whitelist(allow_guest=True) |
| 45 | def add_comment(args=None): |
| 46 | """ |
| 47 | args = { |
| 48 | 'comment': '', |
| 49 | 'comment_by': '', |
| 50 | 'comment_by_fullname': '', |
| 51 | 'comment_doctype': '', |
| 52 | 'comment_docname': '', |
| 53 | 'page_name': '', |
| 54 | } |
| 55 | """ |
| 56 | import webnotes |
Rushabh Mehta | fa0e252 | 2012-08-13 11:09:21 +0530 | [diff] [blame] | 57 | import webnotes.utils, markdown2 |
| 58 | import webnotes.widgets.form.comments |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 59 | |
| 60 | if not args: args = webnotes.form_dict |
Anand Doshi | 25dd92d | 2012-08-17 23:36:11 +0530 | [diff] [blame] | 61 | args['comment'] = unicode(markdown2.markdown(args.get('comment') or '')) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 62 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 63 | comment = webnotes.widgets.form.comments.add_comment(args) |
| 64 | |
| 65 | # since comments are embedded in the page, clear the web cache |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 66 | website.utils.clear_cache(args.get('page_name')) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 67 | |
Anand Doshi | 7e657d7 | 2012-08-23 17:15:13 +0530 | [diff] [blame] | 68 | comment['comment_date'] = webnotes.utils.global_date_format(comment['creation']) |
Anand Doshi | 3d53e86 | 2012-07-12 20:12:40 +0530 | [diff] [blame] | 69 | template_args = { 'comment_list': [comment], 'template': 'html/comment.html' } |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 70 | |
| 71 | # get html of comment row |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 72 | comment_html = website.utils.build_html(template_args) |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 73 | |
| 74 | # notify commentors |
| 75 | commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where |
| 76 | comment_doctype='Blog' and comment_docname=%s and |
| 77 | ifnull(unsubscribed, 0)=0""", args.get('comment_docname'))] |
| 78 | |
| 79 | blog = webnotes.conn.sql("""select * from tabBlog where name=%s""", |
| 80 | args.get('comment_docname'), as_dict=1)[0] |
| 81 | |
| 82 | from webnotes.utils.email_lib.bulk import send |
Rushabh Mehta | c3de42c | 2012-08-13 11:31:27 +0530 | [diff] [blame] | 83 | send(recipients=list(set(commentors + [blog['owner']])), |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 84 | doctype='Comment', |
| 85 | email_field='comment_by', |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 86 | subject='New Comment on Blog: ' + blog['title'], |
Rushabh Mehta | fa0e252 | 2012-08-13 11:09:21 +0530 | [diff] [blame] | 87 | message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args) |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 88 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 89 | return comment_html |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 90 | |
Rushabh Mehta | 4b3b25e | 2012-08-03 14:10:59 +0530 | [diff] [blame] | 91 | @webnotes.whitelist(allow_guest=True) |
| 92 | def add_subscriber(): |
| 93 | """add blog subscriber to lead""" |
| 94 | full_name = webnotes.form_dict.get('your_name') |
| 95 | email = webnotes.form_dict.get('your_email_address') |
| 96 | name = webnotes.conn.sql("""select name from tabLead where email_id=%s""", email) |
| 97 | |
| 98 | from webnotes.model.doc import Document |
| 99 | if name: |
| 100 | lead = Document('Lead', name[0][0]) |
| 101 | else: |
| 102 | lead = Document('Lead') |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 103 | |
| 104 | if not lead.source: lead.source = 'Blog' |
Rushabh Mehta | 4b3b25e | 2012-08-03 14:10:59 +0530 | [diff] [blame] | 105 | lead.unsubscribed = 0 |
| 106 | lead.blog_subscriber = 1 |
| 107 | lead.lead_name = full_name |
| 108 | lead.email_id = email |
| 109 | lead.save() |
| 110 | |
Anand Doshi | 200c443 | 2012-07-13 01:14:52 +0530 | [diff] [blame] | 111 | def get_blog_content(blog_page_name): |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 112 | import website.utils |
| 113 | content = website.utils.get_html(blog_page_name) |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 114 | content = split_blog_content(content) |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 115 | import webnotes.utils |
| 116 | content = webnotes.utils.escape_html(content) |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 117 | return content |
| 118 | |
| 119 | def split_blog_content(content): |
| 120 | content = content.split("<!-- begin blog content -->") |
| 121 | content = len(content) > 1 and content[1] or content[0] |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 122 | content = content.split("<!-- end blog content -->") |
| 123 | content = content[0] |
Anand Doshi | f01059f | 2012-07-13 00:46:59 +0530 | [diff] [blame] | 124 | return content |