[IMP] website_blog: url helper defintiion + use; still blog posts to fix.

bzr revid: tde@openerp.com-20140331152711-yg34m7klexuu6v8t
This commit is contained in:
Thibault Delavallée 2014-03-31 17:27:11 +02:00
parent fd43cecbda
commit 2555249fbb
2 changed files with 87 additions and 53 deletions

View File

@ -19,13 +19,48 @@
#
##############################################################################
import datetime
import werkzeug
from openerp import tools
from openerp.addons.web import http
from openerp.addons.web.http import request
from openerp.addons.website.models.website import slug
from openerp.osv.orm import browse_record
from openerp.tools.translate import _
from openerp import SUPERUSER_ID
import werkzeug
class QueryURL(object):
def __init__(self, path='', path_args=None, **args):
self.path = path
self.args = args
self.path_args = set(path_args or [])
def __call__(self, path=None, path_args=None, **kw):
if not path:
path = self.path
for k, v in self.args.items():
kw.setdefault(k, v)
path_args = set(path_args or []).union(self.path_args)
paths, fragments = [], []
for key, value in kw.items():
if value and key in path_args:
if isinstance(value, browse_record):
paths.append((key, slug(value)))
else:
paths.append((key, value))
elif value:
if isinstance(value, list) or isinstance(value, set):
fragments.append(werkzeug.url_encode([(key, item) for item in value]))
else:
fragments.append(werkzeug.url_encode([(key, value)]))
if paths:
for key, value in paths:
path += '/' + key + '/%s' % value
if fragments:
path += '?' + '&'.join(fragments)
return path
class WebsiteBlog(http.Controller):
@ -37,15 +72,17 @@ class WebsiteBlog(http.Controller):
groups = blog_post_obj.read_group(request.cr, request.uid, [], ['name', 'create_date'],
groupby="create_date", orderby="create_date asc", context=request.context)
for group in groups:
group['date'] = "%s_%s" % (group['__domain'][0][2], group['__domain'][1][2])
begin_date = datetime.datetime.strptime(group['__domain'][0][2], tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
end_date = datetime.datetime.strptime(group['__domain'][1][2], tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
group['date_begin'] = '%s' % datetime.date.strftime(begin_date, tools.DEFAULT_SERVER_DATE_FORMAT)
group['date_end'] = '%s' % datetime.date.strftime(end_date, tools.DEFAULT_SERVER_DATE_FORMAT)
return groups
@http.route([
'/blog',
'/blog/page/<int:page>',
], type='http', auth="public", website=True, multilang=True)
def blogs(self, page=1):
BYPAGE = 60
def blogs(self, page=1, **post):
cr, uid, context = request.cr, request.uid, request.context
blog_obj = request.registry['blog.post']
total = blog_obj.search(cr, uid, [], count=True, context=context)
@ -53,13 +90,15 @@ class WebsiteBlog(http.Controller):
url='/blog',
total=total,
page=page,
step=BYPAGE,
step=self._blog_post_per_page,
)
bids = blog_obj.search(cr, uid, [], offset=(page-1)*BYPAGE, limit=BYPAGE, context=context)
blogs = blog_obj.browse(cr, uid, bids, context=context)
post_ids = blog_obj.search(cr, uid, [], offset=(page-1)*self._blog_post_per_page, limit=self._blog_post_per_page, context=context)
posts = blog_obj.browse(cr, uid, post_ids, context=context)
current_url = QueryURL('', ['blog', 'tag'])
return request.website.render("website_blog.latest_blogs", {
'blogs': blogs,
'pager': pager
'posts': posts,
'pager': pager,
'current_url': current_url,
})
@http.route([
@ -67,12 +106,8 @@ class WebsiteBlog(http.Controller):
'/blog/<model("blog.blog"):blog>/page/<int:page>',
'/blog/<model("blog.blog"):blog>/tag/<model("blog.tag"):tag>',
'/blog/<model("blog.blog"):blog>/tag/<model("blog.tag"):tag>/page/<int:page>',
'/blog/<model("blog.blog"):blog>/date/<string(length=21):date>',
'/blog/<model("blog.blog"):blog>/date/<string(length=21):date>/page/<int:page>',
'/blog/<model("blog.blog"):blog>/tag/<model("blog.tag"):tag>/date/<string(length=21):date>',
'/blog/<model("blog.blog"):blog>/tag/<model("blog.tag"):tag>/date/<string(length=21):date>/page/<int:page>',
], type='http', auth="public", website=True, multilang=True)
def blog(self, blog=None, tag=None, date=None, page=1, **opt):
def blog(self, blog=None, tag=None, page=1, **opt):
""" Prepare all values to display the blog.
:param blog: blog currently browsed.
@ -91,7 +126,7 @@ class WebsiteBlog(http.Controller):
- 'tag': current tag, if tag_id
- 'nav_list': a dict [year][month] for archives navigation
"""
BYPAGE = 10
date_begin, date_end = opt.get('date_begin'), opt.get('date_end')
cr, uid, context = request.cr, request.uid, request.context
blog_post_obj = request.registry['blog.post']
@ -102,28 +137,24 @@ class WebsiteBlog(http.Controller):
blog_ids = blog_obj.search(cr, uid, [], context=context)
blogs = blog_obj.browse(cr, uid, blog_ids, context=context)
path_filter = ""
domain = []
if blog:
path_filter += "%s" % blog.id
domain += [("id", "in", [post.id for post in blog.blog_post_ids])]
if tag:
path_filter += 'tag/%s' % tag.id
domain += [("id", "in", [post.id for post in tag.blog_post_ids])]
if date:
path_filter += "date/%s" % date
domain += [("create_date", ">=", date.split("_")[0]), ("create_date", "<=", date.split("_")[1])]
if date_begin and date_end:
domain += [("create_date", ">=", date_begin), ("create_date", "<=", date_end)]
current_url = QueryURL('', ['blog', 'tag'], blog=blog, tag=tag, date_begin=date_begin, date_end=date_end)
blog_post_ids = blog_post_obj.search(cr, uid, domain, context=context)
blog_posts = blog_post_obj.browse(cr, uid, blog_post_ids, context=context)
pager = request.website.pager(
url="/blog/%s" % path_filter,
url=current_url(),
total=len(blog_posts),
page=page,
step=self._blog_post_per_page,
scope=BYPAGE
)
pager_begin = (page - 1) * self._blog_post_per_page
pager_end = page * self._blog_post_per_page
@ -141,15 +172,15 @@ class WebsiteBlog(http.Controller):
'blog_posts': blog_posts,
'pager': pager,
'nav_list': self.nav_list(),
'path_filter': path_filter,
'date': date,
'current_url': current_url,
'date': date_begin,
}
return request.website.render("website_blog.blog_post_short", values)
@http.route([
'/blogpost/<model("blog.post"):blog_post>',
], type='http', auth="public", website=True, multilang=True)
def blog_post(self, blog_post, tag=None, date=None, page=1, enable_editor=None, **post):
def blog_post(self, blog_post, tag_id=None, page=1, enable_editor=None, **post):
""" Prepare all values to display the blog.
:param blog_post: blog post currently browsed. If not set, the user is
@ -173,6 +204,7 @@ class WebsiteBlog(http.Controller):
- 'tag': current tag, if tag_id
- 'nav_list': a dict [year][month] for archives navigation
"""
date_begin, date_end = post.get('date_begin'), post.get('date_end')
pager_url = "/blogpost/%s" % blog_post.id
@ -187,6 +219,12 @@ class WebsiteBlog(http.Controller):
pager_end = page * self._post_comment_per_page
blog_post.website_message_ids = blog_post.website_message_ids[pager_begin:pager_end]
tag = None
if tag_id:
tag = request.registry['blog.tag'].browse(request.cr, request.uid, tag_id, context=request.context)
current_url = QueryURL('', ['blogpost'], blogpost=blog_post, tag_id=tag_id, date_begin=date_begin, date_end=date_end)
cr, uid, context = request.cr, request.uid, request.context
blog_obj = request.registry['blog.blog']
blog_ids = blog_obj.search(cr, uid, [], context=context)
@ -196,22 +234,18 @@ class WebsiteBlog(http.Controller):
tag_ids = tag_obj.search(cr, uid, [], context=context)
tags = tag_obj.browse(cr, uid, tag_ids, context=context)
MONTHS = [None, _('January'), _('February'), _('March'), _('April'),
_('May'), _('June'), _('July'), _('August'), _('September'),
_('October'), _('November'), _('December')]
values = {
'blog': blog_post.blog_id,
'blogs': blogs,
'tags': tags,
'tag': tag and request.registry['blog.tag'].browse(cr, uid, int(tag), context=context) or None,
'tag': tag,
'blog_post': blog_post,
'main_object': blog_post,
'pager': pager,
'nav_list': self.nav_list(),
'enable_editor': enable_editor,
'date': date,
'date_name': date and "%s %s" % (MONTHS[int(date.split("-")[1])], date.split("-")[0]) or None
'date': date_begin,
'current_url': current_url
}
return request.website.render("website_blog.blog_post_complete", values)

View File

@ -43,45 +43,45 @@
<section data-snippet-id="title" class="container">
<div class="row">
<div class="col-md-12 text-center">
<h1>Latest Blogs</h1>
<h1>Latest Posts</h1>
</div>
</div>
</section>
<section class="container">
<div class="row">
<t t-set="count" t-value="0"/>
<t t-foreach="blogs" t-as="blog">
<t t-foreach="posts" t-as="post">
<div class="col-md-4">
<h4>
<a t-attf-href="/blogpost/#{ slug(blog) }?#{ tag and 'tag=%s' % tag.id or '' }#{tag and date and '&amp;' or ''}#{ date and 'date=%s' % date or ''}" t-field="blog.name"></a>
<span t-if="not blog.website_published" class="text-warning">
<a t-attf-href="#{current_url('', ['blogpost'], blogpost=post)}" t-field="post.name"></a>
<span t-if="not post.website_published" class="text-warning">
&amp;nbsp;
<span class="fa fa-warning" title="Not published"/>
</span>
</h4>
<div class="text-muted">
<span class="fa fa-calendar"> <span t-field="blog.create_date"/> &amp;nbsp;</span>
<span class="fa fa-calendar"> <span t-field="post.create_date"/> &amp;nbsp;</span>
<span class="fa fa-folder-open"> In
<a t-attf-href="/blog/#{ slug(blog.blog_id) }">
<span t-field="blog.blog_id"/>
<a t-attf-href="#{current_url(blog=post.blog_id)}">
<span t-field="post.blog_id"/>
</a> &amp;nbsp;
</span>
</div>
<div class="text-muted fa fa-tags">
<span t-field="blog.website_meta_keywords"/>
<span t-if="editable and not blog.website_meta_keywords" class="label label-danger">
<span t-field="post.website_meta_keywords"/>
<span t-if="editable and not post.website_meta_keywords" class="label label-danger">
No keywords defined!
</span>
</div>
<div class="text-muted" t-if="len(blog.message_ids) &gt; 0">
<div class="text-muted" t-if="len(post.message_ids) &gt; 0">
<span class="fa fa-comment-o">
<a t-attf-href="/blogpost/#{ slug(blog) }/?#{ tag and 'tag=%s' % tag.id or '' }#{tag and date and '&amp;' or ''}#{ date and 'date=%s' % date or ''}#comments">
<t t-if="len(blog.message_ids) &lt;= 1" ><t t-esc="len(blog.message_ids)"/> comment</t>
<t t-if="len(blog.message_ids) > 1"><t t-esc="len(blog.message_ids)"/> comments</t>
<a t-attf-href="#{current_url('', ['blogpost'], blogpost=post)}#comments">
<t t-if="len(post.message_ids) &lt;= 1" ><t t-esc="len(post.message_ids)"/> comment</t>
<t t-if="len(post.message_ids) > 1"><t t-esc="len(post.message_ids)"/> comments</t>
</a>
</span>
</div>
<div class="text-muted mb16" t-field="blog.website_meta_description"/>
<div class="text-muted mb16" t-field="post.website_meta_description"/>
</div>
<t t-set="count" t-value="count+1"/>
<div class="clearfix" t-if="(count % 3) == 0"/>
@ -110,12 +110,12 @@
<t t-foreach="blog_posts" t-as="blog_post">
<div t-att-data-publish="blog_post.website_published and 'on' or 'off'">
<h2 class="text-center">
<a t-attf-href="/blogpost/#{ slug(blog_post) }/?#{ tag and 'tag=%s' % tag.id or '' }#{tag and date and '&amp;' or ''}#{ date and 'date=%s' % date or ''}" t-field="blog_post.name"></a>
<a t-attf-href="#{current_url('', ['blogpost'], blogpost=blog_post, blog=None, tag_id=tag and tag.id or None, tag=None)}" t-field="blog_post.name"></a>
</h2>
<p class="post-meta text-muted text-center" name='blog_post_data'>
<span class="fa fa-calendar oe_date"> <span t-field="blog_post.create_date"/> &amp;nbsp;</span>
<span t-if="len(blog_post.message_ids) &gt; 0" class="fa fa-comment-o">
<a t-attf-href="/blogpost/#{ slug(blog_post) }/?#{ tag and 'tag=%s' % tag.id or '' }#{tag and date and '&amp;' or ''}#{ date and 'date=%s' % date or ''}#comments">
<a t-attf-href="#{current_url('', ['blogpost'], blogpost=blog_post, blog=None)}#comments">
<t t-if="len(blog_post.message_ids) &lt;= 1" ><t t-esc="len(blog_post.message_ids)"/> comment</t>
<t t-if="len(blog_post.message_ids) > 1"><t t-esc="len(blog_post.message_ids)"/> comments</t>
</a>
@ -145,7 +145,7 @@
<p class="post-meta text-muted text-center" t-if="len(blog_post.tag_ids)">
<span class="fa fa-tags"/>
<t t-foreach="blog_post.tag_ids" t-as="tag">
<a t-attf-href="/blog/#{ slug(blog) }/tag/#{ slug(tag) }" t-esc="tag.name"/> &amp;nbsp;
<a t-attf-href="#{current_url(tag_id=tag.id)}" t-esc="tag.name"/> &amp;nbsp;
</t>
</p>
</xpath>
@ -309,7 +309,7 @@
<ul class="nav nav-pills nav-stacked">
<t t-foreach="tags" t-as="tag_id">
<li t-att-class="tag and tag_id.id == tag.id and 'active' or None" style="display: inline-block;">
<a t-attf-href="/blog/#{ slug(blog) }/tag/#{ slug(tag_id) }"><span t-field="tag_id.name"/></a>
<a t-attf-href="#{current_url(tag_id=tag_id)}"><span t-field="tag_id.name"/></a>
</li>
</t>
</ul>
@ -325,8 +325,8 @@
<h4>Archives</h4>
<ul class="nav nav-pills nav-stacked">
<t t-foreach="nav_list" t-as="months">
<li t-att-class="months['date'] == date and 'active' or None">
<a t-ignore="True" t-attf-href="/blog/#{ slug(blog) }/#{ tag and 'tag/%s/' % slug(tag) or '' }date/#{ months['date'] }"><t t-esc="months['create_date']"/><span class="pull-right badge" t-esc="months['create_date_count']"/></a>
<li t-att-class="months['date_begin'] == date and 'active' or None">
<a t-ignore="True" t-attf-href="#{current_url(tag_id=tag, date_begin=months['date_begin'], date_end=months['date_end'])}"><t t-esc="months['create_date']"/><span class="pull-right badge" t-esc="months['create_date_count']"/></a>
</li>
</t>
</ul>