blob: dcb15cbd17739a340c2827c3d6a3dcbf58f3a687 [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)
Rushabh Mehta676a5682013-03-07 18:51:10 +05309def get_blog_list(start=0, by=None):
Anand Doshif01059f2012-07-13 00:46:59 +053010 import webnotes
Rushabh Mehta676a5682013-03-07 18:51:10 +053011 condition = ""
12 if by:
13 condition = " and t1.blogger='%s'" % by.replace("'", "\'")
Anand Doshif01059f2012-07-13 00:46:59 +053014 query = """\
15 select
Rushabh Mehta676a5682013-03-07 18:51:10 +053016 t1.title, t1.name, t1.page_name, t1.creation as creation,
17 ifnull(t1.blog_intro, t1.content) as content,
18 t2.full_name, t2.avatar, t1.blogger,
19 (select count(name) from `tabComment` where
20 comment_doctype='Blog' and comment_docname=t1.name) as comments
21 from `tabBlog` t1, `tabBlogger` t2
22 where ifnull(t1.published,0)=1
23 and t1.blogger = t2.name
24 %(condition)s
Rushabh Mehtafd6ad192012-12-17 12:52:43 +053025 order by creation desc, name asc
Rushabh Mehta676a5682013-03-07 18:51:10 +053026 limit %(start)s, 5""" % {"start": start, "condition": condition}
Rushabh Mehtafd6ad192012-12-17 12:52:43 +053027
Rushabh Mehta676a5682013-03-07 18:51:10 +053028 result = webnotes.conn.sql(query, as_dict=1)
Anand Doshif01059f2012-07-13 00:46:59 +053029
30 # strip html tags from content
31 import webnotes.utils
Anand Doshif01059f2012-07-13 00:46:59 +053032
33 for res in result:
34 from webnotes.utils import global_date_format, get_fullname
Rushabh Mehta87192da2012-12-06 16:45:00 +053035 res['published'] = global_date_format(res['creation'])
Anand Doshib3a4c092012-07-27 14:39:27 +053036 if not res['content']:
Anand Doshif4378992012-12-07 19:59:25 +053037 res['content'] = website.utils.get_html(res['page_name'])
Rushabh Mehta676a5682013-03-07 18:51:10 +053038 res['content'] = res['content'][:140]
39 if res.avatar and not "/" in res.avatar:
40 res.avatar = "files/" + res.avatar
41
Anand Doshi8c7e76b2012-07-11 18:40:57 +053042 return result
43
44@webnotes.whitelist(allow_guest=True)
45def 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 Mehtafa0e2522012-08-13 11:09:21 +053057 import webnotes.utils, markdown2
58 import webnotes.widgets.form.comments
Anand Doshi8c7e76b2012-07-11 18:40:57 +053059
60 if not args: args = webnotes.form_dict
Anand Doshi25dd92d2012-08-17 23:36:11 +053061 args['comment'] = unicode(markdown2.markdown(args.get('comment') or ''))
Anand Doshi8c7e76b2012-07-11 18:40:57 +053062
Anand Doshi8c7e76b2012-07-11 18:40:57 +053063 comment = webnotes.widgets.form.comments.add_comment(args)
64
65 # since comments are embedded in the page, clear the web cache
Rushabh Mehta571377a2012-12-07 11:00:26 +053066 website.utils.clear_cache(args.get('page_name'))
Anand Doshi8c7e76b2012-07-11 18:40:57 +053067
Anand Doshi7e657d72012-08-23 17:15:13 +053068 comment['comment_date'] = webnotes.utils.global_date_format(comment['creation'])
Anand Doshi3d53e862012-07-12 20:12:40 +053069 template_args = { 'comment_list': [comment], 'template': 'html/comment.html' }
Anand Doshi8c7e76b2012-07-11 18:40:57 +053070
71 # get html of comment row
Rushabh Mehta571377a2012-12-07 11:00:26 +053072 comment_html = website.utils.build_html(template_args)
Rushabh Mehtaaaa75492012-08-06 15:22:17 +053073
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 Mehtac3de42c2012-08-13 11:31:27 +053083 send(recipients=list(set(commentors + [blog['owner']])),
Rushabh Mehtaaaa75492012-08-06 15:22:17 +053084 doctype='Comment',
85 email_field='comment_by',
Rushabh Mehtaaaa75492012-08-06 15:22:17 +053086 subject='New Comment on Blog: ' + blog['title'],
Rushabh Mehtafa0e2522012-08-13 11:09:21 +053087 message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args)
Rushabh Mehtaaaa75492012-08-06 15:22:17 +053088
Anand Doshi8c7e76b2012-07-11 18:40:57 +053089 return comment_html
Anand Doshif01059f2012-07-13 00:46:59 +053090
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +053091@webnotes.whitelist(allow_guest=True)
92def 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 Mehtaaaa75492012-08-06 15:22:17 +0530103
104 if not lead.source: lead.source = 'Blog'
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +0530105 lead.unsubscribed = 0
106 lead.blog_subscriber = 1
107 lead.lead_name = full_name
108 lead.email_id = email
109 lead.save()
110
Anand Doshi200c4432012-07-13 01:14:52 +0530111def get_blog_content(blog_page_name):
Rushabh Mehta571377a2012-12-07 11:00:26 +0530112 import website.utils
113 content = website.utils.get_html(blog_page_name)
Anand Doshif01059f2012-07-13 00:46:59 +0530114 content = split_blog_content(content)
Anand Doshif01059f2012-07-13 00:46:59 +0530115 import webnotes.utils
116 content = webnotes.utils.escape_html(content)
Anand Doshif01059f2012-07-13 00:46:59 +0530117 return content
Rushabh Mehta676a5682013-03-07 18:51:10 +0530118