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