2012-12-07 07:14:45 +00:00
|
|
|
# Copyright (c) 2012 Web Notes Technologies Pvt Ltd.
|
|
|
|
# License: GNU General Public License (v3). For more information see license.txt
|
|
|
|
|
2012-07-19 08:10:31 +00:00
|
|
|
from __future__ import unicode_literals
|
2012-07-11 13:10:57 +00:00
|
|
|
import webnotes
|
2013-04-02 05:11:37 +00:00
|
|
|
import webnotes.webutils
|
2013-03-08 05:30:18 +00:00
|
|
|
from webnotes import _
|
2012-07-11 13:10:57 +00:00
|
|
|
|
2013-03-11 08:59:09 +00:00
|
|
|
def clear_blog_cache():
|
|
|
|
for blog in webnotes.conn.sql_list("""select page_name from
|
|
|
|
`tabBlog Post` where ifnull(published,0)=1"""):
|
2013-04-02 05:11:37 +00:00
|
|
|
webnotes.webutils.delete_page_cache(blog)
|
2013-03-11 08:59:09 +00:00
|
|
|
|
2013-04-02 05:11:37 +00:00
|
|
|
webnotes.webutils.delete_page_cache("writers")
|
2013-03-11 08:59:09 +00:00
|
|
|
|
2012-07-12 19:16:59 +00:00
|
|
|
@webnotes.whitelist(allow_guest=True)
|
2013-03-08 05:14:25 +00:00
|
|
|
def get_blog_list(start=0, by=None, category=None):
|
2012-07-12 19:16:59 +00:00
|
|
|
import webnotes
|
2013-03-07 13:21:10 +00:00
|
|
|
condition = ""
|
|
|
|
if by:
|
|
|
|
condition = " and t1.blogger='%s'" % by.replace("'", "\'")
|
2013-03-08 05:14:25 +00:00
|
|
|
if category:
|
|
|
|
condition += " and t1.blog_category='%s'" % category.replace("'", "\'")
|
2012-07-12 19:16:59 +00:00
|
|
|
query = """\
|
|
|
|
select
|
2013-03-11 08:59:09 +00:00
|
|
|
t1.title, t1.name, t1.page_name, t1.published_on as creation,
|
2013-03-07 13:21:10 +00:00
|
|
|
ifnull(t1.blog_intro, t1.content) as content,
|
|
|
|
t2.full_name, t2.avatar, t1.blogger,
|
|
|
|
(select count(name) from `tabComment` where
|
2013-03-11 08:59:09 +00:00
|
|
|
comment_doctype='Blog Post' and comment_docname=t1.name) as comments
|
|
|
|
from `tabBlog Post` t1, `tabBlogger` t2
|
2013-03-07 13:21:10 +00:00
|
|
|
where ifnull(t1.published,0)=1
|
|
|
|
and t1.blogger = t2.name
|
|
|
|
%(condition)s
|
2013-03-11 08:59:09 +00:00
|
|
|
order by published_on desc, name asc
|
|
|
|
limit %(start)s, 20""" % {"start": start, "condition": condition}
|
2012-12-17 07:22:43 +00:00
|
|
|
|
2013-03-07 13:21:10 +00:00
|
|
|
result = webnotes.conn.sql(query, as_dict=1)
|
2012-07-12 19:16:59 +00:00
|
|
|
|
|
|
|
# strip html tags from content
|
|
|
|
import webnotes.utils
|
|
|
|
|
|
|
|
for res in result:
|
|
|
|
from webnotes.utils import global_date_format, get_fullname
|
2012-12-06 11:15:00 +00:00
|
|
|
res['published'] = global_date_format(res['creation'])
|
2012-07-27 09:09:27 +00:00
|
|
|
if not res['content']:
|
2013-04-02 05:11:37 +00:00
|
|
|
res['content'] = webnotes.webutils.get_html(res['page_name'])
|
2013-03-07 13:21:10 +00:00
|
|
|
res['content'] = res['content'][:140]
|
|
|
|
|
2012-07-11 13:10:57 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
@webnotes.whitelist(allow_guest=True)
|
|
|
|
def add_comment(args=None):
|
|
|
|
"""
|
|
|
|
args = {
|
|
|
|
'comment': '',
|
|
|
|
'comment_by': '',
|
|
|
|
'comment_by_fullname': '',
|
|
|
|
'comment_doctype': '',
|
|
|
|
'comment_docname': '',
|
|
|
|
'page_name': '',
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
import webnotes
|
2012-08-13 05:39:21 +00:00
|
|
|
import webnotes.utils, markdown2
|
2012-07-11 13:10:57 +00:00
|
|
|
|
|
|
|
if not args: args = webnotes.form_dict
|
2012-08-17 18:06:11 +00:00
|
|
|
args['comment'] = unicode(markdown2.markdown(args.get('comment') or ''))
|
2013-04-30 11:20:48 +00:00
|
|
|
args['doctype'] = "Comment"
|
2012-07-11 13:10:57 +00:00
|
|
|
|
2013-04-30 11:20:48 +00:00
|
|
|
page_name = args.get("page_name")
|
|
|
|
if "page_name" in args:
|
|
|
|
del args["page_name"]
|
2013-06-09 07:53:50 +00:00
|
|
|
if "cmd" in args:
|
|
|
|
del args["cmd"]
|
2013-04-30 11:20:48 +00:00
|
|
|
|
|
|
|
comment = webnotes.bean(args)
|
2013-06-09 07:47:29 +00:00
|
|
|
comment.ignore_permissions = True
|
2013-04-30 11:20:48 +00:00
|
|
|
comment.insert()
|
2012-07-11 13:10:57 +00:00
|
|
|
|
|
|
|
# since comments are embedded in the page, clear the web cache
|
2013-04-30 11:20:48 +00:00
|
|
|
webnotes.webutils.clear_cache(page_name)
|
2012-07-11 13:10:57 +00:00
|
|
|
|
2013-06-09 07:57:12 +00:00
|
|
|
args['comment_date'] = webnotes.utils.global_date_format(comment.doc.creation)
|
2013-06-09 07:53:50 +00:00
|
|
|
template_args = { 'comment_list': [args], 'template': 'app/website/templates/html/comment.html' }
|
2012-07-11 13:10:57 +00:00
|
|
|
|
|
|
|
# get html of comment row
|
2013-04-02 05:11:37 +00:00
|
|
|
comment_html = webnotes.webutils.build_html(template_args)
|
2012-08-06 09:52:17 +00:00
|
|
|
|
|
|
|
# notify commentors
|
|
|
|
commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where
|
2013-03-12 14:34:30 +00:00
|
|
|
comment_doctype='Blog Post' and comment_docname=%s and
|
2012-08-06 09:52:17 +00:00
|
|
|
ifnull(unsubscribed, 0)=0""", args.get('comment_docname'))]
|
|
|
|
|
2013-03-11 08:59:09 +00:00
|
|
|
blog = webnotes.conn.sql("""select * from `tabBlog Post` where name=%s""",
|
2012-08-06 09:52:17 +00:00
|
|
|
args.get('comment_docname'), as_dict=1)[0]
|
|
|
|
|
|
|
|
from webnotes.utils.email_lib.bulk import send
|
2012-08-13 06:01:27 +00:00
|
|
|
send(recipients=list(set(commentors + [blog['owner']])),
|
2012-08-06 09:52:17 +00:00
|
|
|
doctype='Comment',
|
|
|
|
email_field='comment_by',
|
|
|
|
subject='New Comment on Blog: ' + blog['title'],
|
2012-08-13 05:39:21 +00:00
|
|
|
message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args)
|
2012-08-06 09:52:17 +00:00
|
|
|
|
2013-06-09 08:02:21 +00:00
|
|
|
return comment_html.replace("\n", "")
|
2012-07-12 19:16:59 +00:00
|
|
|
|
2012-08-03 08:40:59 +00:00
|
|
|
@webnotes.whitelist(allow_guest=True)
|
2013-03-11 13:00:05 +00:00
|
|
|
def add_subscriber(name, email_id):
|
2012-08-03 08:40:59 +00:00
|
|
|
"""add blog subscriber to lead"""
|
|
|
|
name = webnotes.conn.sql("""select name from tabLead where email_id=%s""", email)
|
|
|
|
|
|
|
|
from webnotes.model.doc import Document
|
|
|
|
if name:
|
|
|
|
lead = Document('Lead', name[0][0])
|
|
|
|
else:
|
|
|
|
lead = Document('Lead')
|
2012-08-06 09:52:17 +00:00
|
|
|
|
|
|
|
if not lead.source: lead.source = 'Blog'
|
2012-08-03 08:40:59 +00:00
|
|
|
lead.unsubscribed = 0
|
|
|
|
lead.blog_subscriber = 1
|
2013-03-11 13:00:05 +00:00
|
|
|
lead.lead_name = name
|
2012-08-03 08:40:59 +00:00
|
|
|
lead.email_id = email
|
|
|
|
lead.save()
|
2013-03-11 13:00:05 +00:00
|
|
|
|
2012-07-12 19:44:52 +00:00
|
|
|
def get_blog_content(blog_page_name):
|
2013-04-02 05:11:37 +00:00
|
|
|
import webnotes.webutils
|
|
|
|
content = webnotes.webutils.get_html(blog_page_name)
|
2012-07-12 19:16:59 +00:00
|
|
|
import webnotes.utils
|
|
|
|
content = webnotes.utils.escape_html(content)
|
|
|
|
return content
|
2013-03-08 05:14:25 +00:00
|
|
|
|
|
|
|
def get_blog_template_args():
|
2013-03-11 12:27:57 +00:00
|
|
|
args = {
|
2013-03-08 05:14:25 +00:00
|
|
|
"categories": webnotes.conn.sql_list("select name from `tabBlog Category` order by name")
|
2013-03-08 05:30:18 +00:00
|
|
|
}
|
2013-03-11 12:27:57 +00:00
|
|
|
args.update(webnotes.doc("Blog Settings", "Blog Settings").fields)
|
|
|
|
return args
|
2013-03-08 05:30:18 +00:00
|
|
|
|
|
|
|
def get_writers_args():
|
2013-03-11 08:59:09 +00:00
|
|
|
bloggers = webnotes.conn.sql("""select * from `tabBlogger`
|
|
|
|
order by posts desc""", as_dict=1)
|
2013-03-08 05:30:18 +00:00
|
|
|
|
2013-03-11 12:27:57 +00:00
|
|
|
args = {
|
2013-03-08 05:30:18 +00:00
|
|
|
"bloggers": bloggers,
|
|
|
|
"texts": {
|
|
|
|
"all_posts_by": _("All posts by")
|
|
|
|
},
|
|
|
|
"categories": webnotes.conn.sql_list("select name from `tabBlog Category` order by name")
|
2013-03-11 12:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args.update(webnotes.doc("Blog Settings", "Blog Settings").fields)
|
|
|
|
return args
|