[MIG] website_forum: migrate remaining message_post and update code accordingly
This commit is contained in:
@@ -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)))
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user