[IMP] name is name (and not URL in case of links
[odoo/odoo.git] / addons / website_forum / models / forum.py
1 # -*- coding: utf-8 -*-
2
3 from datetime import datetime
4
5 import openerp
6 from openerp import tools
7 from openerp import SUPERUSER_ID
8 from openerp.addons.website.models.website import slug
9 from openerp.osv import osv, fields
10 from openerp.tools import html2plaintext
11 from openerp.tools.translate import _
12
13
14 class KarmaError(ValueError):
15     """ Karma-related error, used for forum and posts. """
16     pass
17
18
19 class Forum(osv.Model):
20     """TDE TODO: set karma values for actions dynamic for a given forum"""
21     _name = 'forum.forum'
22     _description = 'Forums'
23     _inherit = ['mail.thread', 'website.seo.metadata']
24
25     _columns = {
26         'name': fields.char('Forum Name', required=True, translate=True),
27         'faq': fields.html('Guidelines'),
28         'description': fields.html('Description'),
29         'introduction_message': fields.html('Introduction Message'),
30         'relevancy_option_first': fields.float('First Relevancy Parameter'),
31         'relevancy_option_second': fields.float('Second Relevancy Parameter'),
32         'default_order': fields.selection([
33             ('create_date desc','Newest'),
34             ('write_date desc','Last Updated'),
35             ('vote_count desc','Most Voted'),
36             ('relevancy desc','Relevancy'),
37             ('child_count desc','Answered'),
38             ], 'Default Order', required=True),
39         'default_allow': fields.selection([('post_link','Link'),('ask_question','Question'),('post_discussion','Discussion')], 'Default Post', required=True),
40         'allow_link': fields.boolean('Links', help="When clicking on the post, it redirects to an external link"),
41         'allow_question': fields.boolean('Questions', help="Users can answer only once per question. Contributors can edit answers and mark the right ones."),
42         'allow_discussion': fields.boolean('Discussions'),
43         # karma generation
44         'karma_gen_question_new': fields.integer('Post a Questions'),
45         'karma_gen_question_upvote': fields.integer('Upvote a Question'),
46         'karma_gen_question_downvote': fields.integer('Downvote a Question'),
47         'karma_gen_answer_upvote': fields.integer('Upvote an Answer'),
48         'karma_gen_answer_downvote': fields.integer('Downvote an answer'),
49         'karma_gen_answer_accept': fields.integer('Accept an Answer'),
50         'karma_gen_answer_accepted': fields.integer('Have Your Answer Accepted'),
51         'karma_gen_answer_flagged': fields.integer('Have Your Answer Flagged'),
52         # karma-based actions
53         'karma_ask': fields.integer('Ask a new question'),
54         'karma_answer': fields.integer('Answer a question'),
55         'karma_edit_own': fields.integer('Edit its own posts'),
56         'karma_edit_all': fields.integer('Edit all posts'),
57         'karma_close_own': fields.integer('Close its own posts'),
58         'karma_close_all': fields.integer('Close all posts'),
59         'karma_unlink_own': fields.integer('Delete its own posts'),
60         'karma_unlink_all': fields.integer('Delete all posts'),
61         'karma_upvote': fields.integer('Upvote'),
62         'karma_downvote': fields.integer('Downvote'),
63         'karma_answer_accept_own': fields.integer('Accept an answer on its own questions'),
64         'karma_answer_accept_all': fields.integer('Accept an answers to all questions'),
65         'karma_editor_link_files': fields.integer('Linking files (Editor)'),
66         'karma_editor_clickable_link': fields.integer('Add clickable links (Editor)'),
67         'karma_comment_own': fields.integer('Comment its own posts'),
68         'karma_comment_all': fields.integer('Comment all posts'),
69         'karma_comment_convert_own': fields.integer('Convert its own answers to comments and vice versa'),
70         'karma_comment_convert_all': fields.integer('Convert all answers to answers and vice versa'),
71         'karma_comment_unlink_own': fields.integer('Unlink its own comments'),
72         'karma_comment_unlink_all': fields.integer('Unlink all comments'),
73         'karma_retag': fields.integer('Change question tags'),
74         'karma_flag': fields.integer('Flag a post as offensive'),
75     }
76
77     def _get_default_faq(self, cr, uid, context=None):
78         fname = openerp.modules.get_module_resource('website_forum', 'data', 'forum_default_faq.html')
79         with open(fname, 'r') as f:
80             return f.read()
81         return False
82
83     _defaults = {
84         'default_order': 'write_date desc',
85         'allow_question': True,
86         'default_allow': 'ask_question',
87         'allow_link': False,
88         'allow_discussion': False,
89         'description': 'This community is for professionals and enthusiasts of our products and services.',
90         'faq': _get_default_faq,
91         'introduction_message': """<h1 class="mt0">Welcome!</h1>
92                   <p> This community is for professionals and enthusiasts of our products and services.
93                       Share and discuss the best content and new marketing ideas,
94                       build your professional profile and become a better marketer together.
95                   </p>""",
96         'relevancy_option_first': 0.8,
97         'relevancy_option_second': 1.8,
98         'karma_gen_question_new': 2,
99         'karma_gen_question_upvote': 5,
100         'karma_gen_question_downvote': -2,
101         'karma_gen_answer_upvote': 10,
102         'karma_gen_answer_downvote': -2,
103         'karma_gen_answer_accept': 2,
104         'karma_gen_answer_accepted': 15,
105         'karma_gen_answer_flagged': -100,
106         'karma_ask': 0,
107         'karma_answer': 0,
108         'karma_edit_own': 1,
109         'karma_edit_all': 300,
110         'karma_close_own': 100,
111         'karma_close_all': 500,
112         'karma_unlink_own': 500,
113         'karma_unlink_all': 1000,
114         'karma_upvote': 5,
115         'karma_downvote': 50,
116         'karma_answer_accept_own': 20,
117         'karma_answer_accept_all': 500,
118         'karma_editor_link_files': 20,
119         'karma_editor_clickable_link': 20,
120         'karma_comment_own': 1,
121         'karma_comment_all': 1,
122         'karma_comment_convert_own': 50,
123         'karma_comment_convert_all': 500,
124         'karma_comment_unlink_own': 50,
125         'karma_comment_unlink_all': 500,
126         'karma_retag': 75,
127         'karma_flag': 500,
128     }
129
130     def create(self, cr, uid, values, context=None):
131         if context is None:
132             context = {}
133         create_context = dict(context, mail_create_nolog=True)
134         return super(Forum, self).create(cr, uid, values, context=create_context)
135
136
137 class Post(osv.Model):
138     _name = 'forum.post'
139     _description = 'Forum Post'
140     _inherit = ['mail.thread', 'website.seo.metadata']
141     _order = "is_correct DESC, vote_count DESC, write_date DESC"
142
143     def _get_post_relevancy(self, cr, uid, ids, field_name, arg, context):
144         res = dict.fromkeys(ids, 0)
145         for post in self.browse(cr, uid, ids, context=context):
146             days = (datetime.today() - datetime.strptime(post.create_date, tools.DEFAULT_SERVER_DATETIME_FORMAT)).days
147             relavency = abs(post.vote_count - 1) ** post.forum_id.relevancy_option_first / ( days + 2) ** post.forum_id.relevancy_option_second
148             res[post.id] = relavency if (post.vote_count - 1) >= 0 else -relavency
149         return res
150
151     def _get_user_vote(self, cr, uid, ids, field_name, arg, context):
152         res = dict.fromkeys(ids, 0)
153         vote_ids = self.pool['forum.post.vote'].search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
154         for vote in self.pool['forum.post.vote'].browse(cr, uid, vote_ids, context=context):
155             res[vote.post_id.id] = vote.vote
156         return res
157
158     def _get_vote_count(self, cr, uid, ids, field_name, arg, context):
159         res = dict.fromkeys(ids, 0)
160         for post in self.browse(cr, uid, ids, context=context):
161             for vote in post.vote_ids:
162                 res[post.id] += int(vote.vote)
163         return res
164
165     def _get_post_from_vote(self, cr, uid, ids, context=None):
166         result = {}
167         for vote in self.pool['forum.post.vote'].browse(cr, uid, ids, context=context):
168             result[vote.post_id.id] = True
169         return result.keys()
170
171     def _get_user_favourite(self, cr, uid, ids, field_name, arg, context):
172         res = dict.fromkeys(ids, False)
173         for post in self.browse(cr, uid, ids, context=context):
174             if uid in [f.id for f in post.favourite_ids]:
175                 res[post.id] = True
176         return res
177
178     def _get_favorite_count(self, cr, uid, ids, field_name, arg, context):
179         res = dict.fromkeys(ids, 0)
180         for post in self.browse(cr, uid, ids, context=context):
181             res[post.id] += len(post.favourite_ids)
182         return res
183
184     def _get_post_from_hierarchy(self, cr, uid, ids, context=None):
185         post_ids = set(ids)
186         for post in self.browse(cr, SUPERUSER_ID, ids, context=context):
187             if post.parent_id:
188                 post_ids.add(post.parent_id.id)
189         return list(post_ids)
190
191     def _get_child_count(self, cr, uid, ids, field_name=False, arg={}, context=None):
192         res = dict.fromkeys(ids, 0)
193         for post in self.browse(cr, uid, ids, context=context):
194             if post.parent_id:
195                 res[post.parent_id.id] = len(post.parent_id.child_ids)
196             else:
197                 res[post.id] = len(post.child_ids)
198         return res
199
200     def _get_uid_answered(self, cr, uid, ids, field_name, arg, context=None):
201         res = dict.fromkeys(ids, False)
202         for post in self.browse(cr, uid, ids, context=context):
203             res[post.id] = any(answer.create_uid.id == uid for answer in post.child_ids)
204         return res
205
206     def _get_has_validated_answer(self, cr, uid, ids, field_name, arg, context=None):
207         res = dict.fromkeys(ids, False)
208         ans_ids = self.search(cr, uid, [('parent_id', 'in', ids), ('is_correct', '=', True)], context=context)
209         for answer in self.browse(cr, uid, ans_ids, context=context):
210             res[answer.parent_id.id] = True
211         return res
212
213     def _is_self_reply(self, cr, uid, ids, field_name, arg, context=None):
214         res = dict.fromkeys(ids, False)
215         for post in self.browse(cr, uid, ids, context=context):
216             res[post.id] = post.parent_id and post.parent_id.create_uid == post.create_uid or False
217         return res
218
219     def _get_post_karma_rights(self, cr, uid, ids, field_name, arg, context=None):
220         user = self.pool['res.users'].browse(cr, uid, uid, context=context)
221         res = dict.fromkeys(ids, False)
222         for post in self.browse(cr, uid, ids, context=context):
223             res[post.id] = {
224                 'karma_ask': post.forum_id.karma_ask,
225                 'karma_answer': post.forum_id.karma_answer,
226                 '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,
227                 'karma_edit': post.create_uid.id == uid and post.forum_id.karma_edit_own or post.forum_id.karma_edit_all,
228                 'karma_close': post.create_uid.id == uid and post.forum_id.karma_close_own or post.forum_id.karma_close_all,
229                 'karma_unlink': post.create_uid.id == uid and post.forum_id.karma_unlink_own or post.forum_id.karma_unlink_all,
230                 'karma_upvote': post.forum_id.karma_upvote,
231                 'karma_downvote': post.forum_id.karma_downvote,
232                 'karma_comment': post.create_uid.id == uid and post.forum_id.karma_comment_own or post.forum_id.karma_comment_all,
233                 'karma_comment_convert': post.create_uid.id == uid and post.forum_id.karma_comment_convert_own or post.forum_id.karma_comment_convert_all,
234             }
235             res[post.id].update({
236                 'can_ask': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_ask'],
237                 'can_answer': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_answer'],
238                 'can_accept': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_accept'],
239                 'can_edit': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_edit'],
240                 'can_close': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_close'],
241                 'can_unlink': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_unlink'],
242                 'can_upvote': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_upvote'],
243                 'can_downvote': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_downvote'],
244                 'can_comment': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_comment'],
245                 'can_comment_convert': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_comment_convert'],
246             })
247         return res
248
249     _columns = {
250         'name': fields.char('Title'),
251         'forum_id': fields.many2one('forum.forum', 'Forum', required=True),
252         'content': fields.html('Content'),
253         'content_link': fields.char('URL', help="URL of Link Articles"),
254         'tag_ids': fields.many2many('forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', 'Tags'),
255         'state': fields.selection([('active', 'Active'), ('close', 'Close'), ('offensive', 'Offensive')], 'Status'),
256         'views': fields.integer('Number of Views'),
257         'active': fields.boolean('Active'),
258         'type': fields.selection([('question', 'Question'), ('link', 'Article'), ('discussion', 'Discussion')], 'Type'),
259         'relevancy': fields.function(
260             _get_post_relevancy, string="Relevancy", type='float',
261             store={
262                 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
263                 'forum.post.vote': (_get_post_from_vote, [], 10),
264             }),
265         'is_correct': fields.boolean('Valid Answer', help='Correct Answer or Answer on this question accepted.'),
266         'website_message_ids': fields.one2many(
267             'mail.message', 'res_id',
268             domain=lambda self: [
269                 '&', ('model', '=', self._name), ('type', 'in', ['email', 'comment'])
270             ],
271             string='Post Messages', help="Comments on forum post",
272         ),
273         # history
274         'create_date': fields.datetime('Asked on', select=True, readonly=True),
275         'create_uid': fields.many2one('res.users', 'Created by', select=True, readonly=True),
276         'write_date': fields.datetime('Update on', select=True, readonly=True),
277         'write_uid': fields.many2one('res.users', 'Updated by', select=True, readonly=True),
278         # vote fields
279         'vote_ids': fields.one2many('forum.post.vote', 'post_id', 'Votes'),
280         'user_vote': fields.function(_get_user_vote, string='My Vote', type='integer'),
281         'vote_count': fields.function(
282             _get_vote_count, string="Votes", type='integer',
283             store={
284                 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
285                 'forum.post.vote': (_get_post_from_vote, [], 10),
286             }),
287         # favorite fields
288         'favourite_ids': fields.many2many('res.users', string='Favourite'),
289         'user_favourite': fields.function(_get_user_favourite, string="My Favourite", type='boolean'),
290         'favourite_count': fields.function(
291             _get_favorite_count, string='Favorite Count', type='integer',
292             store={
293                 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['favourite_ids'], 10),
294             }),
295         # hierarchy
296         'parent_id': fields.many2one('forum.post', 'Question', ondelete='cascade'),
297         'self_reply': fields.function(
298             _is_self_reply, 'Reply to own question', type='boolean',
299             store={
300                 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['parent_id', 'create_uid'], 10),
301             }),
302         'child_ids': fields.one2many('forum.post', 'parent_id', 'Answers'),
303         'child_count': fields.function(
304             _get_child_count, string="Answers", type='integer',
305             store={
306                 'forum.post': (_get_post_from_hierarchy, ['parent_id', 'child_ids'], 10),
307             }),
308         'uid_has_answered': fields.function(
309             _get_uid_answered, string='Has Answered', type='boolean',
310         ),
311         'has_validated_answer': fields.function(
312             _get_has_validated_answer, string='Has a Validated Answered', type='boolean',
313             store={
314                 'forum.post': (_get_post_from_hierarchy, ['parent_id', 'child_ids', 'is_correct'], 10),
315             }
316         ),
317         # closing
318         'closed_reason_id': fields.many2one('forum.post.reason', 'Reason'),
319         'closed_uid': fields.many2one('res.users', 'Closed by', select=1),
320         'closed_date': fields.datetime('Closed on', readonly=True),
321         # karma
322         'karma_ask': fields.function(_get_post_karma_rights, string='Karma to ask', type='integer', multi='_get_post_karma_rights'),
323         'karma_answer': fields.function(_get_post_karma_rights, string='Karma to answer', type='integer', multi='_get_post_karma_rights'),
324         'karma_accept': fields.function(_get_post_karma_rights, string='Karma to accept this answer', type='integer', multi='_get_post_karma_rights'),
325         'karma_edit': fields.function(_get_post_karma_rights, string='Karma to edit', type='integer', multi='_get_post_karma_rights'),
326         'karma_close': fields.function(_get_post_karma_rights, string='Karma to close', type='integer', multi='_get_post_karma_rights'),
327         'karma_unlink': fields.function(_get_post_karma_rights, string='Karma to unlink', type='integer', multi='_get_post_karma_rights'),
328         'karma_upvote': fields.function(_get_post_karma_rights, string='Karma to upvote', type='integer', multi='_get_post_karma_rights'),
329         'karma_downvote': fields.function(_get_post_karma_rights, string='Karma to downvote', type='integer', multi='_get_post_karma_rights'),
330         'karma_comment': fields.function(_get_post_karma_rights, string='Karma to comment', type='integer', multi='_get_post_karma_rights'),
331         'karma_comment_convert': fields.function(_get_post_karma_rights, string='karma to convert as a comment', type='integer', multi='_get_post_karma_rights'),
332         # access rights
333         'can_ask': fields.function(_get_post_karma_rights, string='Can Ask', type='boolean', multi='_get_post_karma_rights'),
334         'can_answer': fields.function(_get_post_karma_rights, string='Can Answer', type='boolean', multi='_get_post_karma_rights'),
335         'can_accept': fields.function(_get_post_karma_rights, string='Can Accept', type='boolean', multi='_get_post_karma_rights'),
336         'can_edit': fields.function(_get_post_karma_rights, string='Can Edit', type='boolean', multi='_get_post_karma_rights'),
337         'can_close': fields.function(_get_post_karma_rights, string='Can Close', type='boolean', multi='_get_post_karma_rights'),
338         'can_unlink': fields.function(_get_post_karma_rights, string='Can Unlink', type='boolean', multi='_get_post_karma_rights'),
339         'can_upvote': fields.function(_get_post_karma_rights, string='Can Upvote', type='boolean', multi='_get_post_karma_rights'),
340         'can_downvote': fields.function(_get_post_karma_rights, string='Can Downvote', type='boolean', multi='_get_post_karma_rights'),
341         'can_comment': fields.function(_get_post_karma_rights, string='Can Comment', type='boolean', multi='_get_post_karma_rights'),
342         'can_comment_convert': fields.function(_get_post_karma_rights, string='Can Convert to Comment', type='boolean', multi='_get_post_karma_rights'),
343     }
344
345     _defaults = {
346         'state': 'active',
347         'views': 0,
348         'active': True,
349         'type': 'question',
350         'vote_ids': list(),
351         'favourite_ids': list(),
352         'child_ids': list(),
353     }
354
355     def create(self, cr, uid, vals, context=None):
356         if context is None:
357             context = {}
358         create_context = dict(context, mail_create_nolog=True)
359         post_id = super(Post, self).create(cr, uid, vals, context=create_context)
360         post = self.browse(cr, SUPERUSER_ID, post_id, context=context)  # SUPERUSER_ID to avoid read access rights issues when creating
361         # deleted or closed questions
362         if post.parent_id and (post.parent_id.state == 'close' or post.parent_id.active == False):
363             osv.except_osv(_('Error !'), _('Posting answer on [Deleted] or [Closed] question is prohibited'))
364         # karma-based access
365         if post.parent_id and not post.can_ask:
366             raise KarmaError('Not enough karma to create a new question')
367         elif not post.parent_id and not post.can_answer:
368             raise KarmaError('Not enough karma to answer to a question')
369         # messaging and chatter
370         base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
371         if post.parent_id:
372             body = _(
373                 '<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>' %
374                 (post.parent_id.name, base_url, slug(post.parent_id.forum_id), slug(post.parent_id))
375             )
376             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)
377         else:
378             body = _(
379                 '<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>' %
380                 (post.name, post.forum_id.name, base_url, slug(post.forum_id), slug(post))
381             )
382             self.message_post(cr, uid, post_id, subject=post.name, body=body, subtype='website_forum.mt_question_new', context=context)
383             self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], post.forum_id.karma_gen_question_new, context=context)
384         return post_id
385
386     def write(self, cr, uid, ids, vals, context=None):
387         posts = self.browse(cr, uid, ids, context=context)
388         if 'state' in vals:
389             if vals['state'] in ['active', 'close'] and any(not post.can_close for post in posts):
390                 raise KarmaError('Not enough karma to close or reopen a post.')
391         if 'active' in vals:
392             if any(not post.can_unlink for post in posts):
393                 raise KarmaError('Not enough karma to delete or reactivate a post')
394         if 'is_correct' in vals:
395             if any(not post.can_accept for post in posts):
396                 raise KarmaError('Not enough karma to accept or refuse an answer')
397             # update karma except for self-acceptance
398             mult = 1 if vals['is_correct'] else -1
399             for post in self.browse(cr, uid, ids, context=context):
400                 if vals['is_correct'] != post.is_correct and post.create_uid.id != uid:
401                     self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id], post.forum_id.karma_gen_answer_accepted * mult, context=context)
402                     self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], post.forum_id.karma_gen_answer_accept * mult, context=context)
403         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):
404             raise KarmaError('Not enough karma to edit a post.')
405
406         res = super(Post, self).write(cr, uid, ids, vals, context=context)
407         # if post content modify, notify followers
408         if 'content' in vals or 'name' in vals:
409             for post in posts:
410                 if post.parent_id:
411                     body, subtype = _('Answer Edited'), 'website_forum.mt_answer_edit'
412                     obj_id = post.parent_id.id
413                 else:
414                     body, subtype = _('Question Edited'), 'website_forum.mt_question_edit'
415                     obj_id = post.id
416                 self.message_post(cr, uid, obj_id, body=body, subtype=subtype, context=context)
417         return res
418
419     def close(self, cr, uid, ids, reason_id, context=None):
420         if any(post.parent_id for post in self.browse(cr, uid, ids, context=context)):
421             return False
422         return self.pool['forum.post'].write(cr, uid, ids, {
423             'state': 'close',
424             'closed_uid': uid,
425             'closed_date': datetime.today().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT),
426             'closed_reason_id': reason_id,
427         }, context=context)
428
429     def unlink(self, cr, uid, ids, context=None):
430         posts = self.browse(cr, uid, ids, context=context)
431         if any(not post.can_unlink for post in posts):
432             raise KarmaError('Not enough karma to unlink a post')
433         # if unlinking an answer with accepted answer: remove provided karma
434         for post in posts:
435             if post.is_correct:
436                 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id], post.forum_id.karma_gen_answer_accepted * -1, context=context)
437                 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], post.forum_id.karma_gen_answer_accept * -1, context=context)
438         return super(Post, self).unlink(cr, uid, ids, context=context)
439
440     def vote(self, cr, uid, ids, upvote=True, context=None):
441         posts = self.browse(cr, uid, ids, context=context)
442
443         if upvote and any(not post.can_upvote for post in posts):
444             raise KarmaError('Not enough karma to upvote.')
445         elif not upvote and any(not post.can_downvote for post in posts):
446             raise KarmaError('Not enough karma to downvote.')
447
448         Vote = self.pool['forum.post.vote']
449         vote_ids = Vote.search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
450         new_vote = '1' if upvote else '-1'
451         voted_forum_ids = set()
452         if vote_ids:
453             for vote in Vote.browse(cr, uid, vote_ids, context=context):
454                 if upvote:
455                     new_vote = '0' if vote.vote == '-1' else '1'
456                 else:
457                     new_vote = '0' if vote.vote == '1' else '-1'
458                 Vote.write(cr, uid, vote_ids, {'vote': new_vote}, context=context)
459                 voted_forum_ids.add(vote.post_id.id)
460         for post_id in set(ids) - voted_forum_ids:
461             for post_id in ids:
462                 Vote.create(cr, uid, {'post_id': post_id, 'vote': new_vote}, context=context)
463         return {'vote_count': self._get_vote_count(cr, uid, ids, None, None, context=context)[ids[0]], 'user_vote': new_vote}
464
465     def convert_answer_to_comment(self, cr, uid, id, context=None):
466         """ Tools to convert an answer (forum.post) to a comment (mail.message).
467         The original post is unlinked and a new comment is posted on the question
468         using the post create_uid as the comment's author. """
469         post = self.browse(cr, SUPERUSER_ID, id, context=context)
470         if not post.parent_id:
471             return False
472
473         # karma-based action check: use the post field that computed own/all value
474         if not post.can_comment_convert:
475             raise KarmaError('Not enough karma to convert an answer to a comment')
476
477         # post the message
478         question = post.parent_id
479         values = {
480             'author_id': post.create_uid.partner_id.id,
481             'body': html2plaintext(post.content),
482             'type': 'comment',
483             'subtype': 'mail.mt_comment',
484             'date': post.create_date,
485         }
486         message_id = self.pool['forum.post'].message_post(
487             cr, uid, question.id,
488             context=dict(context, mail_create_nosubcribe=True),
489             **values)
490
491         # unlink the original answer, using SUPERUSER_ID to avoid karma issues
492         self.pool['forum.post'].unlink(cr, SUPERUSER_ID, [post.id], context=context)
493
494         return message_id
495
496     def convert_comment_to_answer(self, cr, uid, message_id, default=None, context=None):
497         """ Tool to convert a comment (mail.message) into an answer (forum.post).
498         The original comment is unlinked and a new answer from the comment's author
499         is created. Nothing is done if the comment's author already answered the
500         question. """
501         comment = self.pool['mail.message'].browse(cr, SUPERUSER_ID, message_id, context=context)
502         post = self.pool['forum.post'].browse(cr, uid, comment.res_id, context=context)
503         user = self.pool['res.users'].browse(cr, uid, uid, context=context)
504         if not comment.author_id or not comment.author_id.user_ids:  # only comment posted by users can be converted
505             return False
506
507         # karma-based action check: must check the message's author to know if own / all
508         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
509         can_convert = uid == SUPERUSER_ID or user.karma >= karma_convert
510         if not can_convert:
511             raise KarmaError('Not enough karma to convert a comment to an answer')
512
513         # check the message's author has not already an answer
514         question = post.parent_id if post.parent_id else post
515         post_create_uid = comment.author_id.user_ids[0]
516         if any(answer.create_uid.id == post_create_uid.id for answer in question.child_ids):
517             return False
518
519         # create the new post
520         post_values = {
521             'forum_id': question.forum_id.id,
522             'content': comment.body,
523             'parent_id': question.id,
524         }
525         # done with the author user to have create_uid correctly set
526         new_post_id = self.pool['forum.post'].create(cr, post_create_uid.id, post_values, context=context)
527
528         # delete comment
529         self.pool['mail.message'].unlink(cr, SUPERUSER_ID, [comment.id], context=context)
530
531         return new_post_id
532
533     def unlink_comment(self, cr, uid, id, message_id, context=None):
534         comment = self.pool['mail.message'].browse(cr, SUPERUSER_ID, message_id, context=context)
535         post = self.pool['forum.post'].browse(cr, uid, id, context=context)
536         user = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
537         if not comment.model == 'forum.post' or not comment.res_id == id:
538             return False
539
540         # karma-based action check: must check the message's author to know if own or all
541         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
542         can_unlink = uid == SUPERUSER_ID or user.karma >= karma_unlink
543         if not can_unlink:
544             raise KarmaError('Not enough karma to unlink a comment')
545
546         return self.pool['mail.message'].unlink(cr, SUPERUSER_ID, [message_id], context=context)
547
548     def set_viewed(self, cr, uid, ids, context=None):
549         cr.execute("""UPDATE forum_post SET views = views+1 WHERE id IN %s""", (tuple(ids),))
550         return True
551
552     def _get_access_link(self, cr, uid, mail, partner, context=None):
553         post = self.pool['forum.post'].browse(cr, uid, mail.res_id, context=context)
554         res_id = post.parent_id and "%s#answer-%s" % (post.parent_id.id, post.id) or post.id
555         return "/forum/%s/question/%s" % (post.forum_id.id, res_id)
556
557
558 class PostReason(osv.Model):
559     _name = "forum.post.reason"
560     _description = "Post Closing Reason"
561     _order = 'name'
562     _columns = {
563         'name': fields.char('Post Reason', required=True, translate=True),
564     }
565
566
567 class Vote(osv.Model):
568     _name = 'forum.post.vote'
569     _description = 'Vote'
570     _columns = {
571         'post_id': fields.many2one('forum.post', 'Post', ondelete='cascade', required=True),
572         'user_id': fields.many2one('res.users', 'User', required=True),
573         'vote': fields.selection([('1', '1'), ('-1', '-1'), ('0', '0')], 'Vote', required=True),
574         'create_date': fields.datetime('Create Date', select=True, readonly=True),
575
576         # TODO master: store these two
577         'forum_id': fields.related('post_id', 'forum_id', type='many2one', relation='forum.forum', string='Forum'),
578         'recipient_id': fields.related('post_id', 'create_uid', type='many2one', relation='res.users', string='To', help="The user receiving the vote"),
579     }
580     _defaults = {
581         'user_id': lambda self, cr, uid, ctx: uid,
582         'vote': lambda *args: '1',
583     }
584
585     def _get_karma_value(self, old_vote, new_vote, up_karma, down_karma):
586         _karma_upd = {
587             '-1': {'-1': 0, '0': -1 * down_karma, '1': -1 * down_karma + up_karma},
588             '0': {'-1': 1 * down_karma, '0': 0, '1': up_karma},
589             '1': {'-1': -1 * up_karma + down_karma, '0': -1 * up_karma, '1': 0}
590         }
591         return _karma_upd[old_vote][new_vote]
592
593     def create(self, cr, uid, vals, context=None):
594         vote_id = super(Vote, self).create(cr, uid, vals, context=context)
595         vote = self.browse(cr, uid, vote_id, context=context)
596         if vote.post_id.parent_id:
597             karma_value = self._get_karma_value('0', vote.vote, vote.forum_id.karma_gen_answer_upvote, vote.forum_id.karma_gen_answer_downvote)
598         else:
599             karma_value = self._get_karma_value('0', vote.vote, vote.forum_id.karma_gen_question_upvote, vote.forum_id.karma_gen_question_downvote)
600         self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [vote.recipient_id.id], karma_value, context=context)
601         return vote_id
602
603     def write(self, cr, uid, ids, values, context=None):
604         if 'vote' in values:
605             for vote in self.browse(cr, uid, ids, context=context):
606                 if vote.post_id.parent_id:
607                     karma_value = self._get_karma_value(vote.vote, values['vote'], vote.forum_id.karma_gen_answer_upvote, vote.forum_id.karma_gen_answer_downvote)
608                 else:
609                     karma_value = self._get_karma_value(vote.vote, values['vote'], vote.forum_id.karma_gen_question_upvote, vote.forum_id.karma_gen_question_downvote)
610                 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [vote.recipient_id.id], karma_value, context=context)
611         res = super(Vote, self).write(cr, uid, ids, values, context=context)
612         return res
613
614
615 class Tags(osv.Model):
616     _name = "forum.tag"
617     _description = "Tag"
618     _inherit = ['website.seo.metadata']
619
620     def _get_posts_count(self, cr, uid, ids, field_name, arg, context=None):
621         return dict((tag_id, self.pool['forum.post'].search_count(cr, uid, [('tag_ids', 'in', tag_id)], context=context)) for tag_id in ids)
622
623     def _get_tag_from_post(self, cr, uid, ids, context=None):
624         return list(set(
625             [tag.id for post in self.pool['forum.post'].browse(cr, SUPERUSER_ID, ids, context=context) for tag in post.tag_ids]
626         ))
627
628     _columns = {
629         'name': fields.char('Name', required=True),
630         'forum_id': fields.many2one('forum.forum', 'Forum', required=True),
631         'post_ids': fields.many2many('forum.post', 'forum_tag_rel', 'tag_id', 'post_id', 'Posts'),
632         'posts_count': fields.function(
633             _get_posts_count, type='integer', string="Number of Posts",
634             store={
635                 'forum.post': (_get_tag_from_post, ['tag_ids'], 10),
636             }
637         ),
638         'create_uid': fields.many2one('res.users', 'Created by', readonly=True),
639     }