[FIX] Promote to doc flow
[odoo/odoo.git] / addons / website_forum / models / forum.py
1 # -*- coding: utf-8 -*-
2
3 import openerp
4
5 from openerp import SUPERUSER_ID
6 from openerp.addons.website.models.website import slug
7 from openerp.osv import osv, fields
8 from openerp.tools.translate import _
9
10
11 class Forum(osv.Model):
12     _name = 'forum.forum'
13     _description = 'Forums'
14     _inherit = ['website.seo.metadata']
15
16     _columns = {
17         'name': fields.char('Name', required=True, translate=True),
18         'faq': fields.html('Guidelines'),
19         'description': fields.html('Description'),
20     }
21
22     def _get_default_faq(self, cr, uid, context=None):
23         fname = openerp.modules.get_module_resource('website_forum', 'data', 'forum_default_faq.html')
24         with open(fname, 'r') as f:
25             return f.read()
26         return False
27
28     _defaults = {
29         'description': 'This community is for professionals and enthusiasts of our products and services.',
30         'faq': _get_default_faq,
31     }
32
33     def create(self, cr, uid, values, context=None):
34         if context is None:
35             context = {}
36         create_context = dict(context, mail_create_nolog=True)
37         return super(Forum, self).create(cr, uid, values, context=create_context)
38
39
40 class Post(osv.Model):
41     _name = 'forum.post'
42     _description = 'Forum Post'
43     _inherit = ['mail.thread', 'website.seo.metadata']
44
45     def _get_user_vote(self, cr, uid, ids, field_name, arg, context):
46         res = dict.fromkeys(ids, 0)
47         vote_ids = self.pool['forum.post.vote'].search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
48         for vote in self.pool['forum.post.vote'].browse(cr, uid, vote_ids, context=context):
49             res[vote.post_id.id] = vote.vote
50         return res
51
52     def _get_vote_count(self, cr, uid, ids, field_name, arg, context):
53         res = dict.fromkeys(ids, 0)
54         for post in self.browse(cr, uid, ids, context=context):
55             for vote in post.vote_ids:
56                 res[post.id] += int(vote.vote)
57         return res
58
59     def _get_post_from_vote(self, cr, uid, ids, context=None):
60         result = {}
61         for vote in self.pool['forum.post.vote'].browse(cr, uid, ids, context=context):
62             result[vote.post_id.id] = True
63         return result.keys()
64
65     def _get_user_favourite(self, cr, uid, ids, field_name, arg, context):
66         res = dict.fromkeys(ids, False)
67         for post in self.browse(cr, uid, ids, context=context):
68             if uid in [f.id for f in post.favourite_ids]:
69                 res[post.id] = True
70         return res
71
72     def _get_favorite_count(self, cr, uid, ids, field_name, arg, context):
73         res = dict.fromkeys(ids, 0)
74         for post in self.browse(cr, uid, ids, context=context):
75             res[post.id] += len(post.favourite_ids)
76         return res
77
78     def _get_post_from_hierarchy(self, cr, uid, ids, context=None):
79         post_ids = set(ids)
80         for post in self.browse(cr, SUPERUSER_ID, ids, context=context):
81             if post.parent_id:
82                 post_ids.add(post.parent_id.id)
83         return list(post_ids)
84
85     def _get_child_count(self, cr, uid, ids, field_name=False, arg={}, context=None):
86         res = dict.fromkeys(ids, 0)
87         for post in self.browse(cr, uid, ids, context=context):
88             if post.parent_id:
89                 res[post.parent_id.id] = len(post.parent_id.child_ids)
90             else:
91                 res[post.id] = len(post.child_ids)
92         return res
93
94     def _get_uid_answered(self, cr, uid, ids, field_name, arg, context=None):
95         res = dict.fromkeys(ids, False)
96         for post in self.browse(cr, uid, ids, context=context):
97             res[post.id] = any(answer.create_uid.id == uid for answer in post.child_ids)
98         return res
99
100     def _is_self_reply(self, cr, uid, ids, field_name, arg, context=None):
101         res = dict.fromkeys(ids, False)
102         for post in self.browse(cr, uid, ids, context=context):
103             res[post.id] = post.parent_id and post.parent_id.create_uid == post.create_uid or False
104         return res
105
106     _columns = {
107         'name': fields.char('Title', size=128),
108         'forum_id': fields.many2one('forum.forum', 'Forum', required=True),
109         'content': fields.html('Content'),
110         'tag_ids': fields.many2many('forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', 'Tags'),
111         'state': fields.selection([('active', 'Active'), ('close', 'Close'), ('offensive', 'Offensive')], 'Status'),
112         'views': fields.integer('Number of Views'),
113         'active': fields.boolean('Active'),
114         'is_correct': fields.boolean('Valid Answer', help='Correct Answer or Answer on this question accepted.'),
115         'website_message_ids': fields.one2many(
116             'mail.message', 'res_id',
117             domain=lambda self: [
118                 '&', ('model', '=', self._name), ('type', '=', 'comment')
119             ],
120             string='Post Messages', help="Comments on forum post",
121         ),
122         # history
123         'create_date': fields.datetime('Asked on', select=True, readonly=True),
124         'create_uid': fields.many2one('res.users', 'Created by', select=True, readonly=True),
125         'write_date': fields.datetime('Update on', select=True, readonly=True),
126         'write_uid': fields.many2one('res.users', 'Updated by', select=True, readonly=True),
127         # vote fields
128         'vote_ids': fields.one2many('forum.post.vote', 'post_id', 'Votes'),
129         'user_vote': fields.function(_get_user_vote, string='My Vote', type='integer'),
130         'vote_count': fields.function(
131             _get_vote_count, string="Votes", type='integer',
132             store={
133                 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
134                 'forum.post.vote': (_get_post_from_vote, [], 10),
135             }),
136         # favorite fields
137         'favourite_ids': fields.many2many('res.users', string='Favourite'),
138         'user_favourite': fields.function(_get_user_favourite, string="My Favourite", type='boolean'),
139         'favourite_count': fields.function(
140             _get_favorite_count, string='Favorite Count', type='integer',
141             store={
142                 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['favourite_ids'], 10),
143             }),
144         # hierarchy
145         'parent_id': fields.many2one('forum.post', 'Question', ondelete='cascade'),
146         'self_reply': fields.function(_is_self_reply, 'Reply to own question', type='boolean',
147             store={
148                 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['parent_id', 'create_uid'], 10),
149             }),
150         'child_ids': fields.one2many('forum.post', 'parent_id', 'Answers'),
151         'child_count': fields.function(
152             _get_child_count, string="Answers", type='integer',
153             store={
154                 'forum.post': (_get_post_from_hierarchy, ['parent_id', 'child_ids'], 10),
155             }),
156         'uid_has_answered': fields.function(
157             _get_uid_answered, string='Has Answered', type='boolean',
158         ),
159         # closing
160         'closed_reason_id': fields.many2one('forum.post.reason', 'Reason'),
161         'closed_uid': fields.many2one('res.users', 'Closed by', select=1),
162         'closed_date': fields.datetime('Closed on', readonly=True),
163     }
164
165     _defaults = {
166         'state': 'active',
167         'views': 0,
168         'active': True,
169         'vote_ids': list(),
170         'favourite_ids': list(),
171         'child_ids': list(),
172     }
173
174     def create(self, cr, uid, vals, context=None):
175         if context is None:
176             context = {}
177         create_context = dict(context, mail_create_nolog=True)
178         post_id = super(Post, self).create(cr, uid, vals, context=create_context)
179         # post message + subtype depending on parent_id
180         if vals.get("parent_id"):
181             parent = self.browse(cr, SUPERUSER_ID, vals['parent_id'], context=context)
182             body = _('<p><a href="forum/%s/question/%s">New Answer Posted</a></p>' % (slug(parent.forum_id), slug(parent)))
183             self.message_post(cr, uid, parent.id, subject=_('Re: %s') % parent.name, body=body, subtype='website_forum.mt_answer_new', context=context)
184         else:
185             self.message_post(cr, uid, post_id, subject=vals.get('name', ''), body=_('New Question Created'), subtype='website_forum.mt_question_new', context=context)
186             self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], 2, context=context)
187         return post_id
188
189     def write(self, cr, uid, ids, vals, context=None):
190         res = super(Post, self).write(cr, uid, ids, vals, context=context)
191         # if post content modify, notify followers
192         if 'content' in vals or 'name' in vals:
193             for post in self.browse(cr, uid, ids, context=context):
194                 if post.parent_id:
195                     body, subtype = _('Answer Edited'), 'website_forum.mt_answer_edit'
196                     obj_id = post.parent_id.id
197                 else:
198                     body, subtype = _('Question Edited'), 'website_forum.mt_question_edit'
199                     obj_id = post.id
200                 self.message_post(cr, uid, obj_id, body=_(body), subtype=subtype, context=context)
201         # update karma of related user when any answer accepted
202         if 'correct' in vals:
203             for post in self.browse(cr, uid, ids, context=context):
204                 karma_value = 15 if vals.get('correct') else -15
205                 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id], {'karma': karma_value}, context=context)
206         return res
207
208     def vote(self, cr, uid, ids, upvote=True, context=None):
209         Vote = self.pool['forum.post.vote']
210         vote_ids = Vote.search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
211         if vote_ids:
212             for vote in Vote.browse(cr, uid, vote_ids, context=context):
213                 if upvote:
214                     new_vote = '0' if vote.vote == '-1' else '1'
215                 else:
216                     new_vote = '0' if vote.vote == '1' else '-1'
217                 Vote.write(cr, uid, vote_ids, {'vote': new_vote}, context=context)
218         else:
219             for post_id in ids:
220                 new_vote = '1' if upvote else '-1'
221                 Vote.create(cr, uid, {'post_id': post_id, 'vote': new_vote}, context=context)
222         return {'vote_count': self._get_vote_count(cr, uid, ids, None, None, context=context)[ids[0]]}
223
224     def set_viewed(self, cr, uid, ids, context=None):
225         for post in self.browse(cr, uid, ids, context=context):
226             self.write(cr, uid, [post.id], {'views': post.views + 1}, context=context)
227         return True
228
229
230 class PostReason(osv.Model):
231     _name = "forum.post.reason"
232     _description = "Post Closing Reason"
233     _order = 'name'
234     _columns = {
235         'name': fields.char('Post Reason', required=True, translate=True),
236     }
237
238
239 class Vote(osv.Model):
240     _name = 'forum.post.vote'
241     _description = 'Vote'
242     _columns = {
243         'post_id': fields.many2one('forum.post', 'Post', ondelete='cascade', required=True),
244         'user_id': fields.many2one('res.users', 'User', required=True),
245         'vote': fields.selection([('1', '1'), ('-1', '-1'), ('0', '0')], 'Vote', required=True),
246         'create_date': fields.datetime('Create Date', select=True, readonly=True),
247     }
248     _defaults = {
249         'user_id': lambda self, cr, uid, ctx: uid,
250         'vote': lambda *args: '1',
251     }
252
253     def create(self, cr, uid, vals, context=None):
254         vote_id = super(Vote, self).create(cr, uid, vals, context=context)
255         karma_value = int(vals.get('vote', '1')) * 10
256         post = self.pool['forum.post'].browse(cr, uid, vals.get('post_id'), context=context)
257         self.pool['res.users'].add_karma(cr, SUPERUSER_ID, post.create_uid.id, karma_value, context=context)
258         return vote_id
259
260     def write(self, cr, uid, ids, values, context=None):
261         if 'vote' in values:
262             for vote in self.browse(cr, uid, ids, context=context):
263                 karma_value = (int(values.get('vote')) - int(vote.vote)) * 10
264                 self.pool['res.users'].add_karma(cr, SUPERUSER_ID, vote.post_id.create_uid.id, karma_value, context=context)
265         res = super(Vote, self).write(cr, uid, ids, values, context=context)
266         return res
267
268
269 class Tags(osv.Model):
270     _name = "forum.tag"
271     _description = "Tag"
272     _inherit = ['website.seo.metadata']
273
274     def _get_posts_count(self, cr, uid, ids, field_name, arg, context=None):
275         return dict((tag_id, self.pool['forum.post'].search_count(cr, uid, [('tag_ids', 'in', tag_id)], context=context)) for tag_id in ids)
276
277     def _get_tag_from_post(self, cr, uid, ids, context=None):
278         return list(set(
279             [tag.id for post in self.pool['forum.post'].browse(cr, SUPERUSER_ID, ids, context=context) for tag in post.tag_ids]
280         ))
281
282     _columns = {
283         'name': fields.char('Name', required=True),
284         'forum_id': fields.many2one('forum.forum', 'Forum', required=True),
285         'post_ids': fields.many2many('forum.post', 'forum_tag_rel', 'tag_id', 'post_id', 'Posts'),
286         'posts_count': fields.function(
287             _get_posts_count, type='integer', string="Number of Posts",
288             store={
289                 'forum.post': (_get_tag_from_post, ['tag_ids'], 10),
290             }
291         ),
292         'create_uid': fields.many2one('res.users', 'Created by', readonly=True),
293     }