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