1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from openerp.addons.web import http
23 from openerp.addons.web.http import request
24 from openerp.addons.website.models import website
25 from openerp.tools.translate import _
30 class WebsiteBlog(http.Controller):
31 _category_post_per_page = 6
32 _post_comment_per_page = 6
35 blog_post_obj = request.registry['blog.post']
36 groups = blog_post_obj.read_group(request.cr, request.uid, [], ['name', 'create_date'],
37 groupby="create_date", orderby="create_date asc", context=request.context)
39 group['date'] = "%s_%s" % (group['__domain'][0][2], group['__domain'][1][2])
44 '/blog/page/<int:page>/',
45 ], type='http', auth="public", multilang=True)
46 def blogs(self, page=1):
48 cr, uid, context = request.cr, request.uid, request.context
49 blog_obj = request.registry['blog.post']
50 total = blog_obj.search(cr, uid, [], count=True, context=context)
51 pager = request.website.pager(
57 bids = blog_obj.search(cr, uid, [], offset=(page-1)*BYPAGE, limit=BYPAGE, context=context)
58 blogs = blog_obj.browse(cr, uid, bids, context=context)
59 return request.website.render("website_blog.latest_blogs", {
66 '/blog/page/<int:page>/',
67 '/blog/cat/<model("blog.category"):category>/',
68 '/blog/cat/<model("blog.category"):category>/page/<int:page>/',
69 '/blog/tag/<model("blog.tag"):tag>/',
70 '/blog/tag/<model("blog.tag"):tag>/page/<int:page>/',
71 '/blog/cat/<model("blog.category"):category>/date/<string(length=21):date>/',
72 '/blog/tag/<model("blog.tag"):tag>/date/<string(length=21):date>/',
73 '/blog/tag/<model("blog.tag"):tag>/date/<string(length=21):date>/page/<int:page>/',
74 ], type='http', auth="public", multilang=True)
75 def blog(self, category=None, tag=None, page=1, date=None):
76 """ Prepare all values to display the blog.
78 :param category: category currently browsed.
79 :param tag: tag that is currently used to filter blog posts
80 :param integer page: current page of the pager. Can be the category or
82 :param date: date currently used to filter blog posts (dateBegin_dateEnd)
84 :return dict values: values for the templates, containing
86 - 'blog_posts': list of browse records that are the posts to display
87 in a given category, if not blog_post_id
88 - 'category': browse of the current category, if category_id
89 - 'categories': list of browse records of categories
90 - 'pager': the pager to display posts pager in a category
91 - 'tag': current tag, if tag_id
92 - 'nav_list': a dict [year][month] for archives navigation
96 cr, uid, context = request.cr, request.uid, request.context
97 blog_post_obj = request.registry['blog.post']
101 category_obj = request.registry['blog.category']
102 category_ids = category_obj.search(cr, uid, [], context=context)
103 categories = category_obj.browse(cr, uid, category_ids, context=context)
109 pager_url += "cat/%s/" % category.id
110 domain += [("id", "=", [blog.id for blog in category.blog_post_ids])]
112 pager_url += 'tag/%s/' % tag.id
113 domain += [("id", "=", [blog.id for blog in tag.blog_post_ids])]
115 pager_url += "date/%s/" % date
116 date = date.split("_")
117 domain = [("create_date", ">=", date[0]), ("create_date", "<=", date[1])]
119 blog_post_ids = blog_post_obj.search(cr, uid, domain, context=context)
120 blog_posts = blog_post_obj.browse(cr, uid, blog_post_ids, context=context)
122 pager = request.website.pager(
124 total=len(blog_posts),
126 step=self._category_post_per_page,
129 pager_begin = (page - 1) * self._category_post_per_page
130 pager_end = page * self._category_post_per_page
131 blog_posts = blog_posts[pager_begin:pager_end]
134 'category': category,
135 'categories': categories,
137 'blog_posts': blog_posts,
139 'nav_list': self.nav_list(),
143 values['main_object'] = tag
145 values['main_object'] = category
147 return request.website.render("website_blog.blog_post_short", values)
150 '/blog/<model("blog.post"):blog_post>/',
151 '/blog/<model("blog.post"):blog_post>/page/<int:page>/'
152 ], type='http', auth="public", multilang=True)
153 def blog_post(self, blog_post=None, page=1, enable_editor=None):
154 """ Prepare all values to display the blog.
156 :param blog_post: blog post currently browsed. If not set, the user is
157 browsing the category and a post pager is calculated.
158 If set the user is reading the blog post and a
159 comments pager is calculated.
160 :param integer page: current page of the pager. Can be the category or
162 :param dict post: kwargs, may contain
164 - 'enable_editor': editor control
166 :return dict values: values for the templates, containing
168 - 'blog_post': browse of the current post, if blog_post_id
169 - 'category': browse of the current category, if category_id
170 - 'pager': the pager to display comments pager in a blog post
171 - 'nav_list': a dict [year][month] for archives navigation
173 pager = request.website.pager(
174 url="/blog/%s/" % blog_post.id,
175 total=len(blog_post.website_message_ids),
177 step=self._post_comment_per_page,
180 pager_begin = (page - 1) * self._post_comment_per_page
181 pager_end = page * self._post_comment_per_page
182 blog_post.website_message_ids = blog_post.website_message_ids[pager_begin:pager_end]
184 cr, uid, context = request.cr, request.uid, request.context
185 category_obj = request.registry['blog.category']
186 category_ids = category_obj.search(cr, uid, [], context=context)
187 categories = category_obj.browse(cr, uid, category_ids, context=context)
190 'category': blog_post.category_id,
191 'categories': categories,
192 'blog_post': blog_post,
194 'nav_list': self.nav_list(),
195 'enable_editor': enable_editor,
197 return request.website.render("website_blog.blog_post_complete", values)
199 @website.route(['/blog/<int:blog_post_id>/comment'], type='http', auth="public")
200 def blog_post_comment(self, blog_post_id=None, **post):
201 cr, uid, context = request.cr, request.uid, request.context
202 if post.get('comment'):
203 request.registry['blog.post'].message_post(
204 cr, uid, blog_post_id,
205 body=post.get('comment'),
207 subtype='mt_comment',
208 context=dict(context, mail_create_nosubcribe=True))
209 return werkzeug.utils.redirect(request.httprequest.referrer + "#comments")
211 @website.route(['/blog/<int:category_id>/new'], type='http', auth="public", multilang=True)
212 def blog_post_create(self, category_id=None, **post):
213 cr, uid, context = request.cr, request.uid, request.context
214 create_context = dict(context, mail_create_nosubscribe=True)
215 new_blog_post_id = request.registry['blog.post'].create(
216 request.cr, request.uid, {
217 'category_id': category_id,
218 'name': _("Blog title"),
220 'website_published': False,
221 }, context=create_context)
222 return werkzeug.utils.redirect("/blog/%s/?enable_editor=1" % (new_blog_post_id))
224 @website.route(['/blog/<int:blog_post_id>/duplicate'], type='http', auth="public")
225 def blog_post_copy(self, blog_post_id=None, **post):
226 cr, uid, context = request.cr, request.uid, request.context
227 create_context = dict(context, mail_create_nosubscribe=True)
228 new_blog_post_id = request.registry['blog.post'].copy(cr, uid, blog_post_id, {}, context=create_context)
229 return werkzeug.utils.redirect("/blog/%s/?enable_editor=1" % (new_blog_post_id))