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