blob: 1ebea29d7ced50886ba7784170a6a6427d0aa3ee [file] [log] [blame]
Anand Doshi486f9df2012-07-19 13:40:31 +05301from __future__ import unicode_literals
Anand Doshi8c7e76b2012-07-11 18:40:57 +05302import webnotes
3
4@webnotes.whitelist(allow_guest=True)
Anand Doshif01059f2012-07-13 00:46:59 +05305def get_blog_list(args=None):
6 """
7 args = {
8 'limit_start': 0,
9 'limit_page_length': 10,
10 }
11 """
12 import webnotes
13
14 if not args: args = webnotes.form_dict
15
16 query = """\
17 select
18 cache.name as name, cache.html as content,
19 blog.owner as owner, blog.creation as published,
20 blog.title as title
21 from `tabWeb Cache` cache, `tabBlog` blog
22 where cache.doc_type = 'Blog' and blog.page_name = cache.name
23 order by published desc, name asc"""
24
25 from webnotes.widgets.query_builder import add_limit_to_query
26 query, args = add_limit_to_query(query, args)
27
28 result = webnotes.conn.sql(query, args, as_dict=1)
29
30 # strip html tags from content
31 import webnotes.utils
32 import website.web_cache
33
34 for res in result:
35 from webnotes.utils import global_date_format, get_fullname
36 res['full_name'] = get_fullname(res['owner'])
37 res['published'] = global_date_format(res['published'])
Anand Doshib3a4c092012-07-27 14:39:27 +053038 if not res['content']:
39 res['content'] = website.web_cache.get_html(res['name'])
Anand Doshif01059f2012-07-13 00:46:59 +053040 res['content'] = split_blog_content(res['content'])
41 res['content'] = res['content'][:1000]
42
43 return result
44
45@webnotes.whitelist(allow_guest=True)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053046def get_recent_blog_list(args=None):
47 """
48 args = {
49 'limit_start': 0,
50 'limit_page_length': 5,
51 'name': '',
52 }
53 """
54 import webnotes
55
56 if not args: args = webnotes.form_dict
57
58 query = """\
Anand Doshic7d5fc42012-07-31 19:15:01 +053059 select name, page_name, title, left(content, 100) as content
Anand Doshi8c7e76b2012-07-11 18:40:57 +053060 from tabBlog
61 where ifnull(published,0)=1 and
62 name!=%(name)s order by creation desc"""
63
64 from webnotes.widgets.query_builder import add_limit_to_query
65 query, args = add_limit_to_query(query, args)
66
67 result = webnotes.conn.sql(query, args, as_dict=1)
68
69 # strip html tags from content
70 import webnotes.utils
71 for res in result:
72 res['content'] = webnotes.utils.strip_html(res['content'])
73
74 return result
75
76@webnotes.whitelist(allow_guest=True)
77def add_comment(args=None):
78 """
79 args = {
80 'comment': '',
81 'comment_by': '',
82 'comment_by_fullname': '',
83 'comment_doctype': '',
84 'comment_docname': '',
85 'page_name': '',
86 }
87 """
88 import webnotes
89
90 if not args: args = webnotes.form_dict
91
92 import webnotes.widgets.form.comments
93 comment = webnotes.widgets.form.comments.add_comment(args)
94
95 # since comments are embedded in the page, clear the web cache
96 import website.web_cache
Anand Doshi51146c02012-07-12 18:41:12 +053097 website.web_cache.clear_cache(args.get('page_name'),
98 args.get('comment_doctype'), args.get('comment_docname'))
Anand Doshi8c7e76b2012-07-11 18:40:57 +053099
100 import webnotes.utils
101
102 comment['comment_date'] = webnotes.utils.pretty_date(comment['creation'])
Anand Doshi3d53e862012-07-12 20:12:40 +0530103 template_args = { 'comment_list': [comment], 'template': 'html/comment.html' }
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530104
105 # get html of comment row
Anand Doshi3d53e862012-07-12 20:12:40 +0530106 comment_html = website.web_cache.build_html(template_args)
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530107
108 return comment_html
Anand Doshif01059f2012-07-13 00:46:59 +0530109
Rushabh Mehta4b3b25e2012-08-03 14:10:59 +0530110@webnotes.whitelist(allow_guest=True)
111def add_subscriber():
112 """add blog subscriber to lead"""
113 full_name = webnotes.form_dict.get('your_name')
114 email = webnotes.form_dict.get('your_email_address')
115 name = webnotes.conn.sql("""select name from tabLead where email_id=%s""", email)
116
117 from webnotes.model.doc import Document
118 if name:
119 lead = Document('Lead', name[0][0])
120 else:
121 lead = Document('Lead')
122
123 lead.unsubscribed = 0
124 lead.blog_subscriber = 1
125 lead.lead_name = full_name
126 lead.email_id = email
127 lead.save()
128
Anand Doshi200c4432012-07-13 01:14:52 +0530129def get_blog_content(blog_page_name):
Anand Doshif01059f2012-07-13 00:46:59 +0530130 import website.web_cache
131 content = website.web_cache.get_html(blog_page_name)
132
133 content = split_blog_content(content)
134
135 import webnotes.utils
136 content = webnotes.utils.escape_html(content)
137
138 return content
139
140def split_blog_content(content):
141 content = content.split("<!-- begin blog content -->")
142 content = len(content) > 1 and content[1] or content[0]
143
144 content = content.split("<!-- end blog content -->")
145 content = content[0]
146
147 return content