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