[IMP] [FIX] website_blog: better follow mechanism. Now people following

a blog will receive notifications only for published posts. They
will not receive all comments of all posts anymore.
When commenting a post users will receive the answers.

Added tests to try to test this behavior.
This commit is contained in:
Amit Vora 2014-10-08 11:22:51 +05:30 committed by Thibault Delavallée
parent d3b680e7b2
commit 4bf9dc4078
7 changed files with 127 additions and 45 deletions

View File

@ -260,7 +260,7 @@ class WebsiteBlog(http.Controller):
subtype='mt_comment',
author_id=partner_ids[0],
path=post.get('path', False),
context=dict(context, mail_create_nosubcribe=True))
context=context)
return message_id
@http.route(['/blogpost/comment'], type='http', auth="public", methods=['POST'], website=True)
@ -302,14 +302,13 @@ class WebsiteBlog(http.Controller):
@http.route('/blogpost/new', type='http', auth="public", website=True)
def blog_post_create(self, blog_id, **post):
cr, uid, context = request.cr, request.uid, request.context
create_context = dict(context, mail_create_nosubscribe=True)
new_blog_post_id = request.registry['blog.post'].create(cr, uid, {
'blog_id': blog_id,
'name': _("Blog Post Title"),
'subtitle': _("Subtitle"),
'content': '',
'website_published': False,
}, context=create_context)
}, context=context)
new_blog_post = request.registry['blog.post'].browse(cr, uid, new_blog_post_id, context=context)
return werkzeug.utils.redirect("/blog/%s/post/%s?enable_editor=1" % (slug(new_blog_post.blog_id), slug(new_blog_post)))

View File

@ -28,29 +28,13 @@
<field name="state">open</field>
</record>
<!-- Post-related subtypes for messaging / Chatter -->
<record id="mt_blog_post_new" model="mail.message.subtype">
<field name="name">New Post</field>
<field name="res_model">blog.post</field>
<field name="default" eval="True"/>
<field name="description">New Post</field>
</record>
<record id="mt_blog_post_published" model="mail.message.subtype">
<field name="name">Post Published</field>
<field name="res_model">blog.post</field>
<field name="default" eval="False"/>
<field name="description">Post Published</field>
</record>
<!-- Project-related subtypes for messaging / Chatter -->
<record id="mt_blog_blog_post_new" model="mail.message.subtype">
<field name="name">New Post</field>
<!-- Blog-related subtypes for messaging / Chatter -->
<record id="mt_blog_blog_published" model="mail.message.subtype">
<field name="name">Published Post</field>
<field name="res_model">blog.blog</field>
<field name="default" eval="True"/>
<field name="parent_id" eval="ref('mt_blog_post_new')"/>
<field name="relation_field">blog_id</field>
<field name="description">Published Post</field>
</record>
</data>
</openerp>

View File

@ -7,6 +7,7 @@ import random
from openerp import tools
from openerp import SUPERUSER_ID
from openerp.addons.website.models.website import slug
from openerp.osv import osv, fields
from openerp.tools.translate import _
@ -173,6 +174,23 @@ class BlogPost(osv.Model):
}
history.create(cr, uid, res)
def _check_for_publication(self, cr, uid, ids, vals, context=None):
if vals.get('website_published'):
base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
for post in self.browse(cr, uid, ids, context=context):
post.blog_id.message_post(
body='<p>%(post_publication)s <a href="%(base_url)s/blog/%(blog_slug)s/post/%(post_slug)s">%(post_link)s</a></p>' % {
'post_publication': _('A new post %s has been published on the %s blog.') % (post.name, post.blog_id.name),
'post_link': _('Click here to access the post.'),
'base_url': base_url,
'blog_slug': slug(post.blog_id),
'post_slug': slug(post),
},
subtype='website_blog.mt_blog_blog_published',
context=context)
return True
return False
def create(self, cr, uid, vals, context=None):
if context is None:
context = {}
@ -181,6 +199,7 @@ class BlogPost(osv.Model):
create_context = dict(context, mail_create_nolog=True)
post_id = super(BlogPost, self).create(cr, uid, vals, context=create_context)
self.create_history(cr, uid, [post_id], vals, context)
self._check_for_publication(cr, uid, [post_id], vals, context=context)
return post_id
def write(self, cr, uid, ids, vals, context=None):
@ -188,6 +207,7 @@ class BlogPost(osv.Model):
vals['content'] = self._postproces_content(cr, uid, None, vals['content'], context=context)
result = super(BlogPost, self).write(cr, uid, ids, vals, context)
self.create_history(cr, uid, ids, vals, context)
self._check_for_publication(cr, uid, ids, vals, context=context)
return result
class BlogPostHistory(osv.Model):

View File

@ -1,5 +1,6 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
blog_blog_all,blog.blog,model_blog_blog,,1,0,0,0
blog_blog,blog.blog,model_blog_blog,base.group_document_user,1,1,1,1
blog_post_all,blog.post,model_blog_post,,1,0,0,0
blog_post,blog.post,model_blog_post,base.group_document_user,1,1,1,1
blog_post_history,blog.post.history,model_blog_post_history,base.group_document_user,1,0,1,0

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 blog_blog_all blog.blog model_blog_blog 1 0 0 0
3 blog_blog blog.blog model_blog_blog base.group_document_user 1 1 1 1
4 blog_post_all blog.post model_blog_post 1 0 0 0
5 blog_post blog.post model_blog_post base.group_document_user 1 1 1 1
6 blog_post_history blog.post.history model_blog_post_history base.group_document_user 1 0 1 0

View File

@ -1,24 +1,4 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Business Applications
# Copyright (c) 20123TODAY OpenERP S.A. <http://www.openerp.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import test_ui
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
from openerp.addons.website_blog.tests import test_ui
from openerp.addons.website_blog.tests import test_website_blog_flow

View File

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
from openerp.tests import common
class TestWebsiteBlogCommon(common.TransactionCase):
def setUp(self):
super(TestWebsiteBlogCommon, self).setUp()
Users = self.env['res.users']
group_blog_manager_id = self.ref('base.group_document_user')
group_employee_id = self.ref('base.group_user')
group_public_id = self.ref('base.group_public')
self.user_employee = Users.with_context({'no_reset_password': True}).create({
'name': 'Armande Employee',
'login': 'armande',
'alias_name': 'armande',
'email': 'armande.employee@example.com',
'notify_email': 'none',
'groups_id': [(6, 0, [group_employee_id])]
})
self.user_blogmanager = Users.with_context({'no_reset_password': True}).create({
'name': 'Bastien BlogManager',
'login': 'bastien',
'alias_name': 'bastien',
'email': 'bastien.blogmanager@example.com',
'notify_email': 'none',
'groups_id': [(6, 0, [group_blog_manager_id, group_employee_id])]
})
self.user_public = Users.with_context({'no_reset_password': True}).create({
'name': 'Cedric Public',
'login': 'cedric',
'alias_name': 'cedric',
'email': 'cedric.public@example.com',
'notify_email': 'none',
'groups_id': [(6, 0, [group_public_id])]
})

View File

@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
from openerp.addons.website_blog.tests.common import TestWebsiteBlogCommon
class TestWebsiteBlogFlow(TestWebsiteBlogCommon):
def test_website_blog_followers(self):
""" Test the flow of followers and notifications for blogs. Intended
flow :
- people subscribe to a blog
- when creating a new post, nobody except the creator follows it
- people subscribed to the blog does not receive comments on posts
- when published, a notification is sent to all blog followers
- if someone subscribe to the post or comment it, it become follower
and receive notification for future comments. """
# Create a new blog, subscribe the employee to the blog
test_blog = self.env['blog.blog'].sudo(self.user_blogmanager).create({
'name': 'New Blog',
'description': 'Presentation of new Odoo features'
})
self.assertIn(
self.user_blogmanager.partner_id, test_blog.message_follower_ids,
'website_blog: blog create should be in the blog followers')
test_blog.message_subscribe([self.user_employee.partner_id.id, self.user_public.partner_id.id])
# Create a new post, blog followers should not follow the post
test_blog_post = self.env['blog.post'].sudo(self.user_blogmanager).create({
'name': 'New Post',
'blog_id': test_blog.id,
})
self.assertNotIn(
self.user_employee.partner_id, test_blog_post.message_follower_ids,
'website_blog: subscribing to a blog should not subscribe to its posts')
self.assertNotIn(
self.user_public.partner_id, test_blog_post.message_follower_ids,
'website_blog: subscribing to a blog should not subscribe to its posts')
# Publish the blog
test_blog_post.write({'website_published': True})
# Check publish message has been sent to blog followers
publish_message = next((m for m in test_blog_post.blog_id.message_ids if m.subtype_id.id == self.ref('website_blog.mt_blog_blog_published')), None)
self.assertEqual(
set(publish_message.notified_partner_ids._ids),
set([self.user_employee.partner_id.id, self.user_public.partner_id.id]),
'website_blog: peuple following a blog should be notified of a published post')
# Armand posts a message -> becomes follower
test_blog_post.sudo().message_post(
body='Armande BlogUser Commented',
type='comment',
author_id=self.user_employee.partner_id.id,
subtype='mt_comment',
)
self.assertIn(
self.user_employee.partner_id, test_blog_post.message_follower_ids,
'website_blog: people commenting a post should follow it afterwards')