1 # -*- coding: utf-8 -*-
3 from datetime import datetime
5 from werkzeug.exceptions import Forbidden
10 from openerp import api, tools
11 from openerp import SUPERUSER_ID
12 from openerp.addons.website.models.website import slug
13 from openerp.exceptions import Warning
14 from openerp.osv import osv, fields
15 from openerp.tools import html2plaintext
16 from openerp.tools.translate import _
18 _logger = logging.getLogger(__name__)
20 class KarmaError(Forbidden):
21 """ Karma-related error, used for forum and posts. """
25 class Forum(osv.Model):
26 """TDE TODO: set karma values for actions dynamic for a given forum"""
28 _description = 'Forums'
29 _inherit = ['mail.thread', 'website.seo.metadata']
32 """ Add forum uuid for user email validation. """
33 forum_uuids = self.pool['ir.config_parameter'].search(cr, SUPERUSER_ID, [('key', '=', 'website_forum.uuid')])
35 self.pool['ir.config_parameter'].set_param(cr, SUPERUSER_ID, 'website_forum.uuid', str(uuid.uuid4()), ['base.group_system'])
38 'name': fields.char('Name', required=True, translate=True),
39 'faq': fields.html('Guidelines'),
40 'description': fields.html('Description'),
42 'karma_gen_question_new': fields.integer('Asking a question'),
43 'karma_gen_question_upvote': fields.integer('Question upvoted'),
44 'karma_gen_question_downvote': fields.integer('Question downvoted'),
45 'karma_gen_answer_upvote': fields.integer('Answer upvoted'),
46 'karma_gen_answer_downvote': fields.integer('Answer downvoted'),
47 'karma_gen_answer_accept': fields.integer('Accepting an answer'),
48 'karma_gen_answer_accepted': fields.integer('Answer accepted'),
49 'karma_gen_answer_flagged': fields.integer('Answer flagged'),
51 'karma_ask': fields.integer('Ask a question'),
52 'karma_answer': fields.integer('Answer a question'),
53 'karma_edit_own': fields.integer('Edit its own posts'),
54 'karma_edit_all': fields.integer('Edit all posts'),
55 'karma_close_own': fields.integer('Close its own posts'),
56 'karma_close_all': fields.integer('Close all posts'),
57 'karma_unlink_own': fields.integer('Delete its own posts'),
58 'karma_unlink_all': fields.integer('Delete all posts'),
59 'karma_upvote': fields.integer('Upvote'),
60 'karma_downvote': fields.integer('Downvote'),
61 'karma_answer_accept_own': fields.integer('Accept an answer on its own questions'),
62 'karma_answer_accept_all': fields.integer('Accept an answer to all questions'),
63 'karma_editor_link_files': fields.integer('Linking files (Editor)'),
64 'karma_editor_clickable_link': fields.integer('Clickable links (Editor)'),
65 'karma_comment_own': fields.integer('Comment its own posts'),
66 'karma_comment_all': fields.integer('Comment all posts'),
67 'karma_comment_convert_own': fields.integer('Convert its own answers to comments and vice versa'),
68 'karma_comment_convert_all': fields.integer('Convert all answers to comments and vice versa'),
69 'karma_comment_unlink_own': fields.integer('Unlink its own comments'),
70 'karma_comment_unlink_all': fields.integer('Unlink all comments'),
71 'karma_retag': fields.integer('Change question tags'),
72 'karma_flag': fields.integer('Flag a post as offensive'),
75 def _get_default_faq(self, cr, uid, context=None):
76 fname = openerp.modules.get_module_resource('website_forum', 'data', 'forum_default_faq.html')
77 with open(fname, 'r') as f:
82 'description': 'This community is for professionals and enthusiasts of our products and services.',
83 'faq': _get_default_faq,
84 'karma_gen_question_new': 0, # set to null for anti spam protection
85 'karma_gen_question_upvote': 5,
86 'karma_gen_question_downvote': -2,
87 'karma_gen_answer_upvote': 10,
88 'karma_gen_answer_downvote': -2,
89 'karma_gen_answer_accept': 2,
90 'karma_gen_answer_accepted': 15,
91 'karma_gen_answer_flagged': -100,
92 'karma_ask': 3, # set to not null for anti spam protection
93 'karma_answer': 3, # set to not null for anti spam protection
95 'karma_edit_all': 300,
96 'karma_close_own': 100,
97 'karma_close_all': 500,
98 'karma_unlink_own': 500,
99 'karma_unlink_all': 1000,
101 'karma_downvote': 50,
102 'karma_answer_accept_own': 20,
103 'karma_answer_accept_all': 500,
104 'karma_editor_link_files': 20,
105 'karma_editor_clickable_link': 20,
106 'karma_comment_own': 3,
107 'karma_comment_all': 5,
108 'karma_comment_convert_own': 50,
109 'karma_comment_convert_all': 500,
110 'karma_comment_unlink_own': 50,
111 'karma_comment_unlink_all': 500,
116 def create(self, cr, uid, values, context=None):
119 create_context = dict(context, mail_create_nolog=True)
120 return super(Forum, self).create(cr, uid, values, context=create_context)
122 def _tag_to_write_vals(self, cr, uid, ids, tags='', context=None):
123 User = self.pool['res.users']
124 Tag = self.pool['forum.tag']
126 for forum in self.browse(cr, uid, ids, context=context):
129 for tag in filter(None, tags.split(',')):
130 if tag.startswith('_'): # it's a new tag
131 # check that not already created meanwhile or maybe excluded by the limit on the search
132 tag_ids = Tag.search(cr, uid, [('name', '=', tag[1:])], context=context)
134 existing_keep.append(int(tag_ids[0]))
136 # check if user have Karma needed to create need tag
137 user = User.browse(cr, uid, uid, context=context)
138 if user.exists() and user.karma >= forum.karma_retag:
139 post_tags.append((0, 0, {'name': tag[1:], 'forum_id': forum.id}))
141 existing_keep.append(int(tag))
142 post_tags.insert(0, [6, 0, existing_keep])
143 result[forum.id] = post_tags
148 class Post(osv.Model):
150 _description = 'Forum Post'
151 _inherit = ['mail.thread', 'website.seo.metadata']
152 _order = "is_correct DESC, vote_count DESC, write_date DESC"
154 def _get_user_vote(self, cr, uid, ids, field_name, arg, context):
155 res = dict.fromkeys(ids, 0)
156 vote_ids = self.pool['forum.post.vote'].search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
157 for vote in self.pool['forum.post.vote'].browse(cr, uid, vote_ids, context=context):
158 res[vote.post_id.id] = vote.vote
161 def _get_vote_count(self, cr, uid, ids, field_name, arg, context):
162 res = dict.fromkeys(ids, 0)
163 for post in self.browse(cr, uid, ids, context=context):
164 for vote in post.vote_ids:
165 res[post.id] += int(vote.vote)
168 def _get_post_from_vote(self, cr, uid, ids, context=None):
170 for vote in self.pool['forum.post.vote'].browse(cr, uid, ids, context=context):
171 result[vote.post_id.id] = True
174 def _get_user_favourite(self, cr, uid, ids, field_name, arg, context):
175 res = dict.fromkeys(ids, False)
176 for post in self.browse(cr, uid, ids, context=context):
177 if uid in [f.id for f in post.favourite_ids]:
181 def _get_favorite_count(self, cr, uid, ids, field_name, arg, context):
182 res = dict.fromkeys(ids, 0)
183 for post in self.browse(cr, uid, ids, context=context):
184 res[post.id] += len(post.favourite_ids)
187 def _get_post_from_hierarchy(self, cr, uid, ids, context=None):
189 for post in self.browse(cr, SUPERUSER_ID, ids, context=context):
191 post_ids.add(post.parent_id.id)
192 return list(post_ids)
194 def _get_child_count(self, cr, uid, ids, field_name=False, arg={}, context=None):
195 res = dict.fromkeys(ids, 0)
196 for post in self.browse(cr, uid, ids, context=context):
198 res[post.parent_id.id] = len(post.parent_id.child_ids)
200 res[post.id] = len(post.child_ids)
203 def _get_uid_answered(self, cr, uid, ids, field_name, arg, context=None):
204 res = dict.fromkeys(ids, False)
205 for post in self.browse(cr, uid, ids, context=context):
206 res[post.id] = any(answer.create_uid.id == uid for answer in post.child_ids)
209 def _get_has_validated_answer(self, cr, uid, ids, field_name, arg, context=None):
210 res = dict.fromkeys(ids, False)
211 ans_ids = self.search(cr, uid, [('parent_id', 'in', ids), ('is_correct', '=', True)], context=context)
212 for answer in self.browse(cr, uid, ans_ids, context=context):
213 res[answer.parent_id.id] = True
216 def _is_self_reply(self, cr, uid, ids, field_name, arg, context=None):
217 res = dict.fromkeys(ids, False)
218 for post in self.browse(cr, uid, ids, context=context):
219 res[post.id] = post.parent_id and post.parent_id.create_uid == post.create_uid or False
222 def _get_post_karma_rights(self, cr, uid, ids, field_name, arg, context=None):
223 user = self.pool['res.users'].browse(cr, uid, uid, context=context)
224 res = dict.fromkeys(ids, False)
225 for post in self.browse(cr, uid, ids, context=context):
227 'karma_ask': post.forum_id.karma_ask,
228 'karma_answer': post.forum_id.karma_answer,
229 'karma_accept': post.parent_id and post.parent_id.create_uid.id == uid and post.forum_id.karma_answer_accept_own or post.forum_id.karma_answer_accept_all,
230 'karma_edit': post.create_uid.id == uid and post.forum_id.karma_edit_own or post.forum_id.karma_edit_all,
231 'karma_close': post.create_uid.id == uid and post.forum_id.karma_close_own or post.forum_id.karma_close_all,
232 'karma_unlink': post.create_uid.id == uid and post.forum_id.karma_unlink_own or post.forum_id.karma_unlink_all,
233 'karma_upvote': post.forum_id.karma_upvote,
234 'karma_downvote': post.forum_id.karma_downvote,
235 'karma_comment': post.create_uid.id == uid and post.forum_id.karma_comment_own or post.forum_id.karma_comment_all,
236 'karma_comment_convert': post.create_uid.id == uid and post.forum_id.karma_comment_convert_own or post.forum_id.karma_comment_convert_all,
238 res[post.id].update({
239 'can_ask': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_ask'],
240 'can_answer': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_answer'],
241 'can_accept': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_accept'],
242 'can_edit': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_edit'],
243 'can_close': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_close'],
244 'can_unlink': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_unlink'],
245 'can_upvote': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_upvote'],
246 'can_downvote': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_downvote'],
247 'can_comment': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_comment'],
248 'can_comment_convert': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_comment_convert'],
253 'name': fields.char('Title'),
254 'forum_id': fields.many2one('forum.forum', 'Forum', required=True),
255 'content': fields.html('Content'),
256 'tag_ids': fields.many2many('forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', 'Tags'),
257 'state': fields.selection([('active', 'Active'), ('close', 'Close'), ('offensive', 'Offensive')], 'Status'),
258 'views': fields.integer('Number of Views'),
259 'active': fields.boolean('Active'),
260 'is_correct': fields.boolean('Valid Answer', help='Correct Answer or Answer on this question accepted.'),
261 'website_message_ids': fields.one2many(
262 'mail.message', 'res_id',
263 domain=lambda self: [
264 '&', ('model', '=', self._name), ('type', 'in', ['email', 'comment'])
266 string='Post Messages', help="Comments on forum post",
269 'create_date': fields.datetime('Asked on', select=True, readonly=True),
270 'create_uid': fields.many2one('res.users', 'Created by', select=True, readonly=True),
271 'write_date': fields.datetime('Update on', select=True, readonly=True),
272 'write_uid': fields.many2one('res.users', 'Updated by', select=True, readonly=True),
274 'vote_ids': fields.one2many('forum.post.vote', 'post_id', 'Votes'),
275 'user_vote': fields.function(_get_user_vote, string='My Vote', type='integer'),
276 'vote_count': fields.function(
277 _get_vote_count, string="Votes", type='integer',
279 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
280 'forum.post.vote': (_get_post_from_vote, [], 10),
283 'favourite_ids': fields.many2many('res.users', string='Favourite'),
284 'user_favourite': fields.function(_get_user_favourite, string="My Favourite", type='boolean'),
285 'favourite_count': fields.function(
286 _get_favorite_count, string='Favorite Count', type='integer',
288 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['favourite_ids'], 10),
291 'parent_id': fields.many2one('forum.post', 'Question', ondelete='cascade'),
292 'self_reply': fields.function(
293 _is_self_reply, 'Reply to own question', type='boolean',
295 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['parent_id', 'create_uid'], 10),
297 'child_ids': fields.one2many('forum.post', 'parent_id', 'Answers'),
298 'child_count': fields.function(
299 _get_child_count, string="Answers", type='integer',
301 'forum.post': (_get_post_from_hierarchy, ['parent_id', 'child_ids'], 10),
303 'uid_has_answered': fields.function(
304 _get_uid_answered, string='Has Answered', type='boolean',
306 'has_validated_answer': fields.function(
307 _get_has_validated_answer, string='Has a Validated Answered', type='boolean',
309 'forum.post': (_get_post_from_hierarchy, ['parent_id', 'child_ids', 'is_correct'], 10),
313 'closed_reason_id': fields.many2one('forum.post.reason', 'Reason'),
314 'closed_uid': fields.many2one('res.users', 'Closed by', select=1),
315 'closed_date': fields.datetime('Closed on', readonly=True),
317 'karma_ask': fields.function(_get_post_karma_rights, string='Karma to ask', type='integer', multi='_get_post_karma_rights'),
318 'karma_answer': fields.function(_get_post_karma_rights, string='Karma to answer', type='integer', multi='_get_post_karma_rights'),
319 'karma_accept': fields.function(_get_post_karma_rights, string='Karma to accept this answer', type='integer', multi='_get_post_karma_rights'),
320 'karma_edit': fields.function(_get_post_karma_rights, string='Karma to edit', type='integer', multi='_get_post_karma_rights'),
321 'karma_close': fields.function(_get_post_karma_rights, string='Karma to close', type='integer', multi='_get_post_karma_rights'),
322 'karma_unlink': fields.function(_get_post_karma_rights, string='Karma to unlink', type='integer', multi='_get_post_karma_rights'),
323 'karma_upvote': fields.function(_get_post_karma_rights, string='Karma to upvote', type='integer', multi='_get_post_karma_rights'),
324 'karma_downvote': fields.function(_get_post_karma_rights, string='Karma to downvote', type='integer', multi='_get_post_karma_rights'),
325 'karma_comment': fields.function(_get_post_karma_rights, string='Karma to comment', type='integer', multi='_get_post_karma_rights'),
326 'karma_comment_convert': fields.function(_get_post_karma_rights, string='karma to convert as a comment', type='integer', multi='_get_post_karma_rights'),
328 'can_ask': fields.function(_get_post_karma_rights, string='Can Ask', type='boolean', multi='_get_post_karma_rights'),
329 'can_answer': fields.function(_get_post_karma_rights, string='Can Answer', type='boolean', multi='_get_post_karma_rights'),
330 'can_accept': fields.function(_get_post_karma_rights, string='Can Accept', type='boolean', multi='_get_post_karma_rights'),
331 'can_edit': fields.function(_get_post_karma_rights, string='Can Edit', type='boolean', multi='_get_post_karma_rights'),
332 'can_close': fields.function(_get_post_karma_rights, string='Can Close', type='boolean', multi='_get_post_karma_rights'),
333 'can_unlink': fields.function(_get_post_karma_rights, string='Can Unlink', type='boolean', multi='_get_post_karma_rights'),
334 'can_upvote': fields.function(_get_post_karma_rights, string='Can Upvote', type='boolean', multi='_get_post_karma_rights'),
335 'can_downvote': fields.function(_get_post_karma_rights, string='Can Downvote', type='boolean', multi='_get_post_karma_rights'),
336 'can_comment': fields.function(_get_post_karma_rights, string='Can Comment', type='boolean', multi='_get_post_karma_rights'),
337 'can_comment_convert': fields.function(_get_post_karma_rights, string='Can Convert to Comment', type='boolean', multi='_get_post_karma_rights'),
345 'favourite_ids': list(),
349 def create(self, cr, uid, vals, context=None):
352 create_context = dict(context, mail_create_nolog=True)
353 post_id = super(Post, self).create(cr, uid, vals, context=create_context)
354 post = self.browse(cr, uid, post_id, context=context)
356 if not post.parent_id and not post.can_ask:
357 raise KarmaError('Not enough karma to create a new question')
358 elif post.parent_id and not post.can_answer:
359 raise KarmaError('Not enough karma to answer to a question')
360 # messaging and chatter
361 base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
364 '<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>' %
365 (post.parent_id.name, base_url, slug(post.parent_id.forum_id), slug(post.parent_id))
367 self.message_post(cr, uid, post.parent_id.id, subject=_('Re: %s') % post.parent_id.name, body=body, subtype='website_forum.mt_answer_new', context=context)
370 '<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>' %
371 (post.name, post.forum_id.name, base_url, slug(post.forum_id), slug(post))
373 self.message_post(cr, uid, post_id, subject=post.name, body=body, subtype='website_forum.mt_question_new', context=context)
374 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], post.forum_id.karma_gen_question_new, context=context)
377 def write(self, cr, uid, ids, vals, context=None):
378 posts = self.browse(cr, uid, ids, context=context)
380 if vals['state'] in ['active', 'close'] and any(not post.can_close for post in posts):
381 raise KarmaError('Not enough karma to close or reopen a post.')
383 if any(not post.can_unlink for post in posts):
384 raise KarmaError('Not enough karma to delete or reactivate a post')
385 if 'is_correct' in vals:
386 if any(not post.can_accept for post in posts):
387 raise KarmaError('Not enough karma to accept or refuse an answer')
388 # update karma except for self-acceptance
389 mult = 1 if vals['is_correct'] else -1
390 for post in self.browse(cr, uid, ids, context=context):
391 if vals['is_correct'] != post.is_correct and post.create_uid.id != uid:
392 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id], post.forum_id.karma_gen_answer_accepted * mult, context=context)
393 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], post.forum_id.karma_gen_answer_accept * mult, context=context)
394 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 posts):
395 raise KarmaError('Not enough karma to edit a post.')
397 res = super(Post, self).write(cr, uid, ids, vals, context=context)
398 # if post content modify, notify followers
399 if 'content' in vals or 'name' in vals:
402 body, subtype = _('Answer Edited'), 'website_forum.mt_answer_edit'
403 obj_id = post.parent_id.id
405 body, subtype = _('Question Edited'), 'website_forum.mt_question_edit'
407 self.message_post(cr, uid, obj_id, body=body, subtype=subtype, context=context)
411 def reopen(self, cr, uid, ids, context=None):
412 if any(post.parent_id or post.state != 'close'
413 for post in self.browse(cr, uid, ids, context=context)):
416 reason_offensive = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'website_forum.reason_7')
417 reason_spam = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'website_forum.reason_8')
418 for post in self.browse(cr, uid, ids, context=context):
419 if post.closed_reason_id.id in (reason_offensive, reason_spam):
420 _logger.info('Upvoting user <%s>, reopening spam/offensive question',
422 # TODO: in master, consider making this a tunable karma parameter
423 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id],
424 post.forum_id.karma_gen_question_downvote * -5,
426 self.pool['forum.post'].write(cr, SUPERUSER_ID, ids, {'state': 'active'}, context=context)
428 def close(self, cr, uid, ids, reason_id, context=None):
429 if any(post.parent_id for post in self.browse(cr, uid, ids, context=context)):
432 reason_offensive = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'website_forum.reason_7')
433 reason_spam = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'website_forum.reason_8')
434 if reason_id in (reason_offensive, reason_spam):
435 for post in self.browse(cr, uid, ids, context=context):
436 _logger.info('Downvoting user <%s> for posting spam/offensive contents',
438 # TODO: in master, consider making this a tunable karma parameter
439 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id],
440 post.forum_id.karma_gen_question_downvote * 5,
443 self.pool['forum.post'].write(cr, uid, ids, {
446 'closed_date': datetime.today().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT),
447 'closed_reason_id': reason_id,
450 def unlink(self, cr, uid, ids, context=None):
451 posts = self.browse(cr, uid, ids, context=context)
452 if any(not post.can_unlink for post in posts):
453 raise KarmaError('Not enough karma to unlink a post')
454 # if unlinking an answer with accepted answer: remove provided karma
457 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id], post.forum_id.karma_gen_answer_accepted * -1, context=context)
458 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], post.forum_id.karma_gen_answer_accept * -1, context=context)
459 return super(Post, self).unlink(cr, uid, ids, context=context)
461 def vote(self, cr, uid, ids, upvote=True, context=None):
462 Vote = self.pool['forum.post.vote']
463 vote_ids = Vote.search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
464 new_vote = '1' if upvote else '-1'
465 voted_forum_ids = set()
467 for vote in Vote.browse(cr, uid, vote_ids, context=context):
469 new_vote = '0' if vote.vote == '-1' else '1'
471 new_vote = '0' if vote.vote == '1' else '-1'
472 Vote.write(cr, uid, vote_ids, {'vote': new_vote}, context=context)
473 voted_forum_ids.add(vote.post_id.id)
474 for post_id in set(ids) - voted_forum_ids:
476 Vote.create(cr, uid, {'post_id': post_id, 'vote': new_vote}, context=context)
477 return {'vote_count': self._get_vote_count(cr, uid, ids, None, None, context=context)[ids[0]], 'user_vote': new_vote}
479 def convert_answer_to_comment(self, cr, uid, id, context=None):
480 """ Tools to convert an answer (forum.post) to a comment (mail.message).
481 The original post is unlinked and a new comment is posted on the question
482 using the post create_uid as the comment's author. """
483 post = self.browse(cr, SUPERUSER_ID, id, context=context)
484 if not post.parent_id:
487 # karma-based action check: use the post field that computed own/all value
488 if not post.can_comment_convert:
489 raise KarmaError('Not enough karma to convert an answer to a comment')
492 question = post.parent_id
494 'author_id': post.create_uid.partner_id.id,
495 'body': html2plaintext(post.content),
497 'subtype': 'mail.mt_comment',
498 'date': post.create_date,
500 message_id = self.pool['forum.post'].message_post(
501 cr, uid, question.id,
502 context=dict(context, mail_create_nosubcribe=True),
505 # unlink the original answer, using SUPERUSER_ID to avoid karma issues
506 self.pool['forum.post'].unlink(cr, SUPERUSER_ID, [post.id], context=context)
510 def convert_comment_to_answer(self, cr, uid, message_id, default=None, context=None):
511 """ Tool to convert a comment (mail.message) into an answer (forum.post).
512 The original comment is unlinked and a new answer from the comment's author
513 is created. Nothing is done if the comment's author already answered the
515 comment = self.pool['mail.message'].browse(cr, SUPERUSER_ID, message_id, context=context)
516 post = self.pool['forum.post'].browse(cr, uid, comment.res_id, context=context)
517 user = self.pool['res.users'].browse(cr, uid, uid, context=context)
518 if not comment.author_id or not comment.author_id.user_ids: # only comment posted by users can be converted
521 # karma-based action check: must check the message's author to know if own / all
522 karma_convert = comment.author_id.id == user.partner_id.id and post.forum_id.karma_comment_convert_own or post.forum_id.karma_comment_convert_all
523 can_convert = uid == SUPERUSER_ID or user.karma >= karma_convert
525 raise KarmaError('Not enough karma to convert a comment to an answer')
527 # check the message's author has not already an answer
528 question = post.parent_id if post.parent_id else post
529 post_create_uid = comment.author_id.user_ids[0]
530 if any(answer.create_uid.id == post_create_uid.id for answer in question.child_ids):
533 # create the new post
535 'forum_id': question.forum_id.id,
536 'content': comment.body,
537 'parent_id': question.id,
539 # done with the author user to have create_uid correctly set
540 new_post_id = self.pool['forum.post'].create(cr, post_create_uid.id, post_values, context=context)
543 self.pool['mail.message'].unlink(cr, SUPERUSER_ID, [comment.id], context=context)
547 def unlink_comment(self, cr, uid, id, message_id, context=None):
548 comment = self.pool['mail.message'].browse(cr, SUPERUSER_ID, message_id, context=context)
549 post = self.pool['forum.post'].browse(cr, uid, id, context=context)
550 user = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
551 if not comment.model == 'forum.post' or not comment.res_id == id:
554 # karma-based action check: must check the message's author to know if own or all
555 karma_unlink = comment.author_id.id == user.partner_id.id and post.forum_id.karma_comment_unlink_own or post.forum_id.karma_comment_unlink_all
556 can_unlink = uid == SUPERUSER_ID or user.karma >= karma_unlink
558 raise KarmaError('Not enough karma to unlink a comment')
560 return self.pool['mail.message'].unlink(cr, SUPERUSER_ID, [message_id], context=context)
562 def set_viewed(self, cr, uid, ids, context=None):
563 cr.execute("""UPDATE forum_post SET views = views+1 WHERE id IN %s""", (tuple(ids),))
566 def _get_access_link(self, cr, uid, mail, partner, context=None):
567 post = self.pool['forum.post'].browse(cr, uid, mail.res_id, context=context)
568 res_id = post.parent_id and "%s#answer-%s" % (post.parent_id.id, post.id) or post.id
569 return "/forum/%s/question/%s" % (post.forum_id.id, res_id)
571 @api.cr_uid_ids_context
572 def message_post(self, cr, uid, thread_id, type='notification', subtype=None, context=None, **kwargs):
573 if thread_id and type == 'comment': # user comments have a restriction on karma
574 if isinstance(thread_id, (list, tuple)):
575 post_id = thread_id[0]
578 post = self.browse(cr, uid, post_id, context=context)
579 if not post.can_comment:
580 raise KarmaError('Not enough karma to comment')
581 return super(Post, self).message_post(cr, uid, thread_id, type=type, subtype=subtype, context=context, **kwargs)
584 class PostReason(osv.Model):
585 _name = "forum.post.reason"
586 _description = "Post Closing Reason"
589 'name': fields.char('Post Reason', required=True, translate=True),
593 class Vote(osv.Model):
594 _name = 'forum.post.vote'
595 _description = 'Vote'
597 'post_id': fields.many2one('forum.post', 'Post', ondelete='cascade', required=True),
598 'user_id': fields.many2one('res.users', 'User', required=True),
599 'vote': fields.selection([('1', '1'), ('-1', '-1'), ('0', '0')], 'Vote', required=True),
600 'create_date': fields.datetime('Create Date', select=True, readonly=True),
602 # TODO master: store these two
603 'forum_id': fields.related('post_id', 'forum_id', type='many2one', relation='forum.forum', string='Forum'),
604 'recipient_id': fields.related('post_id', 'create_uid', type='many2one', relation='res.users', string='To', help="The user receiving the vote"),
607 'user_id': lambda self, cr, uid, ctx: uid,
608 'vote': lambda *args: '1',
611 def _get_karma_value(self, old_vote, new_vote, up_karma, down_karma):
613 '-1': {'-1': 0, '0': -1 * down_karma, '1': -1 * down_karma + up_karma},
614 '0': {'-1': 1 * down_karma, '0': 0, '1': up_karma},
615 '1': {'-1': -1 * up_karma + down_karma, '0': -1 * up_karma, '1': 0}
617 return _karma_upd[old_vote][new_vote]
619 def create(self, cr, uid, vals, context=None):
620 vote_id = super(Vote, self).create(cr, uid, vals, context=context)
621 vote = self.browse(cr, uid, vote_id, context=context)
624 if vote.user_id.id == vote.post_id.create_uid.id:
625 raise Warning('Not allowed to vote for its own post')
627 if vote.vote == '1' and not vote.post_id.can_upvote:
628 raise KarmaError('Not enough karma to upvote.')
629 elif vote.vote == '-1' and not vote.post_id.can_downvote:
630 raise KarmaError('Not enough karma to downvote.')
633 if vote.post_id.parent_id:
634 karma_value = self._get_karma_value('0', vote.vote, vote.forum_id.karma_gen_answer_upvote, vote.forum_id.karma_gen_answer_downvote)
636 karma_value = self._get_karma_value('0', vote.vote, vote.forum_id.karma_gen_question_upvote, vote.forum_id.karma_gen_question_downvote)
637 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [vote.recipient_id.id], karma_value, context=context)
640 def write(self, cr, uid, ids, values, context=None):
642 for vote in self.browse(cr, uid, ids, context=context):
644 if vote.user_id.id == vote.post_id.create_uid.id:
645 raise Warning('Not allowed to vote for its own post')
647 if (values['vote'] == '1' or vote.vote == '-1' and values['vote'] == '0') and not vote.post_id.can_upvote:
648 raise KarmaError('Not enough karma to upvote.')
649 elif (values['vote'] == '-1' or vote.vote == '1' and values['vote'] == '0') and not vote.post_id.can_downvote:
650 raise KarmaError('Not enough karma to downvote.')
653 if vote.post_id.parent_id:
654 karma_value = self._get_karma_value(vote.vote, values['vote'], vote.forum_id.karma_gen_answer_upvote, vote.forum_id.karma_gen_answer_downvote)
656 karma_value = self._get_karma_value(vote.vote, values['vote'], vote.forum_id.karma_gen_question_upvote, vote.forum_id.karma_gen_question_downvote)
657 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [vote.recipient_id.id], karma_value, context=context)
658 res = super(Vote, self).write(cr, uid, ids, values, context=context)
662 class Tags(osv.Model):
665 _inherit = ['website.seo.metadata']
667 def _get_posts_count(self, cr, uid, ids, field_name, arg, context=None):
668 return dict((tag_id, self.pool['forum.post'].search_count(cr, uid, [('tag_ids', 'in', tag_id)], context=context)) for tag_id in ids)
670 def _get_tag_from_post(self, cr, uid, ids, context=None):
672 [tag.id for post in self.pool['forum.post'].browse(cr, SUPERUSER_ID, ids, context=context) for tag in post.tag_ids]
676 'name': fields.char('Name', required=True),
677 'forum_id': fields.many2one('forum.forum', 'Forum', required=True),
678 'post_ids': fields.many2many('forum.post', 'forum_tag_rel', 'tag_id', 'post_id', 'Posts'),
679 'posts_count': fields.function(
680 _get_posts_count, type='integer', string="Number of Posts",
682 'forum.post': (_get_tag_from_post, ['tag_ids'], 10),
685 'create_uid': fields.many2one('res.users', 'Created by', readonly=True),