blob: 344277d6446a1e2c361bcbae72e2293878396ffb [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'])
38 res['content'] = split_blog_content(res['content'])
39 res['content'] = res['content'][:1000]
40
41 return result
42
43@webnotes.whitelist(allow_guest=True)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053044def get_recent_blog_list(args=None):
45 """
46 args = {
47 'limit_start': 0,
48 'limit_page_length': 5,
49 'name': '',
50 }
51 """
52 import webnotes
53
54 if not args: args = webnotes.form_dict
55
56 query = """\
57 select name, title, left(content, 100) as content
58 from tabBlog
59 where ifnull(published,0)=1 and
60 name!=%(name)s order by creation desc"""
61
62 from webnotes.widgets.query_builder import add_limit_to_query
63 query, args = add_limit_to_query(query, args)
64
65 result = webnotes.conn.sql(query, args, as_dict=1)
66
67 # strip html tags from content
68 import webnotes.utils
69 for res in result:
70 res['content'] = webnotes.utils.strip_html(res['content'])
71
72 return result
73
74@webnotes.whitelist(allow_guest=True)
75def add_comment(args=None):
76 """
77 args = {
78 'comment': '',
79 'comment_by': '',
80 'comment_by_fullname': '',
81 'comment_doctype': '',
82 'comment_docname': '',
83 'page_name': '',
84 }
85 """
86 import webnotes
87
88 if not args: args = webnotes.form_dict
89
90 import webnotes.widgets.form.comments
91 comment = webnotes.widgets.form.comments.add_comment(args)
92
93 # since comments are embedded in the page, clear the web cache
94 import website.web_cache
Anand Doshi51146c02012-07-12 18:41:12 +053095 website.web_cache.clear_cache(args.get('page_name'),
96 args.get('comment_doctype'), args.get('comment_docname'))
Anand Doshi8c7e76b2012-07-11 18:40:57 +053097
Anand Doshi4cf55c52012-07-13 02:16:22 +053098 # loads fresh blog into cache
99 get_blog_content(args.get('page_name'))
100
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530101 import webnotes.utils
102
103 comment['comment_date'] = webnotes.utils.pretty_date(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
Anand Doshi3d53e862012-07-12 20:12:40 +0530107 comment_html = website.web_cache.build_html(template_args)
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530108
109 return comment_html
Anand Doshif01059f2012-07-13 00:46:59 +0530110
Anand Doshi200c4432012-07-13 01:14:52 +0530111def get_blog_content(blog_page_name):
Anand Doshif01059f2012-07-13 00:46:59 +0530112 import website.web_cache
113 content = website.web_cache.get_html(blog_page_name)
114
115 content = split_blog_content(content)
116
117 import webnotes.utils
118 content = webnotes.utils.escape_html(content)
119
120 return content
121
122def split_blog_content(content):
123 content = content.split("<!-- begin blog content -->")
124 content = len(content) > 1 and content[1] or content[0]
125
126 content = content.split("<!-- end blog content -->")
127 content = content[0]
128
129 return content