1 # -*- coding: utf-8 -*-
3 from datetime import datetime
7 from werkzeug.exceptions import Forbidden
10 from openerp import api, fields, models
11 from openerp import modules
12 from openerp import tools
13 from openerp import SUPERUSER_ID
14 from openerp.addons.website.models.website import slug
15 from openerp.exceptions import Warning
17 _logger = logging.getLogger(__name__)
20 class KarmaError(Forbidden):
21 """ Karma-related error, used for forum and posts. """
25 class Forum(models.Model):
27 _description = 'Forum'
28 _inherit = ['mail.thread', 'website.seo.metadata']
31 """ Add forum uuid for user email validation.
33 TDE TODO: move me somewhere else, auto_init ? """
34 forum_uuids = self.pool['ir.config_parameter'].search(cr, SUPERUSER_ID, [('key', '=', 'website_forum.uuid')])
36 self.pool['ir.config_parameter'].set_param(cr, SUPERUSER_ID, 'website_forum.uuid', str(uuid.uuid4()), ['base.group_system'])
39 def _get_default_faq(self):
40 fname = modules.get_module_resource('website_forum', 'data', 'forum_default_faq.html')
41 with open(fname, 'r') as f:
46 name = fields.Char('Forum Name', required=True, translate=True)
47 faq = fields.Html('Guidelines', default=_get_default_faq, translate=True)
48 description = fields.Html(
50 default='<p> This community is for professionals and enthusiasts of our products and services.'
51 'Share and discuss the best content and new marketing ideas,'
52 'build your professional profile and become a better marketer together.</p>')
53 default_order = fields.Selection([
54 ('create_date desc', 'Newest'),
55 ('write_date desc', 'Last Updated'),
56 ('vote_count desc', 'Most Voted'),
57 ('relevancy desc', 'Relevancy'),
58 ('child_count desc', 'Answered')],
59 string='Default Order', required=True, default='write_date desc')
60 relevancy_post_vote = fields.Float('First Relevancy Parameter', default=0.8)
61 relevancy_time_decay = fields.Float('Second Relevancy Parameter', default=1.8)
62 default_post_type = fields.Selection([
63 ('question', 'Question'),
64 ('discussion', 'Discussion'),
66 string='Default Post', required=True, default='question')
67 allow_question = fields.Boolean('Questions', help="Users can answer only once per question. Contributors can edit answers and mark the right ones.", default=True)
68 allow_discussion = fields.Boolean('Discussions', default=True)
69 allow_link = fields.Boolean('Links', help="When clicking on the post, it redirects to an external link", default=True)
71 karma_gen_question_new = fields.Integer(string='Asking a question', default=2)
72 karma_gen_question_upvote = fields.Integer(string='Question upvoted', default=5)
73 karma_gen_question_downvote = fields.Integer(string='Question downvoted', default=-2)
74 karma_gen_answer_upvote = fields.Integer(string='Answer upvoted', default=10)
75 karma_gen_answer_downvote = fields.Integer(string='Answer downvoted', default=-2)
76 karma_gen_answer_accept = fields.Integer(string='Accepting an answer', default=2)
77 karma_gen_answer_accepted = fields.Integer(string='Answer accepted', default=15)
78 karma_gen_answer_flagged = fields.Integer(string='Answer flagged', default=-100)
80 karma_ask = fields.Integer(string='Ask a new question', default=3)
81 karma_answer = fields.Integer(string='Answer a question', default=3)
82 karma_edit_own = fields.Integer(string='Edit its own posts', default=1)
83 karma_edit_all = fields.Integer(string='Edit all posts', default=300)
84 karma_close_own = fields.Integer(string='Close its own posts', default=100)
85 karma_close_all = fields.Integer(string='Close all posts', default=500)
86 karma_unlink_own = fields.Integer(string='Delete its own posts', default=500)
87 karma_unlink_all = fields.Integer(string='Delete all posts', default=1000)
88 karma_upvote = fields.Integer(string='Upvote', default=5)
89 karma_downvote = fields.Integer(string='Downvote', default=50)
90 karma_answer_accept_own = fields.Integer(string='Accept an answer on its own questions', default=20)
91 karma_answer_accept_all = fields.Integer(string='Accept an answers to all questions', default=500)
92 karma_editor_link_files = fields.Integer(string='Linking files (Editor)', default=20)
93 karma_editor_clickable_link = fields.Integer(string='Add clickable links (Editor)', default=20)
94 karma_comment_own = fields.Integer(string='Comment its own posts', default=1)
95 karma_comment_all = fields.Integer(string='Comment all posts', default=1)
96 karma_comment_convert_own = fields.Integer(string='Convert its own answers to comments and vice versa', default=50)
97 karma_comment_convert_all = fields.Integer(string='Convert all answers to answers and vice versa', default=500)
98 karma_comment_unlink_own = fields.Integer(string='Unlink its own comments', default=50)
99 karma_comment_unlink_all = fields.Integer(string='Unlinnk all comments', default=500)
100 karma_retag = fields.Integer(string='Change question tags', default=75)
101 karma_flag = fields.Integer(string='Flag a post as offensive', default=500)
102 karma_dofollow = fields.Integer(string='Disabled links', help='If the author has not enough karma, a nofollow attribute is added to links', default=500)
105 def create(self, values):
106 return super(Forum, self.with_context(mail_create_nolog=True)).create(values)
109 def _tag_to_write_vals(self, tags=''):
110 User = self.env['res.users']
111 Tag = self.env['forum.tag']
114 for tag in filter(None, tags.split(',')):
115 if tag.startswith('_'): # it's a new tag
116 # check that not arleady created meanwhile or maybe excluded by the limit on the search
117 tag_ids = Tag.search([('name', '=', tag[1:])])
119 existing_keep.append(int(tag_ids[0]))
121 # check if user have Karma needed to create need tag
122 user = User.sudo().browse(self._uid)
123 if user.exists() and user.karma >= self.karma_retag:
124 post_tags.append((0, 0, {'name': tag[1:], 'forum_id': self.id}))
126 existing_keep.append(int(tag))
127 post_tags.insert(0, [6, 0, existing_keep])
131 class Post(models.Model):
133 _description = 'Forum Post'
134 _inherit = ['mail.thread', 'website.seo.metadata']
135 _order = "is_correct DESC, vote_count DESC, write_date DESC"
137 name = fields.Char('Title')
138 forum_id = fields.Many2one('forum.forum', string='Forum', required=True)
139 content = fields.Html('Content')
140 content_link = fields.Char('URL', help="URL of Link Articles")
141 tag_ids = fields.Many2many('forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', string='Tags')
142 state = fields.Selection([('active', 'Active'), ('close', 'Close'), ('offensive', 'Offensive')], string='Status', default='active')
143 views = fields.Integer('Number of Views', default=0)
144 active = fields.Boolean('Active', default=True)
145 post_type = fields.Selection([
146 ('question', 'Question'),
148 ('discussion', 'Discussion')],
149 string='Type', default='question')
150 website_message_ids = fields.One2many(
151 'mail.message', 'res_id',
152 domain=lambda self: ['&', ('model', '=', self._name), ('type', 'in', ['email', 'comment'])],
153 string='Post Messages', help="Comments on forum post",
156 create_date = fields.Datetime('Asked on', select=True, readonly=True)
157 create_uid = fields.Many2one('res.users', string='Created by', select=True, readonly=True)
158 write_date = fields.Datetime('Update on', select=True, readonly=True)
159 write_uid = fields.Many2one('res.users', string='Updated by', select=True, readonly=True)
160 relevancy = fields.Float('Relevancy', compute="_compute_relevancy", store=True)
163 @api.depends('vote_count', 'forum_id.relevancy_post_vote', 'forum_id.relevancy_time_decay')
164 def _compute_relevancy(self):
165 days = (datetime.today() - datetime.strptime(self.create_date, tools.DEFAULT_SERVER_DATETIME_FORMAT)).days
166 self.relevancy = math.copysign(1, self.vote_count) * (abs(self.vote_count - 1) ** self.forum_id.relevancy_post_vote / (days + 2) ** self.forum_id.relevancy_time_decay)
169 vote_ids = fields.One2many('forum.post.vote', 'post_id', string='Votes')
170 user_vote = fields.Integer('My Vote', compute='_get_user_vote')
171 vote_count = fields.Integer('Votes', compute='_get_vote_count', store=True)
174 def _get_user_vote(self):
175 votes = self.env['forum.post.vote'].search_read([('post_id', 'in', self._ids), ('user_id', '=', self._uid)], ['vote', 'post_id'])
176 mapped_vote = dict([(v['post_id'][0], v['vote']) for v in votes])
178 vote.user_vote = mapped_vote.get(vote.id, 0)
181 @api.depends('vote_ids')
182 def _get_vote_count(self):
183 read_group_res = self.env['forum.post.vote'].read_group([('post_id', 'in', self._ids)], ['post_id', 'vote'], ['post_id', 'vote'], lazy=False)
184 result = dict.fromkeys(self._ids, 0)
185 for data in read_group_res:
186 result[data['post_id'][0]] += data['__count'] * int(data['vote'])
188 post.vote_count = result[post.id]
191 favourite_ids = fields.Many2many('res.users', string='Favourite')
192 user_favourite = fields.Boolean('Is Favourite', compute='_get_user_favourite')
193 favourite_count = fields.Integer('Favorite Count', compute='_get_favorite_count', store=True)
196 def _get_user_favourite(self):
197 self.user_favourite = self._uid in self.favourite_ids.ids
200 @api.depends('favourite_ids')
201 def _get_favorite_count(self):
202 self.favourite_count = len(self.favourite_ids)
205 is_correct = fields.Boolean('Correct', help='Correct answer or answer accepted')
206 parent_id = fields.Many2one('forum.post', string='Question', ondelete='cascade')
207 self_reply = fields.Boolean('Reply to own question', compute='_is_self_reply', store=True)
208 child_ids = fields.One2many('forum.post', 'parent_id', string='Answers')
209 child_count = fields.Integer('Number of answers', compute='_get_child_count', store=True)
210 uid_has_answered = fields.Boolean('Has Answered', compute='_get_uid_has_answered')
211 has_validated_answer = fields.Boolean('Is answered', compute='_get_has_validated_answer', store=True)
214 @api.depends('create_uid', 'parent_id')
215 def _is_self_reply(self):
216 self_replies = self.search([('parent_id.create_uid', '=', self._uid)])
218 post.is_self_reply = post in self_replies
221 @api.depends('child_ids', 'website_message_ids')
222 def _get_child_count(self):
224 total = len(node.website_message_ids) + len(node.child_ids)
225 for child in node.child_ids:
226 total += process(child)
228 self.child_count = process(self)
231 def _get_uid_has_answered(self):
232 self.uid_has_answered = any(answer.create_uid.id == self._uid for answer in self.child_ids)
235 @api.depends('child_ids', 'is_correct')
236 def _get_has_validated_answer(self):
237 correct_posts = [ans.parent_id for ans in self.search([('parent_id', 'in', self._ids), ('is_correct', '=', True)])]
239 post.is_correct = post in correct_posts
242 closed_reason_id = fields.Many2one('forum.post.reason', string='Reason')
243 closed_uid = fields.Many2one('res.users', string='Closed by', select=1)
244 closed_date = fields.Datetime('Closed on', readonly=True)
245 # karma calculation and access
246 karma_accept = fields.Integer('Convert comment to answer', compute='_get_post_karma_rights')
247 karma_edit = fields.Integer('Karma to edit', compute='_get_post_karma_rights')
248 karma_close = fields.Integer('Karma to close', compute='_get_post_karma_rights')
249 karma_unlink = fields.Integer('Karma to unlink', compute='_get_post_karma_rights')
250 karma_comment = fields.Integer('Karma to comment', compute='_get_post_karma_rights')
251 karma_comment_convert = fields.Integer('Karma to convert comment to answer', compute='_get_post_karma_rights')
252 can_ask = fields.Boolean('Can Ask', compute='_get_post_karma_rights')
253 can_answer = fields.Boolean('Can Answer', compute='_get_post_karma_rights')
254 can_accept = fields.Boolean('Can Accept', compute='_get_post_karma_rights')
255 can_edit = fields.Boolean('Can Edit', compute='_get_post_karma_rights')
256 can_close = fields.Boolean('Can Close', compute='_get_post_karma_rights')
257 can_unlink = fields.Boolean('Can Unlink', compute='_get_post_karma_rights')
258 can_upvote = fields.Boolean('Can Upvote', compute='_get_post_karma_rights')
259 can_downvote = fields.Boolean('Can Downvote', compute='_get_post_karma_rights')
260 can_comment = fields.Boolean('Can Comment', compute='_get_post_karma_rights')
261 can_comment_convert = fields.Boolean('Can Convert to Comment', compute='_get_post_karma_rights')
264 def _get_post_karma_rights(self):
267 self.karma_accept = self.parent_id and self.parent_id.create_uid.id == self._uid and self.forum_id.karma_answer_accept_own or self.forum_id.karma_answer_accept_all
268 self.karma_edit = self.create_uid.id == self._uid and self.forum_id.karma_edit_own or self.forum_id.karma_edit_all
269 self.karma_close = self.create_uid.id == self._uid and self.forum_id.karma_close_own or self.forum_id.karma_close_all
270 self.karma_unlink = self.create_uid.id == self._uid and self.forum_id.karma_unlink_own or self.forum_id.karma_unlink_all
271 self.karma_comment = self.create_uid.id == self._uid and self.forum_id.karma_comment_own or self.forum_id.karma_comment_all
272 self.karma_comment_convert = self.create_uid.id == self._uid and self.forum_id.karma_comment_convert_own or self.forum_id.karma_comment_convert_all
274 self.can_ask = user.karma >= self.forum_id.karma_ask
275 self.can_answer = user.karma >= self.forum_id.karma_answer
276 self.can_accept = user.karma >= self.karma_accept
277 self.can_edit = user.karma >= self.karma_edit
278 self.can_close = user.karma >= self.karma_close
279 self.can_unlink = user.karma >= self.karma_unlink
280 self.can_upvote = user.karma >= self.forum_id.karma_upvote
281 self.can_downvote = user.karma >= self.forum_id.karma_downvote
282 self.can_comment = user.karma >= self.karma_comment
283 self.can_comment_convert = user.karma >= self.karma_comment_convert
286 def create(self, vals):
287 post = super(Post, self.with_context(mail_create_nolog=True)).create(vals)
288 # deleted or closed questions
289 if post.parent_id and (post.parent_id.state == 'close' or post.parent_id.active is False):
290 raise Warning(_('Posting answer on a [Deleted] or [Closed] question is not possible'))
292 if not post.parent_id and not post.can_ask:
293 raise KarmaError('Not enough karma to create a new question')
294 elif post.parent_id and not post.can_answer:
295 raise KarmaError('Not enough karma to answer to a question')
296 # messaging and chatter
297 base_url = self.env['ir.config_parameter'].get_param('web.base.url')
300 '<p>A new answer for <i>%s</i> has been posted. <a href="%s/forum/%s/question/%s">Click here to access the post.</a></p>' %
301 (post.parent_id.name, base_url, slug(post.parent_id.forum_id), slug(post.parent_id))
303 post.parent_id.message_post(subject=_('Re: %s') % post.parent_id.name, body=body, subtype='website_forum.mt_answer_new')
306 '<p>A new question <i>%s</i> has been asked on %s. <a href="%s/forum/%s/question/%s">Click here to access the question.</a></p>' %
307 (post.name, post.forum_id.name, base_url, slug(post.forum_id), slug(post))
309 post.message_post(subject=post.name, body=body, subtype='website_forum.mt_question_new')
310 self.env.user.sudo().add_karma(post.forum_id.karma_gen_question_new)
314 def write(self, vals):
316 if vals['state'] in ['active', 'close'] and any(not post.can_close for post in self):
317 raise KarmaError('Not enough karma to close or reopen a post.')
319 if any(not post.can_unlink for post in self):
320 raise KarmaError('Not enough karma to delete or reactivate a post')
321 if 'is_correct' in vals:
322 if any(not post.can_accept for post in self):
323 raise KarmaError('Not enough karma to accept or refuse an answer')
324 # update karma except for self-acceptance
325 mult = 1 if vals['is_correct'] else -1
327 if vals['is_correct'] != post.is_correct and post.create_uid.id != self._uid:
328 post.create_uid.sudo().add_karma(post.forum_id.karma_gen_answer_accepted * mult)
329 self.env.user.sudo().add_karma(post.forum_id.karma_gen_answer_accept * mult)
330 if any(key not in ['state', 'active', 'is_correct', 'closed_uid', 'closed_date', 'closed_reason_id'] for key in vals.keys()) and any(not post.can_edit for post in self):
331 raise KarmaError('Not enough karma to edit a post.')
333 res = super(Post, self).write(vals)
334 # if post content modify, notify followers
335 if 'content' in vals or 'name' in vals:
338 body, subtype = _('Answer Edited'), 'website_forum.mt_answer_edit'
339 obj_id = post.parent_id
341 body, subtype = _('Question Edited'), 'website_forum.mt_question_edit'
343 obj_id.message_post(body=body, subtype=subtype)
348 if any(post.parent_id or post.state != 'close' for post in self):
351 reason_offensive = self.env.ref('website_forum.reason_7')
352 reason_spam = self.env.ref('website_forum.reason_8')
354 if post.closed_reason_id in (reason_offensive, reason_spam):
355 _logger.info('Upvoting user <%s>, reopening spam/offensive question',
357 # TODO: in master, consider making this a tunable karma parameter
358 post.create_uid.sudo().add_karma(post.forum_id.karma_gen_question_downvote * -5)
360 self.sudo().write({'state': 'active'})
363 def close(self, reason_id):
364 if any(post.parent_id for post in self):
367 reason_offensive = self.env.ref('website_forum.reason_7').id
368 reason_spam = self.env.ref('website_forum.reason_8').id
369 if reason_id in (reason_offensive, reason_spam):
371 _logger.info('Downvoting user <%s> for posting spam/offensive contents',
373 # TODO: in master, consider making this a tunable karma parameter
374 post.create_uid.sudo().add_karma(post.forum_id.karma_gen_question_downvote * 5)
378 'closed_uid': self._uid,
379 'closed_date': datetime.today().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT),
380 'closed_reason_id': reason_id,
386 if any(not post.can_unlink for post in self):
387 raise KarmaError('Not enough karma to unlink a post')
388 # if unlinking an answer with accepted answer: remove provided karma
391 post.create_uid.sudo().add_karma(post.forum_id.karma_gen_answer_accepted * -1)
392 self.env.user.sudo().add_karma(post.forum_id.karma_gen_answer_accepted * -1)
393 return super(Post, self).unlink()
396 def vote(self, upvote=True):
397 Vote = self.env['forum.post.vote']
398 vote_ids = Vote.search([('post_id', 'in', self._ids), ('user_id', '=', self._uid)])
399 new_vote = '1' if upvote else '-1'
400 voted_forum_ids = set()
402 for vote in vote_ids:
404 new_vote = '0' if vote.vote == '-1' else '1'
406 new_vote = '0' if vote.vote == '1' else '-1'
408 voted_forum_ids.add(vote.post_id.id)
409 for post_id in set(self._ids) - voted_forum_ids:
410 for post_id in self._ids:
411 Vote.create({'post_id': post_id, 'vote': new_vote})
412 return {'vote_count': self.vote_count, 'user_vote': new_vote}
415 def convert_answer_to_comment(self):
416 """ Tools to convert an answer (forum.post) to a comment (mail.message).
417 The original post is unlinked and a new comment is posted on the question
418 using the post create_uid as the comment's author. """
419 if not self.parent_id:
422 # karma-based action check: use the post field that computed own/all value
423 if not self.can_comment_convert:
424 raise KarmaError('Not enough karma to convert an answer to a comment')
427 question = self.parent_id
429 'author_id': self.create_uid.partner_id.id,
430 'body': tools.html2plaintext(self.content),
432 'subtype': 'mail.mt_comment',
433 'date': self.create_date,
435 new_message = self.browse(question.id).with_context(mail_create_nosubcribe=True).message_post(**values)
437 # unlink the original answer, using SUPERUSER_ID to avoid karma issues
443 def convert_comment_to_answer(self, message_id, default=None):
444 """ Tool to convert a comment (mail.message) into an answer (forum.post).
445 The original comment is unlinked and a new answer from the comment's author
446 is created. Nothing is done if the comment's author already answered the
448 comment = self.env['mail.message'].sudo().browse(message_id)
449 post = self.browse(comment.res_id)
450 if not comment.author_id or not comment.author_id.user_ids: # only comment posted by users can be converted
453 # karma-based action check: must check the message's author to know if own / all
454 karma_convert = comment.author_id.id == self.env.user.partner_id.id and post.forum_id.karma_comment_convert_own or post.forum_id.karma_comment_convert_all
455 can_convert = self.env.user.karma >= karma_convert
457 raise KarmaError('Not enough karma to convert a comment to an answer')
459 # check the message's author has not already an answer
460 question = post.parent_id if post.parent_id else post
461 post_create_uid = comment.author_id.user_ids[0]
462 if any(answer.create_uid.id == post_create_uid.id for answer in question.child_ids):
465 # create the new post
467 'forum_id': question.forum_id.id,
468 'content': comment.body,
469 'parent_id': question.id,
471 # done with the author user to have create_uid correctly set
472 new_post = self.sudo(post_create_uid.id).create(post_values)
480 def unlink_comment(self, message_id):
482 comment = self.env['mail.message'].sudo().browse(message_id)
483 if not comment.model == 'forum.post' or not comment.res_id == self.id:
485 # karma-based action check: must check the message's author to know if own or all
486 karma_unlink = comment.author_id.id == user.partner_id.id and self.forum_id.karma_comment_unlink_own or self.forum_id.karma_comment_unlink_all
487 can_unlink = user.karma >= karma_unlink
489 raise KarmaError('Not enough karma to unlink a comment')
490 return comment.unlink()
493 def set_viewed(self):
494 self._cr.execute("""UPDATE forum_post SET views = views+1 WHERE id IN %s""", (self._ids,))
498 def _get_access_link(self, mail, partner):
499 post = self.browse(mail.res_id)
500 res_id = post.parent_id and "%s#answer-%s" % (post.parent_id.id, post.id) or post.id
501 return "/forum/%s/question/%s" % (post.forum_id.id, res_id)
503 @api.cr_uid_ids_context
504 def message_post(self, cr, uid, thread_id, type='notification', subtype=None, context=None, **kwargs):
505 if thread_id and type == 'comment': # user comments have a restriction on karma
506 if isinstance(thread_id, (list, tuple)):
507 post_id = thread_id[0]
510 post = self.browse(cr, uid, post_id, context=context)
511 # TDE FIXME: trigger browse because otherwise the function field is not compted - check with RCO
512 tmp1, tmp2 = post.karma_comment, post.can_comment
513 user = self.pool['res.users'].browse(cr, uid, uid)
516 if not post.can_comment:
517 raise KarmaError('Not enough karma to comment')
518 return super(Post, self).message_post(cr, uid, thread_id, type=type, subtype=subtype, context=context, **kwargs)
521 class PostReason(models.Model):
522 _name = "forum.post.reason"
523 _description = "Post Closing Reason"
526 name = fields.Char(string='Closing Reason', required=True, translate=True)
529 class Vote(models.Model):
530 _name = 'forum.post.vote'
531 _description = 'Vote'
533 post_id = fields.Many2one('forum.post', string='Post', ondelete='cascade', required=True)
534 user_id = fields.Many2one('res.users', string='User', required=True, default=lambda self: self._uid)
535 vote = fields.Selection([('1', '1'), ('-1', '-1'), ('0', '0')], string='Vote', required=True, default='1')
536 create_date = fields.Datetime('Create Date', select=True, readonly=True)
537 forum_id = fields.Many2one('forum.forum', string='Forum', related="post_id.forum_id", store=True)
538 recipient_id = fields.Many2one('res.users', string='To', related="post_id.create_uid", store=True)
540 def _get_karma_value(self, old_vote, new_vote, up_karma, down_karma):
542 '-1': {'-1': 0, '0': -1 * down_karma, '1': -1 * down_karma + up_karma},
543 '0': {'-1': 1 * down_karma, '0': 0, '1': up_karma},
544 '1': {'-1': -1 * up_karma + down_karma, '0': -1 * up_karma, '1': 0}
546 return _karma_upd[old_vote][new_vote]
549 def create(self, vals):
550 vote = super(Vote, self).create(vals)
553 if vote.user_id.id == vote.post_id.create_uid.id:
554 raise Warning('Not allowed to vote for its own post')
556 if vote.vote == '1' and not vote.post_id.can_upvote:
557 raise KarmaError('Not enough karma to upvote.')
558 elif vote.vote == '-1' and not vote.post_id.can_downvote:
559 raise KarmaError('Not enough karma to downvote.')
561 if vote.post_id.parent_id:
562 karma_value = self._get_karma_value('0', vote.vote, vote.forum_id.karma_gen_answer_upvote, vote.forum_id.karma_gen_answer_downvote)
564 karma_value = self._get_karma_value('0', vote.vote, vote.forum_id.karma_gen_question_upvote, vote.forum_id.karma_gen_question_downvote)
565 vote.recipient_id.sudo().add_karma(karma_value)
569 def write(self, values):
573 if vote.user_id.id == vote.post_id.create_uid.id:
574 raise Warning('Not allowed to vote for its own post')
576 if (values['vote'] == '1' or vote.vote == '-1' and values['vote'] == '0') and not vote.post_id.can_upvote:
577 raise KarmaError('Not enough karma to upvote.')
578 elif (values['vote'] == '-1' or vote.vote == '1' and values['vote'] == '0') and not vote.post_id.can_downvote:
579 raise KarmaError('Not enough karma to downvote.')
582 if vote.post_id.parent_id:
583 karma_value = self._get_karma_value(vote.vote, values['vote'], vote.forum_id.karma_gen_answer_upvote, vote.forum_id.karma_gen_answer_downvote)
585 karma_value = self._get_karma_value(vote.vote, values['vote'], vote.forum_id.karma_gen_question_upvote, vote.forum_id.karma_gen_question_downvote)
586 vote.recipient_id.sudo().add_karma(karma_value)
587 res = super(Vote, self).write(values)
591 class Tags(models.Model):
593 _description = "Forum Tag"
594 _inherit = ['website.seo.metadata']
596 name = fields.Char('Name', required=True)
597 create_uid = fields.Many2one('res.users', string='Created by', readonly=True)
598 forum_id = fields.Many2one('forum.forum', string='Forum', required=True)
599 post_ids = fields.Many2many('forum.post', 'forum_tag_rel', 'forum_tag_id', 'forum_id', string='Posts')
600 posts_count = fields.Integer('Number of Posts', compute='_get_posts_count', store=True)
603 @api.depends("post_ids.tag_ids")
604 def _get_posts_count(self):
606 tag.posts_count = len(tag.post_ids)