2013-09-20 13:56:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from openerp.addons.web import http
|
|
|
|
from openerp.addons.web.http import request
|
2013-10-01 08:57:39 +00:00
|
|
|
from openerp.addons.website.models import website
|
2013-09-20 13:56:43 +00:00
|
|
|
from openerp.tools.translate import _
|
|
|
|
from openerp.tools.safe_eval import safe_eval
|
|
|
|
|
|
|
|
import simplejson
|
|
|
|
import werkzeug
|
|
|
|
|
|
|
|
|
2013-09-30 14:33:57 +00:00
|
|
|
class WebsiteBlog(http.Controller):
|
2013-09-30 08:21:02 +00:00
|
|
|
_category_post_per_page = 6
|
|
|
|
_post_comment_per_page = 6
|
2013-09-20 13:56:43 +00:00
|
|
|
|
|
|
|
@website.route([
|
|
|
|
'/blog/',
|
2013-11-08 14:21:49 +00:00
|
|
|
'/blog/page/<int:page>/',
|
|
|
|
'/blog/<model("blog.post"):blog_post>/',
|
|
|
|
'/blog/<model("blog.post"):blog_post>/page/<int:page>/',
|
|
|
|
'/blog/cat/<model("blog.category"):category>/',
|
|
|
|
'/blog/cat/<model("blog.category"):category>/page/<int:page>/',
|
|
|
|
'/blog/tag/<model("blog.tag"):tag>/',
|
|
|
|
'/blog/tag/<model("blog.tag"):tag>/page/<int:page>/',
|
2013-10-30 16:29:09 +00:00
|
|
|
], type='http', auth="public", multilang=True)
|
2013-11-08 14:21:49 +00:00
|
|
|
def blog(self, category=None, blog_post=None, tag=None, page=1, enable_editor=None):
|
2013-09-23 13:47:02 +00:00
|
|
|
""" Prepare all values to display the blog.
|
|
|
|
|
2013-11-08 14:21:49 +00:00
|
|
|
:param category: category currently browsed.
|
|
|
|
:param tag: tag that is currently used to filter blog posts
|
|
|
|
:param blog_post: blog post currently browsed. If not set, the user is
|
|
|
|
browsing the category and a post pager is calculated.
|
|
|
|
If set the user is reading the blog post and a
|
|
|
|
comments pager is calculated.
|
2013-09-23 13:47:02 +00:00
|
|
|
:param integer page: current page of the pager. Can be the category or
|
|
|
|
post pager.
|
|
|
|
:param dict post: kwargs, may contain
|
|
|
|
|
2013-10-23 09:11:52 +00:00
|
|
|
- 'enable_editor': editor control
|
2013-09-23 13:47:02 +00:00
|
|
|
|
|
|
|
:return dict values: values for the templates, containing
|
|
|
|
|
|
|
|
- 'blog_post': browse of the current post, if blog_post_id
|
|
|
|
- 'blog_posts': list of browse records that are the posts to display
|
|
|
|
in a given category, if not blog_post_id
|
|
|
|
- 'category': browse of the current category, if category_id
|
|
|
|
- 'categories': list of browse records of categories
|
|
|
|
- 'pager': the pager to display, posts pager in a category or comments
|
|
|
|
pager in a blog post
|
2013-09-23 15:40:39 +00:00
|
|
|
- 'tag': current tag, if tag_id
|
2013-09-23 13:47:02 +00:00
|
|
|
- 'nav_list': a dict [year][month] for archives navigation
|
|
|
|
"""
|
2013-09-20 13:56:43 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
blog_post_obj = request.registry['blog.post']
|
|
|
|
category_obj = request.registry['blog.category']
|
2013-09-30 14:33:57 +00:00
|
|
|
|
2013-09-20 13:56:43 +00:00
|
|
|
blog_posts = None
|
2013-09-23 10:17:30 +00:00
|
|
|
|
|
|
|
category_ids = category_obj.search(cr, uid, [], context=context)
|
|
|
|
categories = category_obj.browse(cr, uid, category_ids, context=context)
|
2013-09-23 15:40:39 +00:00
|
|
|
|
2013-11-08 14:21:49 +00:00
|
|
|
if blog_post:
|
2013-10-11 13:00:59 +00:00
|
|
|
category = blog_post.category_id
|
2013-11-08 14:21:49 +00:00
|
|
|
pager = request.website.pager(
|
|
|
|
url="/blog/%s/" % blog_post.id,
|
|
|
|
total=len(blog_post.website_message_ids),
|
|
|
|
page=page,
|
|
|
|
step=self._post_comment_per_page,
|
|
|
|
scope=7
|
|
|
|
)
|
|
|
|
pager_begin = (page - 1) * self._post_comment_per_page
|
|
|
|
pager_end = page * self._post_comment_per_page
|
|
|
|
blog_post.website_message_ids = blog_post.website_message_ids[pager_begin:pager_end]
|
|
|
|
else:
|
|
|
|
if category:
|
|
|
|
pager_url = "/blog/cat/%s/" % category.id
|
2013-09-23 15:40:39 +00:00
|
|
|
blog_posts = category.blog_post_ids
|
|
|
|
elif tag:
|
2013-11-08 14:21:49 +00:00
|
|
|
pager_url = '/blog/tag/%s/' % tag.id
|
2013-09-23 15:40:39 +00:00
|
|
|
blog_posts = tag.blog_post_ids
|
2013-09-30 14:33:57 +00:00
|
|
|
else:
|
2013-11-08 14:21:49 +00:00
|
|
|
pager_url = '/blog/'
|
2013-09-30 14:33:57 +00:00
|
|
|
blog_post_ids = blog_post_obj.search(cr, uid, [], context=context)
|
|
|
|
blog_posts = blog_post_obj.browse(cr, uid, blog_post_ids, context=context)
|
2013-09-23 15:40:39 +00:00
|
|
|
|
|
|
|
pager = request.website.pager(
|
2013-11-08 14:21:49 +00:00
|
|
|
url=pager_url,
|
2013-09-23 15:40:39 +00:00
|
|
|
total=len(blog_posts),
|
|
|
|
page=page,
|
|
|
|
step=self._category_post_per_page,
|
|
|
|
scope=7
|
|
|
|
)
|
|
|
|
pager_begin = (page - 1) * self._category_post_per_page
|
|
|
|
pager_end = page * self._category_post_per_page
|
|
|
|
blog_posts = blog_posts[pager_begin:pager_end]
|
|
|
|
|
2013-11-08 14:51:42 +00:00
|
|
|
nav = {}
|
2013-09-23 13:47:02 +00:00
|
|
|
for group in blog_post_obj.read_group(cr, uid, [], ['name', 'create_date'], groupby="create_date", orderby="create_date asc", context=context):
|
2013-11-08 14:51:42 +00:00
|
|
|
# FIXME: vietnamese month names contain spaces. Set sail for fail.
|
2013-09-23 13:47:02 +00:00
|
|
|
year = group['create_date'].split(" ")[1]
|
|
|
|
if not year in nav:
|
|
|
|
nav[year] = {'name': year, 'create_date_count': 0, 'months': []}
|
|
|
|
nav[year]['create_date_count'] += group['create_date_count']
|
|
|
|
nav[year]['months'].append(group)
|
|
|
|
|
|
|
|
values = {
|
2013-09-20 13:56:43 +00:00
|
|
|
'category': category,
|
2013-10-11 13:00:59 +00:00
|
|
|
'categories': categories,
|
2013-09-23 15:40:39 +00:00
|
|
|
'tag': tag,
|
|
|
|
'blog_post': blog_post,
|
|
|
|
'blog_posts': blog_posts,
|
2013-09-20 13:56:43 +00:00
|
|
|
'pager': pager,
|
2013-09-23 13:47:02 +00:00
|
|
|
'nav_list': nav,
|
2013-11-08 14:21:49 +00:00
|
|
|
'enable_editor': enable_editor,
|
2013-09-23 13:47:02 +00:00
|
|
|
}
|
2013-09-20 13:56:43 +00:00
|
|
|
|
2013-10-17 10:40:53 +00:00
|
|
|
if blog_post:
|
|
|
|
values['main_object'] = blog_post
|
2013-10-17 11:58:49 +00:00
|
|
|
elif tag:
|
|
|
|
values['main_object'] = tag
|
|
|
|
elif category:
|
|
|
|
values['main_object'] = category
|
2013-10-17 10:40:53 +00:00
|
|
|
|
2013-09-20 13:56:43 +00:00
|
|
|
return request.website.render("website_blog.index", values)
|
|
|
|
|
2013-10-31 13:56:20 +00:00
|
|
|
# TODO: Refactor (used in website_blog.js for archive links)
|
2013-11-05 15:27:47 +00:00
|
|
|
# => the archive links should be generated server side
|
2013-10-31 13:56:20 +00:00
|
|
|
@website.route(['/blog/nav'], type='http', auth="public", multilang=True)
|
2013-09-23 10:35:34 +00:00
|
|
|
def nav(self, **post):
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
blog_post_ids = request.registry['blog.post'].search(
|
|
|
|
cr, uid, safe_eval(post.get('domain')),
|
|
|
|
order="create_date asc",
|
|
|
|
limit=None,
|
|
|
|
context=context
|
|
|
|
)
|
|
|
|
blog_post_data = [
|
|
|
|
{
|
|
|
|
'id': blog_post.id,
|
|
|
|
'website_published': blog_post.website_published,
|
2013-10-31 13:56:20 +00:00
|
|
|
'fragment': request.website.render("website_blog.blog_archive_link", {
|
|
|
|
'blog_post': blog_post
|
|
|
|
}),
|
2013-09-23 10:35:34 +00:00
|
|
|
}
|
|
|
|
for blog_post in request.registry['blog.post'].browse(cr, uid, blog_post_ids, context=context)
|
|
|
|
]
|
|
|
|
return simplejson.dumps(blog_post_data)
|
2013-09-20 13:56:43 +00:00
|
|
|
|
2013-11-04 17:23:56 +00:00
|
|
|
@website.route(['/blog/<int:blog_post_id>/comment'], type='http', auth="public")
|
2013-10-11 13:00:59 +00:00
|
|
|
def blog_post_comment(self, blog_post_id=None, **post):
|
2013-09-20 13:56:43 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-11-04 17:23:56 +00:00
|
|
|
request.registry['blog.post'].message_post(
|
|
|
|
cr, uid, blog_post_id,
|
|
|
|
body=post.get('comment'),
|
|
|
|
type='comment',
|
|
|
|
subtype='mt_comment',
|
|
|
|
context=dict(context, mail_create_nosubcribe=True))
|
2013-11-05 09:56:22 +00:00
|
|
|
return werkzeug.utils.redirect(request.httprequest.referrer + "#comments")
|
2013-09-20 13:56:43 +00:00
|
|
|
|
2013-11-12 15:37:49 +00:00
|
|
|
@website.route(['/blog/<int:category_id>/new'], type='http', auth="public", multilang=True)
|
2013-10-11 13:00:59 +00:00
|
|
|
def blog_post_create(self, category_id=None, **post):
|
2013-09-20 13:56:43 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
create_context = dict(context, mail_create_nosubscribe=True)
|
2013-10-11 13:00:59 +00:00
|
|
|
new_blog_post_id = request.registry['blog.post'].create(
|
2013-09-20 13:56:43 +00:00
|
|
|
request.cr, request.uid, {
|
|
|
|
'category_id': category_id,
|
|
|
|
'name': _("Blog title"),
|
|
|
|
'content': '',
|
|
|
|
'website_published': False,
|
|
|
|
}, context=create_context)
|
2013-10-23 09:11:52 +00:00
|
|
|
return werkzeug.utils.redirect("/blog/%s/?enable_editor=1" % (new_blog_post_id))
|
2013-10-11 13:00:59 +00:00
|
|
|
|
|
|
|
@website.route(['/blog/<int:blog_post_id>/duplicate'], type='http', auth="public")
|
|
|
|
def blog_post_copy(self, blog_post_id=None, **post):
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
create_context = dict(context, mail_create_nosubscribe=True)
|
|
|
|
new_blog_post_id = request.registry['blog.post'].copy(cr, uid, blog_post_id, {}, context=create_context)
|
2013-10-23 09:11:52 +00:00
|
|
|
return werkzeug.utils.redirect("/blog/%s/?enable_editor=1" % (new_blog_post_id))
|