1 # -*- coding: utf-8 -*-
6 from openerp import tools
7 from openerp.addons.web import http
8 from openerp.addons.web.http import request
9 from openerp.addons.website.models.website import slug
10 from openerp.osv.orm import browse_record
11 from openerp.tools.translate import _
12 from openerp import SUPERUSER_ID
13 from openerp.tools import html2plaintext
16 class QueryURL(object):
17 def __init__(self, path='', path_args=None, **args):
20 self.path_args = set(path_args or [])
22 def __call__(self, path=None, path_args=None, **kw):
23 path = path or self.path
24 for k, v in self.args.items():
26 path_args = set(path_args or []).union(self.path_args)
27 paths, fragments = [], []
28 for key, value in kw.items():
29 if value and key in path_args:
30 if isinstance(value, browse_record):
31 paths.append((key, slug(value)))
33 paths.append((key, value))
35 if isinstance(value, list) or isinstance(value, set):
36 fragments.append(werkzeug.url_encode([(key, item) for item in value]))
38 fragments.append(werkzeug.url_encode([(key, value)]))
39 for key, value in paths:
40 path += '/' + key + '/%s' % value
42 path += '?' + '&'.join(fragments)
46 class WebsiteBlog(http.Controller):
47 _blog_post_per_page = 20
48 _post_comment_per_page = 10
51 blog_post_obj = request.registry['blog.post']
52 groups = blog_post_obj.read_group(
53 request.cr, request.uid, [], ['name', 'create_date'],
54 groupby="create_date", orderby="create_date desc", context=request.context)
56 begin_date = datetime.datetime.strptime(group['__domain'][0][2], tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
57 end_date = datetime.datetime.strptime(group['__domain'][1][2], tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
58 group['date_begin'] = '%s' % datetime.date.strftime(begin_date, tools.DEFAULT_SERVER_DATE_FORMAT)
59 group['date_end'] = '%s' % datetime.date.strftime(end_date, tools.DEFAULT_SERVER_DATE_FORMAT)
64 '/blog/page/<int:page>',
65 ], type='http', auth="public", website=True)
66 def blogs(self, page=1, **post):
67 cr, uid, context = request.cr, request.uid, request.context
68 blog_obj = request.registry['blog.post']
69 total = blog_obj.search(cr, uid, [], count=True, context=context)
70 pager = request.website.pager(
74 step=self._blog_post_per_page,
76 post_ids = blog_obj.search(cr, uid, [], offset=(page-1)*self._blog_post_per_page, limit=self._blog_post_per_page, context=context)
77 posts = blog_obj.browse(cr, uid, post_ids, context=context)
78 blog_url = QueryURL('', ['blog', 'tag'])
79 return request.website.render("website_blog.latest_blogs", {
86 '/blog/<model("blog.blog"):blog>',
87 '/blog/<model("blog.blog"):blog>/page/<int:page>',
88 '/blog/<model("blog.blog"):blog>/tag/<model("blog.tag"):tag>',
89 '/blog/<model("blog.blog"):blog>/tag/<model("blog.tag"):tag>/page/<int:page>',
90 ], type='http', auth="public", website=True)
91 def blog(self, blog=None, tag=None, page=1, **opt):
92 """ Prepare all values to display the blog.
94 :return dict values: values for the templates, containing
96 - 'blog': current blog
97 - 'blogs': all blogs for navigation
98 - 'pager': pager of posts
100 - 'tags': all tags, for navigation
101 - 'nav_list': a dict [year][month] for archives navigation
102 - 'date': date_begin optional parameter, used in archives navigation
103 - 'blog_url': help object to create URLs
105 date_begin, date_end = opt.get('date_begin'), opt.get('date_end')
107 cr, uid, context = request.cr, request.uid, request.context
108 blog_post_obj = request.registry['blog.post']
110 blog_obj = request.registry['blog.blog']
111 blog_ids = blog_obj.search(cr, uid, [], order="create_date asc", context=context)
112 blogs = blog_obj.browse(cr, uid, blog_ids, context=context)
116 domain += [('blog_id', '=', blog.id)]
118 domain += [('tag_ids', 'in', tag.id)]
119 if date_begin and date_end:
120 domain += [("create_date", ">=", date_begin), ("create_date", "<=", date_end)]
122 blog_url = QueryURL('', ['blog', 'tag'], blog=blog, tag=tag, date_begin=date_begin, date_end=date_end)
123 post_url = QueryURL('', ['blogpost'], tag_id=tag and tag.id or None, date_begin=date_begin, date_end=date_end)
125 blog_post_ids = blog_post_obj.search(cr, uid, domain, order="create_date desc", context=context)
126 blog_posts = blog_post_obj.browse(cr, uid, blog_post_ids, context=context)
128 pager = request.website.pager(
130 total=len(blog_posts),
132 step=self._blog_post_per_page,
134 pager_begin = (page - 1) * self._blog_post_per_page
135 pager_end = page * self._blog_post_per_page
136 blog_posts = blog_posts[pager_begin:pager_end]
138 tag_obj = request.registry['blog.tag']
139 tag_ids = tag_obj.search(cr, uid, [], context=context)
140 tags = tag_obj.browse(cr, uid, tag_ids, context=context)
147 'blog_posts': blog_posts,
149 'nav_list': self.nav_list(),
150 'blog_url': blog_url,
151 'post_url': post_url,
154 response = request.website.render("website_blog.blog_post_short", values)
158 '''/blog/<model("blog.blog"):blog>/post/<model("blog.post", "[('blog_id','=',blog[0])]"):blog_post>''',
159 ], type='http', auth="public", website=True)
160 def blog_post(self, blog, blog_post, tag_id=None, page=1, enable_editor=None, **post):
161 """ Prepare all values to display the blog.
163 :return dict values: values for the templates, containing
165 - 'blog_post': browse of the current post
166 - 'blog': browse of the current blog
167 - 'blogs': list of browse records of blogs
168 - 'tag': current tag, if tag_id in parameters
169 - 'tags': all tags, for tag-based navigation
170 - 'pager': a pager on the comments
171 - 'nav_list': a dict [year][month] for archives navigation
172 - 'next_post': next blog post, to direct the user towards the next interesting post
174 cr, uid, context = request.cr, request.uid, request.context
175 tag_obj = request.registry['blog.tag']
176 blog_post_obj = request.registry['blog.post']
177 date_begin, date_end = post.get('date_begin'), post.get('date_end')
179 pager_url = "/blogpost/%s" % blog_post.id
181 pager = request.website.pager(
183 total=len(blog_post.website_message_ids),
185 step=self._post_comment_per_page,
188 pager_begin = (page - 1) * self._post_comment_per_page
189 pager_end = page * self._post_comment_per_page
190 comments = blog_post.website_message_ids[pager_begin:pager_end]
194 tag = request.registry['blog.tag'].browse(request.cr, request.uid, int(tag_id), context=request.context)
195 post_url = QueryURL('', ['blogpost'], blogpost=blog_post, tag_id=tag_id, date_begin=date_begin, date_end=date_end)
196 blog_url = QueryURL('', ['blog', 'tag'], blog=blog_post.blog_id, tag=tag, date_begin=date_begin, date_end=date_end)
198 if not blog_post.blog_id.id == blog.id:
199 return request.redirect("/blog/%s/post/%s" % (slug(blog_post.blog_id), slug(blog_post)))
201 tags = tag_obj.browse(cr, uid, tag_obj.search(cr, uid, [], context=context), context=context)
204 visited_blogs = request.httprequest.cookies.get('visited_blogs') or ''
205 visited_ids = filter(None, visited_blogs.split(','))
206 visited_ids = map(lambda x: int(x), visited_ids)
207 if blog_post.id not in visited_ids:
208 visited_ids.append(blog_post.id)
209 next_post_id = blog_post_obj.search(cr, uid, [
210 ('id', 'not in', visited_ids),
211 ], order='ranking desc', limit=1, context=context)
213 next_post_id = blog_post_obj.search(cr, uid, [('id', '!=', blog.id)], order='ranking desc', limit=1, context=context)
214 next_post = next_post_id and blog_post_obj.browse(cr, uid, next_post_id[0], context=context) or False
220 'blog_post': blog_post,
221 'main_object': blog_post,
222 'nav_list': self.nav_list(),
223 'enable_editor': enable_editor,
224 'next_post': next_post,
226 'post_url': post_url,
227 'blog_url': blog_url,
229 'comments': comments,
231 response = request.website.render("website_blog.blog_post_complete", values)
232 response.set_cookie('visited_blogs', ','.join(map(str, visited_ids)))
234 request.session[request.session_id] = request.session.get(request.session_id, [])
235 if not (blog_post.id in request.session[request.session_id]):
236 request.session[request.session_id].append(blog_post.id)
238 blog_post_obj.write(cr, SUPERUSER_ID, [blog_post.id], {
239 'visits': blog_post.visits+1,
243 def _blog_post_message(self, user, blog_post_id=0, **post):
244 cr, uid, context = request.cr, request.uid, request.context
245 blog_post = request.registry['blog.post']
246 partner_obj = request.registry['res.partner']
247 thread_obj = request.registry['mail.thread']
249 if uid != request.website.user_id.id:
250 partner_ids = [user.partner_id.id]
252 partner_ids = blog_post._find_partner_from_emails(
253 cr, SUPERUSER_ID, 0, [post.get('email')], context=context)
254 if not partner_ids or not partner_ids[0]:
255 partner_ids = [partner_obj.create(cr, SUPERUSER_ID, {'name': post.get('name'), 'email': post.get('email')}, context=context)]
257 message_id = blog_post.message_post(
258 cr, SUPERUSER_ID, int(blog_post_id),
259 body=post.get('comment'),
261 subtype='mt_comment',
262 author_id=partner_ids[0],
263 path=post.get('path', False),
264 context=dict(context, mail_create_nosubcribe=True))
267 @http.route(['/blogpost/comment'], type='http', auth="public", methods=['POST'], website=True)
268 def blog_post_comment(self, blog_post_id=0, **post):
269 cr, uid, context = request.cr, request.uid, request.context
270 if post.get('comment'):
271 user = request.registry['res.users'].browse(cr, uid, uid, context=context)
272 blog_post = request.registry['blog.post']
273 blog_post.check_access_rights(cr, uid, 'read')
274 self._blog_post_message(user, blog_post_id, **post)
275 return werkzeug.utils.redirect(request.httprequest.referrer + "#comments")
277 def _get_discussion_detail(self, ids, publish=False, **post):
278 cr, uid, context = request.cr, request.uid, request.context
280 mail_obj = request.registry.get('mail.message')
281 for message in mail_obj.browse(cr, SUPERUSER_ID, ids, context=context):
284 "author_name": message.author_id.name,
285 "author_image": message.author_id.image and \
286 ("data:image/png;base64,%s" % message.author_id.image) or \
287 '/website_blog/static/src/img/anonymous.png',
288 "date": message.date,
289 'body': html2plaintext(message.body),
290 'website_published' : message.website_published,
295 @http.route(['/blogpost/post_discussion'], type='json', auth="public", website=True)
296 def post_discussion(self, blog_post_id, **post):
297 cr, uid, context = request.cr, request.uid, request.context
298 publish = request.registry['res.users'].has_group(cr, uid, 'base.group_website_publisher')
299 user = request.registry['res.users'].browse(cr, uid, uid, context=context)
300 id = self._blog_post_message(user, blog_post_id, **post)
301 return self._get_discussion_detail([id], publish, **post)
303 @http.route('/blogpost/new', type='http', auth="public", website=True)
304 def blog_post_create(self, blog_id, **post):
305 cr, uid, context = request.cr, request.uid, request.context
306 create_context = dict(context, mail_create_nosubscribe=True)
307 new_blog_post_id = request.registry['blog.post'].create(cr, uid, {
309 'name': _("Blog Post Title"),
310 'subtitle': _("Subtitle"),
312 'website_published': False,
313 }, context=create_context)
314 new_blog_post = request.registry['blog.post'].browse(cr, uid, new_blog_post_id, context=context)
315 return werkzeug.utils.redirect("/blog/%s/post/%s?enable_editor=1" % (slug(new_blog_post.blog_id), slug(new_blog_post)))
317 @http.route('/blogpost/duplicate', type='http', auth="public", website=True)
318 def blog_post_copy(self, blog_post_id, **post):
319 """ Duplicate a blog.
321 :param blog_post_id: id of the blog post currently browsed.
323 :return redirect to the new blog created
325 cr, uid, context = request.cr, request.uid, request.context
326 create_context = dict(context, mail_create_nosubscribe=True)
327 nid = request.registry['blog.post'].copy(cr, uid, blog_post_id, {}, context=create_context)
328 new_blog_post = request.registry['blog.post'].browse(cr, uid, nid, context=context)
329 post = request.registry['blog.post'].browse(cr, uid, nid, context)
330 return werkzeug.utils.redirect("/blog/%s/post/%s?enable_editor=1" % (slug(post.blog_id), slug(new_blog_post)))
332 @http.route('/blogpost/get_discussion/', type='json', auth="public", website=True)
333 def discussion(self, post_id=0, path=None, count=False, **post):
334 cr, uid, context = request.cr, request.uid, request.context
335 mail_obj = request.registry.get('mail.message')
336 domain = [('res_id', '=', int(post_id)), ('model', '=', 'blog.post'), ('path', '=', path)]
337 #check current user belongs to website publisher group
338 publish = request.registry['res.users'].has_group(cr, uid, 'base.group_website_publisher')
340 domain.append(('website_published', '=', True))
341 ids = mail_obj.search(cr, SUPERUSER_ID, domain, count=count)
344 return self._get_discussion_detail(ids, publish, **post)
346 @http.route('/blogpost/change_background', type='json', auth="public", website=True)
347 def change_bg(self, post_id=0, image=None, **post):
350 return request.registry['blog.post'].write(request.cr, request.uid, [int(post_id)], {'background_image': image}, request.context)
352 @http.route('/blog/get_user/', type='json', auth="public", website=True)
353 def get_user(self, **post):
354 return [False if request.session.uid else True]