diff options
| author | Thibault Delavallée <tde@openerp.com> | 2016-08-10 12:38:16 +0200 |
|---|---|---|
| committer | Thibault Delavallée <tde@openerp.com> | 2016-08-10 15:48:09 +0200 |
| commit | 78797a4b174b95fece9e9c01d6a657aad3acad16 (patch) | |
| tree | 520655dc9920b96fcc0bf8c0ef70ec8a0c72a241 | |
| parent | 3f2539bc604fb362548a89f504cf8300a3570956 (diff) | |
[MIG] website_forum: migrate remaining message_post and update code accordingly
| -rw-r--r-- | addons/website_forum/controllers/main.py | 4 | ||||
| -rw-r--r-- | addons/website_forum/models/forum.py | 30 | ||||
| -rw-r--r-- | addons/website_forum/tests/test_forum.py | 13 |
3 files changed, 19 insertions, 28 deletions
diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py index 6ff3d1404f9..7ad7151938d 100644 --- a/addons/website_forum/controllers/main.py +++ b/addons/website_forum/controllers/main.py @@ -731,8 +731,8 @@ class WebsiteForum(http.Controller): @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/convert_to_comment', type='http', auth="user", methods=['POST'], website=True) def convert_answer_to_comment(self, forum, post, **kwarg): question = post.parent_id - new_msg_id = post.convert_answer_to_comment()[0] - if not new_msg_id: + new_msg = post.convert_answer_to_comment() + if not new_msg: return werkzeug.utils.redirect("/forum/%s" % slug(forum)) return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question))) diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py index d12b7d77626..1f6013df78c 100644 --- a/addons/website_forum/models/forum.py +++ b/addons/website_forum/models/forum.py @@ -680,13 +680,14 @@ class Post(models.Model): Vote.create({'post_id': post_id, 'vote': new_vote}) return {'vote_count': self.vote_count, 'user_vote': new_vote} - @api.one + @api.multi def convert_answer_to_comment(self): """ Tools to convert an answer (forum.post) to a comment (mail.message). The original post is unlinked and a new comment is posted on the question using the post create_uid as the comment's author. """ + self.ensure_one() if not self.parent_id: - return False + return self.env['mail.message'] # karma-based action check: use the post field that computed own/all value if not self.can_comment_convert: @@ -701,7 +702,7 @@ class Post(models.Model): 'subtype': 'mail.mt_comment', 'date': self.create_date, } - new_message = self.browse(question.id).with_context(mail_create_nosubscribe=True).message_post(**values) + new_message = question.with_context(mail_create_nosubscribe=True).message_post(**values) # unlink the original answer, using SUPERUSER_ID to avoid karma issues self.sudo().unlink() @@ -786,23 +787,14 @@ class Post(models.Model): res[category]['button_access'] = {'url': access_action, 'title': '%s %s' % (_('View'), self.post_type)} return res - @api.cr_uid_ids_context - def message_post(self, cr, uid, thread_id, message_type='notification', subtype=None, context=None, **kwargs): - if thread_id and message_type == 'comment': # user comments have a restriction on karma - if isinstance(thread_id, (list, tuple)): - post_id = thread_id[0] - else: - post_id = thread_id - post = self.browse(cr, uid, post_id, context=context) - # TDE FIXME: trigger browse because otherwise the function field is not compted - check with RCO - tmp1, tmp2 = post.karma_comment, post.can_comment - user = self.pool['res.users'].browse(cr, uid, uid) - tmp3 = user.karma - # TDE END FIXME - if not post.can_comment: + @api.multi + def message_post(self, message_type='notification', subtype=None, **kwargs): + if self.ids and message_type == 'comment': # user comments have a restriction on karma + self.ensure_one() + if not self.can_comment: raise KarmaError('Not enough karma to comment') - kwargs['record_name'] = kwargs.get('record_name') or post.parent_id and post.parent_id.name - return super(Post, self).message_post(cr, uid, thread_id, message_type=message_type, subtype=subtype, context=context, **kwargs) + kwargs['record_name'] = kwargs.get('record_name') or self.parent_id and self.parent_id.name + return super(Post, self).message_post(message_type=message_type, subtype=subtype, **kwargs) class PostReason(models.Model): diff --git a/addons/website_forum/tests/test_forum.py b/addons/website_forum/tests/test_forum.py index 344122abe2f..be0714a4c14 100644 --- a/addons/website_forum/tests/test_forum.py +++ b/addons/website_forum/tests/test_forum.py @@ -217,8 +217,8 @@ class TestForum(TestForumCommon): Post = self.env['forum.post'] # converting a question does nothing - msg_ids = self.post.sudo(self.user_portal).convert_answer_to_comment() - self.assertEqual(msg_ids[0], False, 'website_forum: question to comment conversion failed') + new_msg = self.post.sudo(self.user_portal).convert_answer_to_comment() + self.assertEqual(new_msg.id, False, 'website_forum: question to comment conversion failed') self.assertEqual(Post.search([('name', '=', 'TestQuestion')])[0].forum_id.name, 'TestForum', 'website_forum: question to comment conversion failed') with self.assertRaises(KarmaError): @@ -227,11 +227,10 @@ class TestForum(TestForumCommon): def test_convert_answer_to_comment(self): self.user_portal.karma = KARMA['com_conv_all'] post_author = self.answer.create_uid.partner_id - msg_ids = self.answer.sudo(self.user_portal).convert_answer_to_comment() - self.assertEqual(len(msg_ids), 1, 'website_forum: wrong answer to comment conversion') - msg = self.env['mail.message'].browse(msg_ids[0]) - self.assertEqual(msg.author_id, post_author, 'website_forum: wrong answer to comment conversion') - self.assertIn('I am an anteater', msg.body, 'website_forum: wrong answer to comment conversion') + new_msg = self.answer.sudo(self.user_portal).convert_answer_to_comment() + self.assertEqual(len(new_msg), 1, 'website_forum: wrong answer to comment conversion') + self.assertEqual(new_msg.author_id, post_author, 'website_forum: wrong answer to comment conversion') + self.assertIn('I am an anteater', new_msg.body, 'website_forum: wrong answer to comment conversion') def test_edit_post_crash(self): with self.assertRaises(KarmaError): |
