[IMP] website_mail: blog archive, pager

bzr revid: chm@openerp.com-20130729141934-y45eofkxxjgya59u
This commit is contained in:
Christophe Matthieu
2013-07-29 16:19:34 +02:00
parent 4ebaf63987
commit 4dde39af8f
6 changed files with 90 additions and 32 deletions
+29 -12
View File
@@ -1,38 +1,50 @@
# -*- coding: utf-8 -*-
import openerp
from datetime import datetime
from openerp.addons.web.http import request
from openerp.addons.website import website
import werkzeug
from openerp.tools.translate import _
from openerp.tools.safe_eval import safe_eval
import simplejson
_months = {1:_("January"), 2:_("February"), 3:_("March"), 4:_("April"), 5:_("May"), 6:_("June"), 7:_("July"), 8:_("August"), 9:_("September"), 10:_("October"), 11:_("November"), 12:_("December")}
class website_mail(website):
@website.route(['/blog', '/blog/<int:mail_group_id>', '/blog/<int:mail_group_id>/<int:blog_id>'], type='http', auth="admin")
def blog(self, cr, uid, mail_group_id=None, blog_id=None, **post):
mail_group_obj = request.registry['mail.group']
group_obj = request.registry['mail.group']
message_obj = request.registry['mail.message']
values = {
'res_company': request.registry['res.company'].browse(cr, uid, 1),
'blog_ids': None,
'blog_id': None,
'nav_list': dict(),
'prev_date': None,
'next_date': None,
}
domain = mail_group_id and [("res_id", "=", mail_group_id)] or []
values['nav_list'] = dict()
message_ids = mail_group_obj.get_public_message_ids(cr, uid, domain=mail_group_id and [("res_id", "=", mail_group_id)] or [])
for group in message_obj.read_group(cr, uid, domain + group_obj.get_public_domain(cr, uid), ['subject', 'date'], groupby="date", orderby="create_date asc"):
year = group['date'].split(" ")[1]
if not values['nav_list'].get(year):
values['nav_list'][year] = {'name': year, 'date_count': 0, 'months': []}
values['nav_list'][year]['date_count'] += group['date_count']
values['nav_list'][year]['months'].append(group)
if post.get('date'):
ids = group_obj.get_public_message_ids(cr, uid, domain=domain + [("date", ">", post.get('date'))], order="create_date asc", limit=10)
if ids:
values['prev_date'] = message_obj.browse(cr, uid, ids.pop()).date
domain += [("date", "<=", post.get('date'))]
message_ids = group_obj.get_public_message_ids(cr, uid, domain=domain, order="create_date desc", limit=11)
if message_ids:
values['blog_ids'] = message_obj.browse(cr, uid, message_ids)
for blog in values['blog_ids']:
date = datetime.strptime(blog.date, "%Y-%m-%d %H:%M:%S")
if not values['nav_list'].get(date.year):
values['nav_list'][date.year] = {'name': date.year, 'months': {}}
if not values['nav_list'][date.year]['months'].get(date.month):
values['nav_list'][date.year]['months'][date.month] = {'name': _months[date.month], 'blog_ids': []}
values['nav_list'][date.year]['months'][date.month]['blog_ids'].append(blog)
if len(message_ids) > 10:
values['next_date'] = values['blog_ids'].pop().date
if blog_id:
values['blog_id'] = message_obj.browse(cr, uid, blog_id)
@@ -42,6 +54,11 @@ class website_mail(website):
html = self.render(cr, uid, "website_mail.index", values)
return html
@website.route(['/blog/nav'], type='http', auth="admin")
def nav(self, cr, uid, **post):
comment_ids = request.registry['mail.group'].get_public_message_ids(cr, uid, domain=safe_eval(post.get('domain')), order="create_date asc", limit=None)
return simplejson.dumps(request.registry['mail.message'].read(cr, uid, comment_ids, ['website_published', 'subject', 'res_id']))
@website.route(['/blog/publish'], type='http', auth="admin")
def publish(self, cr, uid, **post):
message_id = int(post['message_id'])
+7 -4
View File
@@ -32,9 +32,12 @@ class mail_message(osv.osv):
class mail_group(osv.Model):
_inherit = 'mail.group'
def get_public_message_ids(self, cr, uid, domain=[], context=None):
mail_group_ids = self.search(cr, uid, [('public', '=', 'public')], order="create_date", context=context)
domain += [ ("type", "in", ['comment']),
def get_public_domain(self, cr, uid, context=None):
mail_group_ids = self.search(cr, uid, [('public', '=', 'public')], context=context)
return [ ("type", "in", ['comment']),
("parent_id", "=", False),
("model", "=", 'mail.group'), ("res_id", "in", mail_group_ids)]
return self.pool.get('mail.message').search(cr, uid, domain, context=context)
def get_public_message_ids(self, cr, uid, domain=[], order="create_date desc", limit=10, offset=0, context=None):
domain += self.get_public_domain(cr, uid, context=context)
return self.pool.get('mail.message').search(cr, uid, domain, order=order, limit=limit, offset=offset, context=context)
@@ -25,3 +25,9 @@
.css_website_mail .has-error {
border-color: red;
}
.css_website_mail .css_older {
float: right;
}
.css_website_mail .css_newer {
float: left;
}
+5 -1
View File
@@ -21,4 +21,8 @@
.css_unpublish
display: none
.has-error
border-color: red
border-color: red
.css_older
float: right
.css_newer
float: left
+30 -3
View File
@@ -1,17 +1,45 @@
$(document).ready(function () {
$('.js_website_mail').on('click', '.js_publish', function (e) {
var $link = $(e.currentTarget);
e.preventDefault();
var $media = $link.parent();
$media.toggleClass('css_published');
$.post('/blog/publish/', {'message_id': $link.data('id')}, function (result) {
if (+result) $media.addClass('css_published');
else $media.removeClass('css_published');
});
return false;
});
$form = $('.js_nav_year a:first').on('click', function (e) {
e.preventDefault();
$(this).next("ul").toggle();
});
$form = $('.js_nav_month a:first').on('click', function (e) {
e.preventDefault();
var $ul = $(this).next("ul");
if (!$ul.find('li').length) {
$.post('/blog/nav', {'domain': $(this).data("domain")}, function (result) {
var result = JSON.parse(result);
var blog_id = +window.location.pathname.split("/").pop();
$(result).each(function () {
var $a = $('<a href="/blog/'+this.res_id+'/'+this.id+'"/>').text(this.subject);
var $li = $("<li/>").append($a);
if (blog_id == this.id)
$li.addClass("active");
if (!this.website_published)
$a.css("color", "red");
$ul.append($li);
});
});
} else {
$ul.toggle();
}
});
$form = $('.js_website_mail form#post');
$form.submit(function (e) {
e.preventDefault();
var error = $form.find("textarea").val().length < 3;
$form.find("textarea").toggleClass("has-error", error);
if (!error) {
@@ -20,6 +48,5 @@ $(document).ready(function () {
window.location.href = url
});
}
return false;
});
});
+13 -12
View File
@@ -34,16 +34,11 @@
<!-- TODO: check qweb iteration -->
<t t-foreach="nav_list" t-as="year">
<t t-set="year" t-value="nav_list[year]"/>
<li><t t-esc="year['name']"/>
<ul class="nav nav-list">
<t t-foreach="year['months']" t-as="month">
<t t-set="month" t-value="year['months'][month]"/>
<li><t t-esc="month['name']"/>
<ul class="nav nav-list">
<t t-foreach="month['blog_ids']" t-as="blog">
<li t-att-class="blog.id == blog_id.id and 'active' or ''"><a t-att-href="'/blog/%%s/%%s' %% (blog.res_id, blog.id)"><t t-esc="blog.subject"/> </a></li>
</t>
</ul>
<li class="js_nav_year"><a href="#"><t t-esc="year['name']"/> <small>(<t t-esc="year['date_count']"/>)</small></a>
<ul class="nav nav-list" style="display: none">
<t t-foreach="year['months']">
<li class="js_nav_month"><a href="#" t-att-data-domain="__domain"><t t-esc="date"/> <small>(<t t-esc="date_count"/>)</small></a>
<ul class="nav nav-list"/>
</li>
</t>
</ul>
@@ -95,13 +90,17 @@
<ul class="media-list">
<t t-foreach="blog_ids" t-as="blog">
<li class="media well">
<div class="media-body">
<div t-att-class="'media-body %%s' %% (blog.website_published and 'css_published' or '')">
<a href="#" title="Click to publish or unpublish this message" t-att-data-id="blog.id" class="js_publish css_button_publish" groups="group_website_mail_manager">
<i class="icon-ok-circle css_publish"></i>
<i class="icon-remove-sign css_unpublish"></i>
</a>
<h4 class="media-heading"><t t-esc="blog.subject"/></h4>
<div class="media">
<t t-raw="blog.body"/>
<small class="pull-right muted text-right">
<a t-att-href="'/blog/%%s/%%s' %% (blog.res_id, blog.id)">Read more </a>
<div><t t-esc="blog.author_id.name"/></div>
<div><t t-esc="blog.read(['author_id'])[0]['author_id'][1]"/></div>
<div><t t-esc="blog.date"/></div>
</small>
</div>
@@ -109,6 +108,8 @@
</li>
</t>
</ul>
<a t-if="prev_date" t-att-href="'?date=%%s' %% (prev_date)" class="css_newer">Newer</a>
<a t-if="next_date" t-att-href="'?date=%%s' %% (next_date)" class="css_older">Older</a>
</div>
</div>
</div>