blob: 02230dcd3486ac384be1abd11692002b1333e3c6 [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
Anand Doshibaf4f642012-12-07 19:53:18 +05306import website.utils
Anand Doshi8c7e76b2012-07-11 18:40:57 +05307
8@webnotes.whitelist(allow_guest=True)
Anand Doshif01059f2012-07-13 00:46:59 +05309def get_blog_list(args=None):
10 """
11 args = {
12 'limit_start': 0,
13 'limit_page_length': 10,
14 }
15 """
16 import webnotes
17
18 if not args: args = webnotes.form_dict
19
20 query = """\
21 select
Anand Doshif4378992012-12-07 19:59:25 +053022 name, page_name, content, owner, creation as creation,
Rushabh Mehta87192da2012-12-06 16:45:00 +053023 title, (select count(name) from `tabComment` where
Anand Doshi67327a32012-12-06 20:13:57 +053024 comment_doctype='Blog' and comment_docname=`tabBlog`.name) as comments
Rushabh Mehta87192da2012-12-06 16:45:00 +053025 from `tabBlog`
26 where ifnull(published,0)=1
Anand Doshi711f9862012-12-06 19:49:00 +053027 order by creation desc, name asc"""
Anand Doshif01059f2012-07-13 00:46:59 +053028
29 from webnotes.widgets.query_builder import add_limit_to_query
30 query, args = add_limit_to_query(query, args)
31
32 result = webnotes.conn.sql(query, args, as_dict=1)
33
34 # strip html tags from content
35 import webnotes.utils
Anand Doshif01059f2012-07-13 00:46:59 +053036
37 for res in result:
38 from webnotes.utils import global_date_format, get_fullname
39 res['full_name'] = get_fullname(res['owner'])
Rushabh Mehta87192da2012-12-06 16:45:00 +053040 res['published'] = global_date_format(res['creation'])
Anand Doshib3a4c092012-07-27 14:39:27 +053041 if not res['content']:
Anand Doshif4378992012-12-07 19:59:25 +053042 res['content'] = website.utils.get_html(res['page_name'])
Anand Doshif01059f2012-07-13 00:46:59 +053043 res['content'] = split_blog_content(res['content'])
44 res['content'] = res['content'][:1000]
45
46 return result
47
48@webnotes.whitelist(allow_guest=True)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053049def get_recent_blog_list(args=None):
50 """
51 args = {
52 'limit_start': 0,
53 'limit_page_length': 5,
54 'name': '',
55 }
56 """
57 import webnotes
58
59 if not args: args = webnotes.form_dict
60
61 query = """\
Anand Doshic7d5fc42012-07-31 19:15:01 +053062 select name, page_name, title, left(content, 100) as content
Anand Doshi8c7e76b2012-07-11 18:40:57 +053063 from tabBlog
64 where ifnull(published,0)=1 and
65 name!=%(name)s order by creation desc"""
66
67 from webnotes.widgets.query_builder import add_limit_to_query
68 query, args = add_limit_to_query(query, args)
69
70 result = webnotes.conn.sql(query, args, as_dict=1)
71
72 # strip html tags from content
73 import webnotes.utils
74 for res in result:
75 res['content'] = webnotes.utils.strip_html(res['content'])
76
77 return result
78
79@webnotes.whitelist(allow_guest=True)
80def add_comment(args=None):
81 """
82 args = {
83 'comment': '',
84 'comment_by': '',
85 'comment_by_fullname': '',
86 'comment_doctype': '',
87 'comment_docname': '',
88 'page_name': '',
89 }
90 """
91 import webnotes
Rushabh Mehtafa0e2522012-08-13 11:09:21 +053092 import webnotes.utils, markdown2
93 import webnotes.widgets.form.comments
Anand Doshi8c7e76b2012-07-11 18:40:57 +053094
95 if not args: args = webnotes.form_dict
Anand Doshi25dd92d2012-08-17 23:36:11 +053096 args['comment'] = unicode(markdown2.markdown(args.get('comment') or ''))
Anand Doshi8c7e76b2012-07-11 18:40:57 +053097
Anand Doshi8c7e76b2012-07-11 18:40:57 +053098 comment = webnotes.widgets.form.comments.add_comment(args)
99
100 # since comments are embedded in the page, clear the web cache
Rushabh Mehta571377a2012-12-07 11:00:26 +0530101 website.utils.clear_cache(args.get('page_name'))
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530102
Anand Doshi7e657d72012-08-23 17:15:13 +0530103 comment['comment_date'] = webnotes.utils.global_date_format(comment['creation'])
Anand Doshi3d53e862012-07-12 20:12:40 +0530104 template_args = { 'comment_list': [comment], 'template': 'html/comment.html' }
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530105
106 # get html of comment row
Rushabh Mehta571377a2012-12-07 11:00:26 +0530107 comment_html = website.utils.build_html(template_args)
Rushabh Mehtaaaa75492012-08-06 15:22:17 +0530108
109 # notify commentors
110 commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where
111 comment_doctype='Blog' and comment_docname=%s and
112 ifnull(unsubscribed, 0)=0""", args.get('comment_docname'))]
113
114 blog = webnotes.conn.sql("""select * from tabBlog where name=%s""",
115 args.get('comment_docname'), as_dict=1)[0]
116
117 from webnotes.utils.email_lib.bulk import send
Rushabh Mehtac3de42c2012-08-13 11:31:27 +0530118 send(recipients=list(set(commentors + [blog['owner']])),
Rushabh Mehtaaaa75492012-08-06 15:22:17 +0530119 doctype='Comment',
120 email_field='comment_by',
Rushabh Mehtaaaa75492012-08-06 15:22:17 +0530121 subject='New Comment on Blog: ' + blog['title'],
Rushabh Mehtafa0e2522012-08-13 11:09:21 +0530122 message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args)
Rushabh Mehtaaaa75492012-08-06 15:22:17 +0530123
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530124 return comment_html
Anand Doshif01059f2012-07-13 00:46:59 +0530125
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +0530126@webnotes.whitelist(allow_guest=True)
127def add_subscriber():
128 """add blog subscriber to lead"""
129 full_name = webnotes.form_dict.get('your_name')
130 email = webnotes.form_dict.get('your_email_address')
131 name = webnotes.conn.sql("""select name from tabLead where email_id=%s""", email)
132
133 from webnotes.model.doc import Document
134 if name:
135 lead = Document('Lead', name[0][0])
136 else:
137 lead = Document('Lead')
Rushabh Mehtaaaa75492012-08-06 15:22:17 +0530138
139 if not lead.source: lead.source = 'Blog'
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +0530140 lead.unsubscribed = 0
141 lead.blog_subscriber = 1
142 lead.lead_name = full_name
143 lead.email_id = email
144 lead.save()
145
Anand Doshi200c4432012-07-13 01:14:52 +0530146def get_blog_content(blog_page_name):
Rushabh Mehta571377a2012-12-07 11:00:26 +0530147 import website.utils
148 content = website.utils.get_html(blog_page_name)
Anand Doshif01059f2012-07-13 00:46:59 +0530149 content = split_blog_content(content)
Anand Doshif01059f2012-07-13 00:46:59 +0530150 import webnotes.utils
151 content = webnotes.utils.escape_html(content)
Anand Doshif01059f2012-07-13 00:46:59 +0530152 return content
153
154def split_blog_content(content):
155 content = content.split("<!-- begin blog content -->")
156 content = len(content) > 1 and content[1] or content[0]
Anand Doshif01059f2012-07-13 00:46:59 +0530157 content = content.split("<!-- end blog content -->")
158 content = content[0]
Anand Doshif01059f2012-07-13 00:46:59 +0530159 return content