summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristophe Matthieu <chm@openerp.com>2013-11-18 11:33:29 +0100
committerChristophe Matthieu <chm@openerp.com>2013-11-18 11:33:29 +0100
commit46bcff4da64280a9bc8d4ee06d3aaf0cb0d368b0 (patch)
tree34562336f253b1bee5e99470fcac4c3a5b192958
parent3a3f7fbc1537b9f3be18bd5dec3c8ba46b8ce67d (diff)
[IMP] website_blog: replace search by date in js to search server side
bzr revid: chm@openerp.com-20131118103329-zarcgcdcyablsvtz
-rw-r--r--addons/website_blog/controllers/main.py78
-rw-r--r--addons/website_blog/static/src/js/website_blog.js23
-rw-r--r--addons/website_blog/views/website_blog_templates.xml26
3 files changed, 35 insertions, 92 deletions
diff --git a/addons/website_blog/controllers/main.py b/addons/website_blog/controllers/main.py
index d37ed48ec34..448d5ce41c2 100644
--- a/addons/website_blog/controllers/main.py
+++ b/addons/website_blog/controllers/main.py
@@ -23,9 +23,7 @@ from openerp.addons.web import http
from openerp.addons.web.http import request
from openerp.addons.website.models import website
from openerp.tools.translate import _
-from openerp.tools.safe_eval import safe_eval
-import simplejson
import werkzeug
@@ -35,16 +33,11 @@ class WebsiteBlog(http.Controller):
def nav_list(self):
blog_post_obj = request.registry['blog.post']
- nav = {}
- for group in blog_post_obj.read_group(request.cr, request.uid, [], ['name', 'create_date'],
- groupby="create_date", orderby="create_date asc", context=request.context):
- # FIXME: vietnamese month names contain spaces. Set sail for fail.
- 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)
- return nav
+ 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])
+ return groups
@website.route([
'/blog',
@@ -63,24 +56,30 @@ class WebsiteBlog(http.Controller):
)
bids = blog_obj.search(cr, uid, [], offset=(page-1)*BYPAGE, limit=BYPAGE, context=context)
blogs = blog_obj.browse(cr, uid, bids, context=context)
- return request.website.render("website_blog.blogs", {
+ return request.website.render("website_blog.latest_blogs", {
'blogs': blogs,
'pager': pager
})
@website.route([
+ '/blog',
+ '/blog/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>/',
+ '/blog/cat/<model("blog.category"):category>/date/<string(length=21):date>/',
+ '/blog/tag/<model("blog.tag"):tag>/date/<string(length=21):date>/',
+ '/blog/tag/<model("blog.tag"):tag>/date/<string(length=21):date>/page/<int:page>/',
], type='http', auth="public", multilang=True)
- def blog(self, category=None, tag=None, page=1):
+ def blog(self, category=None, tag=None, page=1, date=None):
""" Prepare all values to display the blog.
:param category: category currently browsed.
:param tag: tag that is currently used to filter blog posts
:param integer page: current page of the pager. Can be the category or
post pager.
+ :param date: date currently used to filter blog posts (dateBegin_dateEnd)
:return dict values: values for the templates, containing
@@ -92,6 +91,8 @@ class WebsiteBlog(http.Controller):
- 'tag': current tag, if tag_id
- 'nav_list': a dict [year][month] for archives navigation
"""
+ BYPAGE = 10
+
cr, uid, context = request.cr, request.uid, request.context
blog_post_obj = request.registry['blog.post']
@@ -101,23 +102,29 @@ class WebsiteBlog(http.Controller):
category_ids = category_obj.search(cr, uid, [], context=context)
categories = category_obj.browse(cr, uid, category_ids, context=context)
+ pager_url = "/blog/"
+ domain = []
+
if category:
- pager_url = "/blog/cat/%s/" % category.id
- blog_posts = category.blog_post_ids
- elif tag:
- pager_url = '/blog/tag/%s/' % tag.id
- blog_posts = tag.blog_post_ids
- else:
- pager_url = '/blog/'
- blog_post_ids = blog_post_obj.search(cr, uid, [], context=context)
- blog_posts = blog_post_obj.browse(cr, uid, blog_post_ids, context=context)
+ pager_url += "cat/%s/" % category.id
+ domain += [("id", "=", [blog.id for blog in category.blog_post_ids])]
+ if tag:
+ pager_url += 'tag/%s/' % tag.id
+ domain += [("id", "=", [blog.id for blog in tag.blog_post_ids])]
+ if date:
+ pager_url += "date/%s/" % date
+ date = date.split("_")
+ domain = [("create_date", ">=", date[0]), ("create_date", "<=", date[1])]
+
+ 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=pager_url,
total=len(blog_posts),
page=page,
step=self._category_post_per_page,
- scope=7
+ scope=BYPAGE
)
pager_begin = (page - 1) * self._category_post_per_page
pager_end = page * self._category_post_per_page
@@ -189,29 +196,6 @@ class WebsiteBlog(http.Controller):
}
return request.website.render("website_blog.blog_post_complete", values)
- # TODO: Refactor (used in website_blog.js for archive links)
- # => the archive links should be generated server side
- @website.route(['/blog/nav'], type='http', auth="public", multilang=True)
- 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,
- 'fragment': request.website.render("website_blog.blog_archive_link", {
- 'blog_post': blog_post
- }),
- }
- for blog_post in request.registry['blog.post'].browse(cr, uid, blog_post_ids, context=context)
- ]
- return simplejson.dumps(blog_post_data)
-
@website.route(['/blog/<int:blog_post_id>/comment'], type='http', auth="public")
def blog_post_comment(self, blog_post_id=None, **post):
cr, uid, context = request.cr, request.uid, request.context
diff --git a/addons/website_blog/static/src/js/website_blog.js b/addons/website_blog/static/src/js/website_blog.js
deleted file mode 100644
index abacbb02781..00000000000
--- a/addons/website_blog/static/src/js/website_blog.js
+++ /dev/null
@@ -1,23 +0,0 @@
-$(document).ready(function () {
- $('.js_nav_month a:first').on('click', function (e) {
- e.preventDefault();
- var $ul = $(this).next("ul");
- if (!$ul.find('li').length) {
- // TODO: Why POST? (to pass the domain) A GET would be more appropriate...
- // This should be done server side anyway...
- $.post('/blog/nav', {'domain': $(this).data("domain")}, function (result) {
- var blog_id = +window.location.pathname.split("/").pop();
- $(JSON.parse(result)).each(function () {
- var $li = $($.parseHTML(this.fragment));
- if (blog_id == this.id) $li.addClass("active");
- if (!this.website_published) $li.find('a').css("color", "red");
- $ul.append($li);
- });
-
- });
- } else {
- $ul.toggle();
- }
- });
-
-});
diff --git a/addons/website_blog/views/website_blog_templates.xml b/addons/website_blog/views/website_blog_templates.xml
index 538cc914459..73e9fb1d0fa 100644
--- a/addons/website_blog/views/website_blog_templates.xml
+++ b/addons/website_blog/views/website_blog_templates.xml
@@ -15,7 +15,7 @@
</template>
<!-- Blog Post Summary -->
- <template id="blogs" name="All Blogs">
+ <template id="latest_blogs" name="Latest Blogs">
<t t-call="website.layout">
<div id="wrap">
<div class="oe_structure"/>
@@ -263,7 +263,6 @@
<template id="index" name="Blog Navigation">
<t t-call="website.layout">
<t t-set="head">
- <script type="text/javascript" src="/website_blog/static/src/js/website_blog.js"></script>
<link rel='stylesheet' href='/website_blog/static/src/css/website_blog.css'/>
</t>
<div id="wrap">
@@ -289,20 +288,9 @@
<section class="mt32">
<h4>Archives</h4>
<ul class="list-unstyled">
- <li t-foreach="nav_list" t-as="year">
- <t t-set="year" t-value="nav_list[year]"/>
- <t t-esc="year['name']"/>
- <ul class="css_nav_month">
- <t t-foreach="year['months']">
- <li class="js_nav_month">
- <a href="#" t-att-data-domain="__domain">
- <t t-esc="create_date"/>
- <span class="pull-right badge" t-esc="create_date_count"/>
- </a>
- <ul class="list-unstyled"/>
- </li>
- </t>
- </ul>
+ <li t-foreach="nav_list" t-as="months">
+ <a t-href="/blog/#{ category and 'cat/%s' % category.id or '' }/date/#{ months['date'] }"><t t-esc="months['create_date']"/></a>
+ <span class="pull-right badge" t-esc="months['create_date_count']"/>
</li>
</ul>
</section>
@@ -381,11 +369,5 @@
</xpath>
</template>
- <template id="blog_archive_link">
- <li>
- <a t-href="/blog/#{blog_post.id}"><t t-esc="blog_post.name"/></a>
- </li>
- </template>
-
</data>
</openerp>