2014-02-21 13:16:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-03-09 18:41:19 +00:00
|
|
|
import openerp
|
2014-04-09 16:50:59 +00:00
|
|
|
|
2014-02-22 17:19:37 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
|
|
|
from openerp.osv import osv, fields
|
2014-02-21 13:16:08 +00:00
|
|
|
from openerp.tools.translate import _
|
|
|
|
|
|
|
|
|
2014-02-22 17:19:37 +00:00
|
|
|
class Forum(osv.Model):
|
|
|
|
_name = 'website.forum'
|
2014-03-03 19:17:48 +00:00
|
|
|
_description = 'Forums'
|
2014-02-22 17:19:37 +00:00
|
|
|
_inherit = ['mail.thread', 'website.seo.metadata']
|
2014-03-28 09:03:57 +00:00
|
|
|
|
2014-03-04 10:49:30 +00:00
|
|
|
_columns = {
|
2014-03-03 19:17:48 +00:00
|
|
|
'name': fields.char('Name', required=True, translate=True),
|
2014-03-09 18:58:47 +00:00
|
|
|
'faq': fields.html('Guidelines'),
|
2014-04-09 16:50:59 +00:00
|
|
|
'description': fields.text('Description'),
|
2014-02-22 17:19:37 +00:00
|
|
|
}
|
2014-04-09 16:50:59 +00:00
|
|
|
|
|
|
|
def _get_default_faq(self, cr, uid, context=None):
|
2014-03-09 18:41:19 +00:00
|
|
|
fname = openerp.modules.get_module_resource('website_forum', 'data', 'forum_default_faq.html')
|
|
|
|
with open(fname, 'r') as f:
|
|
|
|
return f.read()
|
|
|
|
return False
|
|
|
|
|
|
|
|
_defaults = {
|
2014-04-09 16:50:59 +00:00
|
|
|
'description': 'This community is for professionals and enthusiasts of our products and services.',
|
2014-03-09 18:58:47 +00:00
|
|
|
'faq': _get_default_faq,
|
2014-03-09 18:41:19 +00:00
|
|
|
}
|
2014-02-22 06:27:15 +00:00
|
|
|
|
2014-04-09 16:50:59 +00:00
|
|
|
|
2014-02-22 06:02:01 +00:00
|
|
|
class Post(osv.Model):
|
2014-02-22 17:19:37 +00:00
|
|
|
_name = 'website.forum.post'
|
2014-02-22 06:02:01 +00:00
|
|
|
_description = "Question"
|
2014-02-21 13:16:08 +00:00
|
|
|
_inherit = ['mail.thread', 'website.seo.metadata']
|
2014-03-07 11:03:29 +00:00
|
|
|
|
2014-03-06 13:17:36 +00:00
|
|
|
def _get_vote_count(self, cr, uid, ids, field_name, arg, context):
|
2014-03-06 09:40:42 +00:00
|
|
|
res = dict.fromkeys(ids, 0)
|
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
if post.vote_ids:
|
|
|
|
for vote in post.vote_ids:
|
2014-03-13 08:41:30 +00:00
|
|
|
res[post.id] += int(vote.vote)
|
2014-03-06 09:40:42 +00:00
|
|
|
return res
|
2014-02-21 13:16:08 +00:00
|
|
|
|
2014-03-11 09:25:17 +00:00
|
|
|
def _get_vote(self, cr, uid, ids, context=None):
|
|
|
|
result = {}
|
2014-03-26 10:26:54 +00:00
|
|
|
for vote in self.pool['website.forum.post.vote'].browse(cr, uid, ids, context=context):
|
2014-03-11 09:25:17 +00:00
|
|
|
result[vote.post_id.id] = True
|
|
|
|
return result.keys()
|
|
|
|
|
2014-03-12 09:16:02 +00:00
|
|
|
def _get_child_count(self, cr, uid, ids, field_name=False, arg={}, context=None):
|
|
|
|
res = dict.fromkeys(ids, 0)
|
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
if post.parent_id:
|
|
|
|
res[post.parent_id.id] = len(post.parent_id.child_ids)
|
2014-04-02 12:04:22 +00:00
|
|
|
else:
|
|
|
|
res[post.id] = len(post.child_ids)
|
2014-03-12 09:16:02 +00:00
|
|
|
return res
|
|
|
|
|
2014-04-07 07:20:32 +00:00
|
|
|
def _get_user_vote(self, cr, uid, ids, field_name, arg, context):
|
|
|
|
res = dict.fromkeys(ids, 0)
|
2014-04-09 16:50:59 +00:00
|
|
|
vote_ids = self.pool['website.forum.post.vote'].search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
|
|
|
|
for vote in self.pool['website.forum.post.vote'].browse(cr, uid, vote_ids, context=context):
|
|
|
|
res[vote.post_id.id] = vote.vote
|
2014-04-07 07:20:32 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
def _get_user_favourite(self, cr, uid, ids, field_name, arg, context):
|
|
|
|
res = dict.fromkeys(ids, False)
|
|
|
|
user = self.pool['res.users'].browse(cr, uid, uid, context=context)
|
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
if user in post.favourite_ids:
|
|
|
|
res[post.id] = True
|
|
|
|
return res
|
|
|
|
|
2014-02-21 13:16:08 +00:00
|
|
|
_columns = {
|
2014-03-09 18:41:19 +00:00
|
|
|
'name': fields.char('Title', size=128),
|
2014-03-03 19:17:48 +00:00
|
|
|
'forum_id': fields.many2one('website.forum', 'Forum', required=True),
|
2014-03-09 18:41:19 +00:00
|
|
|
'content': fields.text('Content'),
|
2014-02-22 06:02:01 +00:00
|
|
|
'create_date': fields.datetime('Asked on', select=True, readonly=True),
|
2014-04-09 16:50:59 +00:00
|
|
|
'user_id': fields.many2one('res.users', 'Asked by', select=True, readonly=True),
|
|
|
|
'write_date': fields.datetime('Update on', select=True, readonly=True),
|
2014-02-22 06:02:01 +00:00
|
|
|
'write_uid': fields.many2one('res.users', 'Update by', select=True, readonly=True),
|
2014-04-09 16:50:59 +00:00
|
|
|
'tag_ids': fields.many2many('website.forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', 'Tag'),
|
|
|
|
'vote_ids': fields.one2many('website.forum.post.vote', 'post_id', 'Votes'),
|
|
|
|
'user_vote': fields.function(_get_user_vote, string="My Vote", type='integer'),
|
2014-02-22 06:02:01 +00:00
|
|
|
'favourite_ids': fields.many2many('res.users', 'forum_favourite_rel', 'forum_id', 'user_id', 'Favourite'),
|
2014-04-09 16:50:59 +00:00
|
|
|
'user_favourite': fields.function(_get_user_favourite, string="My Favourite", type='boolean'),
|
|
|
|
'state': fields.selection([('active', 'Active'), ('close', 'Close'), ('offensive', 'Offensive')], 'Status'),
|
2014-02-22 06:02:01 +00:00
|
|
|
'active': fields.boolean('Active'),
|
2014-04-09 16:50:59 +00:00
|
|
|
'views': fields.integer('Number of Views'),
|
2014-03-11 09:12:28 +00:00
|
|
|
'parent_id': fields.many2one('website.forum.post', 'Question', ondelete='cascade'),
|
2014-03-09 18:41:19 +00:00
|
|
|
'child_ids': fields.one2many('website.forum.post', 'parent_id', 'Answers'),
|
2014-04-09 16:50:59 +00:00
|
|
|
'child_count': fields.function(
|
|
|
|
_get_child_count, string="Answers", type='integer',
|
2014-03-12 09:16:02 +00:00
|
|
|
store={
|
2014-04-09 16:50:59 +00:00
|
|
|
'website.forum.post': (lambda self, cr, uid, ids, c={}: ids, ['parent_id', 'child_ids'], 10),
|
2014-03-12 09:16:02 +00:00
|
|
|
}
|
|
|
|
),
|
2014-02-21 13:16:08 +00:00
|
|
|
'website_message_ids': fields.one2many(
|
|
|
|
'mail.message', 'res_id',
|
|
|
|
domain=lambda self: [
|
|
|
|
'&', ('model', '=', self._name), ('type', '=', 'comment')
|
|
|
|
],
|
2014-02-22 06:02:01 +00:00
|
|
|
string='Post Messages',
|
|
|
|
help="Comments on forum post",
|
2014-02-21 13:16:08 +00:00
|
|
|
),
|
2014-04-09 16:50:59 +00:00
|
|
|
'vote_count': fields.function(
|
|
|
|
_get_vote_count, string="Votes", type='integer',
|
2014-03-11 09:25:17 +00:00
|
|
|
store={
|
|
|
|
'website.forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
|
|
|
|
'website.forum.post.vote': (_get_vote, [], 10),
|
|
|
|
}
|
|
|
|
),
|
2014-04-09 16:50:59 +00:00
|
|
|
'is_correct': fields.boolean('Valid Answer', help='Correct Answer/ Answer on this question accepted.'),
|
|
|
|
'closed_reason_id': fields.many2one('website.forum.post.reason', 'Reason'),
|
2014-03-21 12:57:47 +00:00
|
|
|
'closed_by': fields.many2one('res.users', 'Closed by'),
|
|
|
|
'closed_date': fields.datetime('Closed on', readonly=True),
|
2014-02-21 13:16:08 +00:00
|
|
|
}
|
2014-04-09 16:50:59 +00:00
|
|
|
|
2014-02-21 13:16:08 +00:00
|
|
|
_defaults = {
|
|
|
|
'state': 'active',
|
2014-04-09 17:25:22 +00:00
|
|
|
'views': 0,
|
2014-03-11 09:25:17 +00:00
|
|
|
'vote_count': 0,
|
2014-03-12 09:16:02 +00:00
|
|
|
'active': True,
|
2014-02-21 13:16:08 +00:00
|
|
|
}
|
2014-03-05 13:07:24 +00:00
|
|
|
|
2014-02-23 07:15:16 +00:00
|
|
|
def create(self, cr, uid, vals, context=None):
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
create_context = dict(context, mail_create_nolog=True)
|
2014-04-09 16:50:59 +00:00
|
|
|
post_id = super(Post, self).create(cr, uid, vals, context=create_context)
|
|
|
|
# post message + subtype depending on parent_id
|
2014-03-11 12:59:49 +00:00
|
|
|
if vals.get("parent_id"):
|
2014-04-09 16:50:59 +00:00
|
|
|
name, body, subtype = vals.get('name'), 'New Answer', 'website_forum.mt_answer_new'
|
|
|
|
else:
|
|
|
|
name, body, subtype = vals.get('name'), 'Post Created', 'website_forum.mt_question_new'
|
2014-03-20 13:00:14 +00:00
|
|
|
#add 2 karma to user when asks question.
|
2014-03-26 10:26:54 +00:00
|
|
|
self.pool['res.users'].write(cr, SUPERUSER_ID, [vals.get('user_id')], {'karma': 2}, context=context)
|
2014-03-24 06:36:01 +00:00
|
|
|
self.message_post(cr, uid, [post_id], body=_(body), subtype=subtype, context=context)
|
2014-02-23 07:15:16 +00:00
|
|
|
return post_id
|
|
|
|
|
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
2014-04-09 16:50:59 +00:00
|
|
|
res = super(Post, self).write(cr, uid, ids, vals, context=context)
|
|
|
|
# if post content modify, notify followers
|
|
|
|
if 'content' in vals or 'name' in vals:
|
2014-03-13 08:41:30 +00:00
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
if post.parent_id:
|
2014-04-09 16:50:59 +00:00
|
|
|
body, subtype = 'Answer Edited', 'website_forum.mt_answer_edit'
|
|
|
|
else:
|
|
|
|
body, subtype = 'Question Edited', 'website_forum.mt_question_edit'
|
2014-03-13 08:41:30 +00:00
|
|
|
self.message_post(cr, uid, [post.id], body=_(body), subtype=subtype, context=context)
|
2014-04-09 16:50:59 +00:00
|
|
|
# update karma of related user when any answer accepted
|
|
|
|
if vals.get('correct'):
|
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
value = -15
|
2014-04-01 11:57:27 +00:00
|
|
|
if vals.get('correct'):
|
|
|
|
value = 15
|
|
|
|
self.pool['res.users'].write(cr, SUPERUSER_ID, [post.user_id.id], {'karma': value}, context=context)
|
2014-04-09 16:50:59 +00:00
|
|
|
return res
|
|
|
|
|
2014-04-09 17:39:41 +00:00
|
|
|
def vote(self, cr, uid, ids, upvote=True, context=None):
|
|
|
|
Vote = self.pool['website.forum.post.vote']
|
|
|
|
user = self.pool['res.users'].browse(cr, uid, uid, context=context)
|
|
|
|
# must have at least 10 karma to vote
|
|
|
|
if not upvote and user.karma <= 10:
|
|
|
|
return {'error': 'lessthen_10_karma'}
|
|
|
|
# user can not vote on own post
|
|
|
|
posts = self.pool['website.forum.post'].browse(cr, uid, ids, context=context)
|
|
|
|
if any(post.user_id.id == uid for post in posts):
|
|
|
|
return {'error': 'own_post'}
|
|
|
|
|
|
|
|
vote_ids = Vote.search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
|
|
|
|
if vote_ids:
|
|
|
|
for vote in Vote.browse(cr, uid, vote_ids, context=context):
|
|
|
|
if upvote:
|
|
|
|
new_vote = '0' if vote.vote == '-1' else '1'
|
|
|
|
else:
|
|
|
|
new_vote = '0' if vote.vote == '1' else '-1'
|
|
|
|
Vote.write(cr, uid, vote_ids, {'vote': new_vote}, context=context)
|
|
|
|
else:
|
|
|
|
for post_id in ids:
|
|
|
|
new_vote = '1' if upvote else '-1'
|
|
|
|
Vote.create(cr, uid, {'post_id': post_id, 'vote': new_vote}, context=context)
|
|
|
|
return {'vote_count': self._get_vote_count(cr, uid, ids, None, None, context=context)[ids[0]]}
|
2014-02-21 13:16:08 +00:00
|
|
|
|
2014-04-09 17:25:22 +00:00
|
|
|
def set_viewed(self, cr, uid, ids, context=None):
|
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
self.write(cr, uid, [post.id], {'views': post.views + 1}, context=context)
|
|
|
|
return True
|
|
|
|
|
2014-03-25 11:34:09 +00:00
|
|
|
|
2014-03-24 07:11:52 +00:00
|
|
|
class PostReason(osv.Model):
|
|
|
|
_name = "website.forum.post.reason"
|
|
|
|
_description = "Post Reason"
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Post Reason'),
|
|
|
|
}
|
|
|
|
|
2014-04-09 16:50:59 +00:00
|
|
|
|
2014-02-22 06:27:15 +00:00
|
|
|
class Users(osv.Model):
|
|
|
|
_inherit = 'res.users'
|
2014-03-07 09:34:30 +00:00
|
|
|
|
|
|
|
def _get_user_badge_level(self, cr, uid, ids, name, args, context=None):
|
|
|
|
"""Return total badge per level of users"""
|
|
|
|
result = dict.fromkeys(ids, False)
|
2014-03-26 10:26:54 +00:00
|
|
|
badge_user_obj = self.pool['gamification.badge.user']
|
2014-03-07 09:34:30 +00:00
|
|
|
for id in ids:
|
|
|
|
result[id] = {
|
|
|
|
'gold_badge': badge_user_obj.search(cr, uid, [('badge_id.level', '=', 'gold'), ('user_id', '=', id)], context=context, count=True),
|
|
|
|
'silver_badge': badge_user_obj.search(cr, uid, [('badge_id.level', '=', 'silver'), ('user_id', '=', id)], context=context, count=True),
|
|
|
|
'bronze_badge': badge_user_obj.search(cr, uid, [('badge_id.level', '=', 'bronze'), ('user_id', '=', id)], context=context, count=True),
|
|
|
|
}
|
|
|
|
return result
|
2014-03-11 12:59:49 +00:00
|
|
|
|
2014-02-22 06:27:15 +00:00
|
|
|
_columns = {
|
2014-03-03 11:06:20 +00:00
|
|
|
'create_date': fields.datetime('Create Date', select=True, readonly=True),
|
2014-04-09 16:50:59 +00:00
|
|
|
# 'is_forum': fields.boolean('Is Forum Member'),
|
|
|
|
'karma': fields.integer('Karma'),
|
|
|
|
'badge_ids': fields.one2many('gamification.badge.user', 'user_id', 'Badges'),
|
|
|
|
'gold_badge': fields.function(_get_user_badge_level, string="Number of gold badges", type='integer', multi='badge_level'),
|
|
|
|
'silver_badge': fields.function(_get_user_badge_level, string="Number of silver badges", type='integer', multi='badge_level'),
|
|
|
|
'bronze_badge': fields.function(_get_user_badge_level, string="Number of bronze badges", type='integer', multi='badge_level'),
|
2014-02-22 06:27:15 +00:00
|
|
|
}
|
2014-04-09 16:50:59 +00:00
|
|
|
|
2014-03-03 20:34:25 +00:00
|
|
|
_defaults = {
|
2014-04-09 16:50:59 +00:00
|
|
|
# 'is_forum': False,
|
|
|
|
'karma': 0,
|
2014-03-03 20:34:25 +00:00
|
|
|
}
|
2014-02-22 06:27:15 +00:00
|
|
|
|
2014-04-09 16:50:59 +00:00
|
|
|
def add_karma(self, cr, uid, ids, karma, context=None):
|
|
|
|
for user in self.browse(cr, uid, ids, context=context):
|
|
|
|
self.write(cr, uid, [user.id], {'karma': user.karma + karma}, context=context)
|
|
|
|
return True
|
|
|
|
|
2014-02-22 06:02:01 +00:00
|
|
|
|
|
|
|
class Vote(osv.Model):
|
2014-02-22 06:27:15 +00:00
|
|
|
_name = 'website.forum.post.vote'
|
2014-02-22 17:19:37 +00:00
|
|
|
_description = 'Vote'
|
|
|
|
_columns = {
|
2014-03-25 06:48:04 +00:00
|
|
|
'post_id': fields.many2one('website.forum.post', 'Post', ondelete='cascade', required=True),
|
2014-02-22 06:02:01 +00:00
|
|
|
'user_id': fields.many2one('res.users', 'User'),
|
2014-04-09 16:50:59 +00:00
|
|
|
'vote': fields.selection([('1', '1'), ('-1', '-1'), ('0', '0')], 'Vote', required=True),
|
2014-03-20 08:51:49 +00:00
|
|
|
'create_date': fields.datetime('Create Date', select=True, readonly=True),
|
2014-02-22 06:02:01 +00:00
|
|
|
}
|
2014-03-12 08:46:47 +00:00
|
|
|
_defaults = {
|
|
|
|
'user_id': lambda self, cr, uid, ctx: uid,
|
2014-04-09 16:50:59 +00:00
|
|
|
'vote': lambda *args: '1',
|
2014-03-12 08:46:47 +00:00
|
|
|
}
|
2014-03-13 08:41:30 +00:00
|
|
|
|
2014-04-09 16:50:59 +00:00
|
|
|
def update_karma(self, cr, uid, ids, new_vote='0', old_vote='0', context=None):
|
|
|
|
karma_value = (int(new_vote) - int(old_vote)) * 10
|
|
|
|
if karma_value:
|
|
|
|
for vote in self.browse(cr, uid, ids, context=context):
|
|
|
|
self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [vote.post_id.user_id.id], karma_value, context=context)
|
|
|
|
return True
|
|
|
|
|
2014-03-05 13:07:24 +00:00
|
|
|
def create(self, cr, uid, vals, context=None):
|
|
|
|
vote_id = super(Vote, self).create(cr, uid, vals, context=context)
|
2014-04-09 16:50:59 +00:00
|
|
|
self.update_karma(cr, uid, [vote_id], new_vote=vals.get('vote', '1'), context=context)
|
|
|
|
# body = "voted %s %s" % ('answer' if record.parent_id else 'question','up' if vals.get('vote')==1 else 'down')
|
|
|
|
# Post.message_post(cr, uid, [record.id], body=_(body), context=context)
|
2014-03-05 13:07:24 +00:00
|
|
|
return vote_id
|
|
|
|
|
2014-04-09 16:50:59 +00:00
|
|
|
def write(self, cr, uid, ids, values, context=None):
|
|
|
|
res = super(Vote, self).write(cr, uid, ids, values, context=context)
|
|
|
|
if 'vote' in values:
|
|
|
|
for vote in self.browse(cr, uid, ids, context=context):
|
|
|
|
self.update_karma(cr, uid, ids, new_vote=values['vote'], old_vote=vote.vote, context=context)
|
|
|
|
return res
|
|
|
|
|
2014-03-18 09:01:54 +00:00
|
|
|
|
2014-02-22 06:02:01 +00:00
|
|
|
class Tags(osv.Model):
|
2014-02-21 13:16:08 +00:00
|
|
|
_name = "website.forum.tag"
|
2014-02-22 06:27:15 +00:00
|
|
|
_description = "Tag"
|
2014-02-21 13:16:08 +00:00
|
|
|
_inherit = ['website.seo.metadata']
|
2014-03-19 13:04:13 +00:00
|
|
|
|
2014-03-12 08:46:47 +00:00
|
|
|
def _get_posts_count(self, cr, uid, ids, field_name, arg, context=None):
|
2014-04-09 16:50:59 +00:00
|
|
|
return dict((tag_id, self.pool['website.forum.post'].search_count(cr, uid, [('tag_ids', 'in', tag_id)], context=context)) for tag_id in ids)
|
2014-03-11 09:25:17 +00:00
|
|
|
|
2014-04-09 16:50:59 +00:00
|
|
|
def _get_tag_from_post(self, cr, uid, ids, context=None):
|
|
|
|
return list(set(
|
|
|
|
[tag.id for post in self.pool['website.forum.post'].browse(cr, SUPERUSER_ID, ids, context=context) for tag in post.tag_ids]
|
|
|
|
))
|
2014-03-19 13:04:13 +00:00
|
|
|
|
2014-02-21 13:16:08 +00:00
|
|
|
_columns = {
|
2014-02-22 20:26:18 +00:00
|
|
|
'name': fields.char('Name', size=64, required=True),
|
2014-03-11 09:25:17 +00:00
|
|
|
'forum_id': fields.many2one('website.forum', 'Forum', required=True),
|
2014-04-09 16:50:59 +00:00
|
|
|
'posts_count': fields.function(
|
|
|
|
_get_posts_count, type='integer', string="# of Posts",
|
2014-03-19 13:04:13 +00:00
|
|
|
store={
|
2014-04-09 16:50:59 +00:00
|
|
|
'website.forum.post': (_get_tag_from_post, ['tag_ids'], 10),
|
2014-03-19 13:04:13 +00:00
|
|
|
}
|
|
|
|
),
|
2014-04-09 16:50:59 +00:00
|
|
|
}
|