blob: 250406718d3b3db4a048c4ba85765b3d11fcc57a [file] [log] [blame]
Rushabh Mehtaa494b882012-12-07 12:44:45 +05301# Copyright (c) 2012 Web Notes Technologies Pvt Ltd.
2# License: GNU General Public License (v3). For more information see license.txt
3
Anand Doshi486f9df2012-07-19 13:40:31 +05304from __future__ import unicode_literals
Anand Doshi8c7e76b2012-07-11 18:40:57 +05305import webnotes
Rushabh Mehta9db1a682013-04-02 10:41:37 +05306import webnotes.webutils
Rushabh Mehta1dd33012013-03-08 11:00:18 +05307from webnotes import _
Anand Doshi8c7e76b2012-07-11 18:40:57 +05308
Rushabh Mehtab8ec3e72013-03-11 14:29:09 +05309def clear_blog_cache():
10 for blog in webnotes.conn.sql_list("""select page_name from
11 `tabBlog Post` where ifnull(published,0)=1"""):
Rushabh Mehta9db1a682013-04-02 10:41:37 +053012 webnotes.webutils.delete_page_cache(blog)
Rushabh Mehtab8ec3e72013-03-11 14:29:09 +053013
Rushabh Mehta9db1a682013-04-02 10:41:37 +053014 webnotes.webutils.delete_page_cache("writers")
Rushabh Mehtab8ec3e72013-03-11 14:29:09 +053015
Anand Doshi8c7e76b2012-07-11 18:40:57 +053016@webnotes.whitelist(allow_guest=True)
Rushabh Mehtaa2deb682013-03-08 10:44:25 +053017def get_blog_list(start=0, by=None, category=None):
Anand Doshif01059f2012-07-13 00:46:59 +053018 import webnotes
Rushabh Mehta676a5682013-03-07 18:51:10 +053019 condition = ""
20 if by:
21 condition = " and t1.blogger='%s'" % by.replace("'", "\'")
Rushabh Mehtaa2deb682013-03-08 10:44:25 +053022 if category:
23 condition += " and t1.blog_category='%s'" % category.replace("'", "\'")
Anand Doshif01059f2012-07-13 00:46:59 +053024 query = """\
25 select
Rushabh Mehtab8ec3e72013-03-11 14:29:09 +053026 t1.title, t1.name, t1.page_name, t1.published_on as creation,
Rushabh Mehta676a5682013-03-07 18:51:10 +053027 ifnull(t1.blog_intro, t1.content) as content,
28 t2.full_name, t2.avatar, t1.blogger,
29 (select count(name) from `tabComment` where
Rushabh Mehtab8ec3e72013-03-11 14:29:09 +053030 comment_doctype='Blog Post' and comment_docname=t1.name) as comments
31 from `tabBlog Post` t1, `tabBlogger` t2
Rushabh Mehta676a5682013-03-07 18:51:10 +053032 where ifnull(t1.published,0)=1
33 and t1.blogger = t2.name
34 %(condition)s
Rushabh Mehtab8ec3e72013-03-11 14:29:09 +053035 order by published_on desc, name asc
36 limit %(start)s, 20""" % {"start": start, "condition": condition}
Rushabh Mehtafd6ad192012-12-17 12:52:43 +053037
Rushabh Mehta676a5682013-03-07 18:51:10 +053038 result = webnotes.conn.sql(query, as_dict=1)
Anand Doshif01059f2012-07-13 00:46:59 +053039
40 # strip html tags from content
41 import webnotes.utils
Anand Doshif01059f2012-07-13 00:46:59 +053042
43 for res in result:
44 from webnotes.utils import global_date_format, get_fullname
Rushabh Mehta87192da2012-12-06 16:45:00 +053045 res['published'] = global_date_format(res['creation'])
Anand Doshib3a4c092012-07-27 14:39:27 +053046 if not res['content']:
Rushabh Mehta9db1a682013-04-02 10:41:37 +053047 res['content'] = webnotes.webutils.get_html(res['page_name'])
Rushabh Mehta676a5682013-03-07 18:51:10 +053048 res['content'] = res['content'][:140]
Rushabh Mehta676a5682013-03-07 18:51:10 +053049
Anand Doshi8c7e76b2012-07-11 18:40:57 +053050 return result
51
52@webnotes.whitelist(allow_guest=True)
53def 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 Mehtafa0e2522012-08-13 11:09:21 +053065 import webnotes.utils, markdown2
Anand Doshi8c7e76b2012-07-11 18:40:57 +053066
67 if not args: args = webnotes.form_dict
Anand Doshi25dd92d2012-08-17 23:36:11 +053068 args['comment'] = unicode(markdown2.markdown(args.get('comment') or ''))
Rushabh Mehta3ddc5242013-04-30 16:50:48 +053069 args['doctype'] = "Comment"
Anand Doshi8c7e76b2012-07-11 18:40:57 +053070
Rushabh Mehta3ddc5242013-04-30 16:50:48 +053071 page_name = args.get("page_name")
72 if "page_name" in args:
73 del args["page_name"]
Rushabh Mehtab8402962013-06-09 13:23:50 +053074 if "cmd" in args:
75 del args["cmd"]
Rushabh Mehta3ddc5242013-04-30 16:50:48 +053076
77 comment = webnotes.bean(args)
Rushabh Mehta2e51f8a2013-06-09 13:17:29 +053078 comment.ignore_permissions = True
Rushabh Mehta3ddc5242013-04-30 16:50:48 +053079 comment.insert()
Anand Doshi8c7e76b2012-07-11 18:40:57 +053080
81 # since comments are embedded in the page, clear the web cache
Rushabh Mehta3ddc5242013-04-30 16:50:48 +053082 webnotes.webutils.clear_cache(page_name)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053083
Rushabh Mehtaf0ac1162013-06-09 13:27:12 +053084 args['comment_date'] = webnotes.utils.global_date_format(comment.doc.creation)
Rushabh Mehtab8402962013-06-09 13:23:50 +053085 template_args = { 'comment_list': [args], 'template': 'app/website/templates/html/comment.html' }
Anand Doshi8c7e76b2012-07-11 18:40:57 +053086
87 # get html of comment row
Rushabh Mehta9db1a682013-04-02 10:41:37 +053088 comment_html = webnotes.webutils.build_html(template_args)
Rushabh Mehtaaaa75492012-08-06 15:22:17 +053089
90 # notify commentors
91 commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where
Anand Doshi2e6806e2013-03-12 20:04:30 +053092 comment_doctype='Blog Post' and comment_docname=%s and
Rushabh Mehtaaaa75492012-08-06 15:22:17 +053093 ifnull(unsubscribed, 0)=0""", args.get('comment_docname'))]
94
Rushabh Mehtab8ec3e72013-03-11 14:29:09 +053095 blog = webnotes.conn.sql("""select * from `tabBlog Post` where name=%s""",
Rushabh Mehtaaaa75492012-08-06 15:22:17 +053096 args.get('comment_docname'), as_dict=1)[0]
97
98 from webnotes.utils.email_lib.bulk import send
Rushabh Mehtac3de42c2012-08-13 11:31:27 +053099 send(recipients=list(set(commentors + [blog['owner']])),
Rushabh Mehtaaaa75492012-08-06 15:22:17 +0530100 doctype='Comment',
101 email_field='comment_by',
Rushabh Mehtaaaa75492012-08-06 15:22:17 +0530102 subject='New Comment on Blog: ' + blog['title'],
Rushabh Mehtafa0e2522012-08-13 11:09:21 +0530103 message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args)
Rushabh Mehtaaaa75492012-08-06 15:22:17 +0530104
Rushabh Mehtad3d34722013-06-09 13:32:21 +0530105 return comment_html.replace("\n", "")
Anand Doshif01059f2012-07-13 00:46:59 +0530106
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +0530107@webnotes.whitelist(allow_guest=True)
Rushabh Mehta770e7932013-03-11 18:30:05 +0530108def add_subscriber(name, email_id):
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +0530109 """add blog subscriber to lead"""
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +0530110 name = webnotes.conn.sql("""select name from tabLead where email_id=%s""", email)
111
112 from webnotes.model.doc import Document
113 if name:
114 lead = Document('Lead', name[0][0])
115 else:
116 lead = Document('Lead')
Rushabh Mehtaaaa75492012-08-06 15:22:17 +0530117
118 if not lead.source: lead.source = 'Blog'
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +0530119 lead.unsubscribed = 0
120 lead.blog_subscriber = 1
Rushabh Mehta770e7932013-03-11 18:30:05 +0530121 lead.lead_name = name
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +0530122 lead.email_id = email
123 lead.save()
Rushabh Mehta770e7932013-03-11 18:30:05 +0530124
Anand Doshi200c4432012-07-13 01:14:52 +0530125def get_blog_content(blog_page_name):
Rushabh Mehta9db1a682013-04-02 10:41:37 +0530126 import webnotes.webutils
127 content = webnotes.webutils.get_html(blog_page_name)
Anand Doshif01059f2012-07-13 00:46:59 +0530128 import webnotes.utils
129 content = webnotes.utils.escape_html(content)
Anand Doshif01059f2012-07-13 00:46:59 +0530130 return content
Rushabh Mehtaa2deb682013-03-08 10:44:25 +0530131
132def get_blog_template_args():
Rushabh Mehtab33693d2013-03-11 17:57:57 +0530133 args = {
Rushabh Mehtaa2deb682013-03-08 10:44:25 +0530134 "categories": webnotes.conn.sql_list("select name from `tabBlog Category` order by name")
Rushabh Mehta1dd33012013-03-08 11:00:18 +0530135 }
Rushabh Mehtab33693d2013-03-11 17:57:57 +0530136 args.update(webnotes.doc("Blog Settings", "Blog Settings").fields)
137 return args
Rushabh Mehta1dd33012013-03-08 11:00:18 +0530138
139def get_writers_args():
Rushabh Mehtab8ec3e72013-03-11 14:29:09 +0530140 bloggers = webnotes.conn.sql("""select * from `tabBlogger`
141 order by posts desc""", as_dict=1)
Rushabh Mehta1dd33012013-03-08 11:00:18 +0530142
Rushabh Mehtab33693d2013-03-11 17:57:57 +0530143 args = {
Rushabh Mehta1dd33012013-03-08 11:00:18 +0530144 "bloggers": bloggers,
145 "texts": {
146 "all_posts_by": _("All posts by")
147 },
148 "categories": webnotes.conn.sql_list("select name from `tabBlog Category` order by name")
Rushabh Mehtab33693d2013-03-11 17:57:57 +0530149 }
150
151 args.update(webnotes.doc("Blog Settings", "Blog Settings").fields)
152 return args