summaryrefslogtreecommitdiff
path: root/addons/website_blog/controllers/main.py
diff options
context:
space:
mode:
authorJeremy Kersten <jke@odoo.com>2019-08-20 14:41:52 +0000
committerJeremy Kersten <jke@odoo.com>2019-08-20 15:47:39 +0000
commit0d1fdb0b95a671e50d9875a81bfab88eeae910bf (patch)
treeb1c80a545ffb5b394861f21c36baa5df190aca2f /addons/website_blog/controllers/main.py
parentbb0cdec4594fab8c22265ed8af0c2d431a263b72 (diff)
[IMP] website_blog: refactoring python code for new layout
task-1973902 + use of img locally + last PO review: + show latest in the list + remove underline + force same height if blog or not + ... closes odoo/odoo#34609 Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
Diffstat (limited to 'addons/website_blog/controllers/main.py')
-rw-r--r--addons/website_blog/controllers/main.py229
1 files changed, 84 insertions, 145 deletions
diff --git a/addons/website_blog/controllers/main.py b/addons/website_blog/controllers/main.py
index f6ee1a1debe..4fa76d217e2 100644
--- a/addons/website_blog/controllers/main.py
+++ b/addons/website_blog/controllers/main.py
@@ -16,9 +16,18 @@ from odoo.tools import html2plaintext
class WebsiteBlog(http.Controller):
- _blog_post_per_page = 20
+ _blog_post_per_page = 12 # multiple of 2,3,4
_post_comment_per_page = 10
+ def tags_list(self, tag_ids, current_tag):
+ tag_ids = list(tag_ids) # required to avoid using the same list
+ if current_tag in tag_ids:
+ tag_ids.remove(current_tag)
+ else:
+ tag_ids.append(current_tag)
+ tag_ids = request.env['blog.tag'].browse(tag_ids).exists()
+ return ','.join(slug(tag) for tag in tag_ids)
+
def nav_list(self, blog=None):
dom = blog and [('blog_id', '=', blog.id)] or []
if not request.env.user.has_group('website.group_website_designer'):
@@ -43,111 +52,24 @@ class WebsiteBlog(http.Controller):
return OrderedDict((year, [m for m in months]) for year, months in itertools.groupby(groups, lambda g: g['year']))
- @http.route([
- '/blog',
- '/blog/page/<int:page>',
- ], type='http', auth="public", website=True)
- def blogs(self, page=1, tag=None, **post):
- domain = request.website.website_domain()
+ def _prepare_blog_values(self, blogs=False, date_begin=False, date_end=False, tags=False, state=False, page=False):
+ """ Prepare all values to display the blogs index page or one specific blog"""
Blog = request.env['blog.blog']
- blogs = Blog.search(domain, order="create_date, id asc")
- if len(blogs) == 1:
- return werkzeug.utils.redirect('/blog%s' % slug(blogs[0]), code=302)
-
BlogPost = request.env['blog.post']
- total = BlogPost.search_count(domain)
-
- pager = request.website.pager(
- url='/blog',
- total=total,
- page=page,
- step=self._blog_post_per_page,
- )
- date_begin, date_end = post.get('date_begin'), post.get('date_end')
- if date_begin and date_end:
- domain += [("post_date", ">=", date_begin), ("post_date", "<=", date_end)]
-
- # retrocompatibility to accept tag as slug
- active_tag_ids = tag and [int(unslug(t)[1]) for t in tag.split(',')] or []
- if active_tag_ids:
- domain += [('tag_ids', 'in', active_tag_ids)]
-
- # function to create the string list of tag ids.
- def tags_list(tag_ids, current_tag):
- tag_ids = list(tag_ids) # required to avoid using the same list
- if current_tag in tag_ids:
- tag_ids.remove(current_tag)
- else:
- tag_ids.append(current_tag)
- tag_ids = request.env['blog.tag'].browse(tag_ids).exists()
- return ','.join(slug(tag) for tag in tag_ids)
-
- posts = BlogPost.search(domain, offset=(page - 1) * self._blog_post_per_page, limit=self._blog_post_per_page)
-
- first_post = next((p for p in posts if p.website_published), None)
- first_cover = first_post and json.loads(first_post.cover_properties)
-
- blog_url = QueryURL('', ['blog', 'tag'])
- return request.render("website_blog.blog_post_short", {
- 'posts': posts,
- 'pager': pager,
- 'blogs': blogs,
- 'blog_url': blog_url,
- 'nav_list': self.nav_list(),
- 'date_begin': date_begin,
- 'date_end': date_end,
- 'tags_list' : tags_list,
- 'active_tag_ids': active_tag_ids,
- 'first_post': first_post,
- 'first_cover': first_cover,
- 'blog_posts_cover_properties': [json.loads(b.cover_properties) for b in posts],
- })
-
- @http.route([
- '''/blog/<model("blog.blog", "[('website_id', 'in', (False, current_website_id))]"):blog>''',
- '''/blog/<model("blog.blog"):blog>/page/<int:page>''',
- '''/blog/<model("blog.blog"):blog>/tag/<string:tag>''',
- '''/blog/<model("blog.blog"):blog>/tag/<string:tag>/page/<int:page>''',
- ], type='http', auth="public", website=True)
- def blog(self, blog=None, tag=None, page=1, **opt):
- """ Prepare all values to display the blog.
-
- :return dict values: values for the templates, containing
-
- - 'blog': current blog
- - 'blogs': all blogs for navigation
- - 'pager': pager of posts
- - 'active_tag_ids' : list of active tag ids,
- - 'tag': current tag, if tag_id in parameters
- - 'tags_list' : function to built the comma-separated tag list ids (for the url),
- - 'tags': all tags, for navigation
- - 'state_info': state of published/unpublished filter
- - 'nav_list': a dict [year][month] for archives navigation
- - 'date': date_begin optional parameter, used in archives navigation
- - 'blog_url': help object to create URLs
- """
- if not blog.can_access_from_current_website():
- raise werkzeug.exceptions.NotFound()
-
- date_begin, date_end, state = opt.get('date_begin'), opt.get('date_end'), opt.get('state')
- published_count, unpublished_count = 0, 0
+ # prepare domain
domain = request.website.website_domain()
- BlogPost = request.env['blog.post']
+ if len(blogs) == 1:
+ domain += [('blog_id', '=', blogs.id)]
- Blog = request.env['blog.blog']
- blogs = Blog.search(domain, order="create_date asc")
+ if date_begin and date_end:
+ domain += [("post_date", ">=", date_begin), ("post_date", "<=", date_end)]
- # retrocompatibility to accept tag as slug
- active_tag_ids = tag and [int(unslug(t)[1]) for t in tag.split(',')] or []
+ active_tag_ids = tags and [unslug(tag)[1] for tag in tags.split(',')] or []
if active_tag_ids:
domain += [('tag_ids', 'in', active_tag_ids)]
- if blog:
- domain += [('blog_id', '=', blog.id)]
- if date_begin and date_end:
- domain += [("post_date", ">=", date_begin), ("post_date", "<=", date_end)]
if request.env.user.has_group('website.group_website_designer'):
count_domain = domain + [("website_published", "=", True), ("post_date", "<=", fields.Datetime.now())]
@@ -161,77 +83,94 @@ class WebsiteBlog(http.Controller):
else:
domain += [("post_date", "<=", fields.Datetime.now())]
- blog_url = QueryURL('', ['blog', 'tag'], blog=blog, tag=tag, date_begin=date_begin, date_end=date_end)
-
- posts = BlogPost.search(domain, order="post_date, id asc")
-
- first_post = next((p for p in posts if p.website_published), None)
- first_cover = first_post and json.loads(first_post.cover_properties)
-
+ total = BlogPost.search_count(domain)
+ posts = BlogPost.search(domain, offset=(page - 1) * self._blog_post_per_page, limit=self._blog_post_per_page, order="post_date, id asc")
pager = request.website.pager(
url=request.httprequest.path.partition('/page/')[0],
- total=len(posts),
+ total=total,
page=page,
step=self._blog_post_per_page,
- url_args=opt,
)
- pager_begin = (page - 1) * self._blog_post_per_page
- pager_end = page * self._blog_post_per_page
- posts = posts[pager_begin:pager_end]
-
- all_tags = blog.all_tags()[blog.id]
-
- # function to create the string list of tag ids, and toggle a given one.
- # used in the 'Tags Cloud' template.
- def tags_list(tag_ids, current_tag):
- tag_ids = list(tag_ids) # required to avoid using the same list
- if current_tag in tag_ids:
- tag_ids.remove(current_tag)
- else:
- tag_ids.append(current_tag)
- tag_ids = request.env['blog.tag'].browse(tag_ids).exists()
- return ','.join(slug(tag) for tag in tag_ids)
+ first_post = next((p for p in posts if p.website_published), None)
+ first_cover = first_post and json.loads(first_post.cover_properties)
+ covers = [json.loads(b.cover_properties) for b in posts]
+
+ all_tags = len(blogs) == 1 and blogs.all_tags()[blogs.id] or blogs.all_tags(join=True)
tag_category = sorted(all_tags.mapped('category_id'), key=lambda category: category.name.upper())
- other_tags = sorted(all_tags.filtered(lambda x: not x.category_id), key=lambda tag: tag.name.upper())
+ other_tags = sorted(all_tags.filtered(lambda x: not x.category_id), key=lambda tag: tag.name.upper())
- values = {
- 'blog': blog,
- 'blogs': blogs,
- 'main_object': blog,
- 'other_tags': other_tags,
- 'state_info': {"state": state, "published": published_count, "unpublished": unpublished_count},
- 'active_tag_ids': active_tag_ids,
- 'tag': tag,
- 'tags_list' : tags_list,
- 'posts': posts,
- 'first_post': first_post,
- 'first_cover': first_cover,
- 'blog_posts_cover_properties': [json.loads(b.cover_properties) for b in posts],
- 'pager': pager,
- 'nav_list': self.nav_list(blog),
- 'blog_url': blog_url,
+ return {
'date_begin': date_begin,
'date_end': date_end,
+ 'first_post': first_post,
+ 'first_cover': first_cover,
+ 'blog_posts_cover_properties': covers,
+ 'other_tags': other_tags,
'tag_category': tag_category,
+ 'nav_list': self.nav_list(),
+ 'tags_list': self.tags_list,
+ 'pager': pager,
+ 'posts': posts,
+ 'tag': tags,
+ 'active_tag_ids': active_tag_ids,
+ 'domain': domain,
+ 'state_info': state and {"state": state, "published": published_count, "unpublished": unpublished_count},
+ 'blogs': Blog.search(request.website.website_domain(), order="create_date asc, id asc"),
}
- response = request.render("website_blog.blog_post_short", values)
- return response
+
+ @http.route([
+ '/blog',
+ '/blog/page/<int:page>',
+ '/blog/tag/<string:tag>',
+ '/blog/tag/<string:tag>/page/<int:page>',
+ ], type='http', auth="public", website=True)
+ def blogs(self, page=1, tag=None, **post):
+ Blog = request.env['blog.blog']
+ blogs = Blog.search(request.website.website_domain(), order="create_date asc, id asc")
+
+ if len(blogs) == 1:
+ return werkzeug.utils.redirect('/blog/%s' % slug(blogs[0]), code=302)
+
+ date_begin, date_end = post.get('date_begin'), post.get('date_end')
+
+ values = self._prepare_blog_values(blogs=blogs, date_begin=date_begin, date_end=date_end, tags=tag, page=page)
+ values.update({
+ 'blog_url': QueryURL('/blog', ['tag'], date_begin=date_begin, date_end=date_end),
+ })
+ return request.render("website_blog.blog_post_short", values)
+
+ @http.route([
+ '''/blog/<model("blog.blog", "[('website_id', 'in', (False, current_website_id))]"):blog>''',
+ '''/blog/<model("blog.blog"):blog>/page/<int:page>''',
+ '''/blog/<model("blog.blog"):blog>/tag/<string:tag>''',
+ '''/blog/<model("blog.blog"):blog>/tag/<string:tag>/page/<int:page>''',
+ ], type='http', auth="public", website=True)
+ def blog(self, blog=None, tag=None, page=1, **opt):
+ if not blog.can_access_from_current_website():
+ raise werkzeug.exceptions.NotFound()
+
+ date_begin, date_end, state = opt.get('date_begin'), opt.get('date_end'), opt.get('state')
+
+ values = self._prepare_blog_values(blogs=blog, date_begin=date_begin, date_end=date_end, tags=tag, state=state, page=page)
+ values.update({
+ 'blog_url': QueryURL('', ['blog', 'tag'], blog=blog, tag=tag, date_begin=date_begin, date_end=date_end),
+ 'blog': blog,
+ })
+ return request.render("website_blog.blog_post_short", values)
@http.route(['''/blog/<model("blog.blog", "[('website_id', 'in', (False, current_website_id))]"):blog>/feed'''], type='http', auth="public")
def blog_feed(self, blog, limit='15', **kwargs):
v = {}
v['blog'] = blog
v['base_url'] = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
- v['posts'] = request.env['blog.post'].search([('blog_id','=', blog.id)],
- limit=min(int(limit), 50),
- order="post_date DESC")
+ v['posts'] = request.env['blog.post'].search([('blog_id', '=', blog.id)], limit=min(int(limit), 50), order="post_date DESC")
v['html2plaintext'] = html2plaintext
r = request.render("website_blog.blog_feed", v, headers=[('Content-Type', 'application/atom+xml')])
return r
@http.route([
- '''/blog/<model("blog.blog", "[('website_id', 'in', (False, current_website_id))]"):blog>/post/<model("blog.post", "[('blog_id','=',blog[0])]"):blog_post>''',
+ '''/blog/<model("blog.blog", "[('website_id', 'in', (False, current_website_id))]"):blog>/post/<model("blog.post", "[('blog_id','=',blog[0])]"):blog_post>''',
], type='http', auth="public", website=True)
def blog_post(self, blog, blog_post, tag_id=None, page=1, enable_editor=None, **post):
""" Prepare all values to display the blog.
@@ -318,7 +257,7 @@ class WebsiteBlog(http.Controller):
request.session[request.session.sid].append(blog_post.id)
# Increase counter
blog_post.sudo().write({
- 'visits': blog_post.visits+1,
+ 'visits': blog_post.visits + 1,
})
return response