2014-02-21 13:16:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2014-02-22 17:19:37 +00:00
|
|
|
import re
|
2014-02-21 13:16:08 +00:00
|
|
|
|
2014-03-09 18:41:19 +00:00
|
|
|
import openerp
|
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 _
|
|
|
|
|
|
|
|
from openerp.addons.website.models.website import slug
|
|
|
|
|
2014-02-22 06:27:15 +00:00
|
|
|
#TODO: Do we need a forum object like blog object ? Need to check with BE team
|
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-04 10:49:30 +00:00
|
|
|
_columns = {
|
2014-03-03 19:17:48 +00:00
|
|
|
'name': fields.char('Name', required=True, translate=True),
|
|
|
|
'faq': fields.html('FAQ'),
|
|
|
|
'right_column': fields.html('FAQ'),
|
2014-02-22 17:19:37 +00:00
|
|
|
}
|
2014-03-09 18:41:19 +00:00
|
|
|
def _get_default_faq(self, cr, uid, context={}):
|
|
|
|
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 = {
|
|
|
|
'faq': _get_default_faq
|
|
|
|
}
|
2014-02-22 06:27:15 +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
|
|
|
_order = "id desc"
|
|
|
|
|
2014-03-06 09:40:42 +00:00
|
|
|
def _get_votes(self, cr, uid, ids, field_name, arg, context):
|
|
|
|
res = dict.fromkeys(ids, False)
|
2014-03-09 18:41:19 +00:00
|
|
|
# TODO: implement this with a read_group call instead of browsing all records
|
2014-03-06 09:40:42 +00:00
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
if post.vote_ids:
|
|
|
|
for vote in post.vote_ids:
|
|
|
|
if vote.user_id.id == uid:
|
|
|
|
if vote.vote == '1':
|
|
|
|
res[post.id] = 1
|
|
|
|
else:
|
|
|
|
res[post.id] = -1
|
|
|
|
return res
|
|
|
|
|
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:
|
|
|
|
if vote.vote == '1':
|
|
|
|
res[post.id] += 1
|
|
|
|
else:
|
|
|
|
res[post.id] -= 1
|
|
|
|
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),
|
|
|
|
'create_uid': fields.many2one('res.users', 'Asked by', select=True, readonly=True ),
|
|
|
|
'write_date': fields.datetime('Update on', select=True, readonly=True ),
|
|
|
|
'write_uid': fields.many2one('res.users', 'Update by', select=True, readonly=True),
|
2014-03-06 11:28:16 +00:00
|
|
|
|
2014-02-21 13:16:08 +00:00
|
|
|
'tags': fields.many2many('website.forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', 'Tag'),
|
2014-02-22 17:19:37 +00:00
|
|
|
'vote_ids':fields.one2many('website.forum.post.vote', 'post_id', 'Vote'),
|
2014-03-06 11:28:16 +00:00
|
|
|
|
2014-02-22 06:02:01 +00:00
|
|
|
'favourite_ids': fields.many2many('res.users', 'forum_favourite_rel', 'forum_id', 'user_id', 'Favourite'),
|
2014-03-06 11:28:16 +00:00
|
|
|
|
2014-02-21 13:16:08 +00:00
|
|
|
'state': fields.selection([('active', 'Active'),('close', 'Close'),('offensive', 'Offensive')], 'Status'),
|
2014-02-22 06:02:01 +00:00
|
|
|
'active': fields.boolean('Active'),
|
2014-03-09 18:41:19 +00:00
|
|
|
'views': fields.integer('Page Views'),
|
2014-03-06 11:28:16 +00:00
|
|
|
|
2014-03-09 18:41:19 +00:00
|
|
|
'parent_id': fields.many2one('website.forum.post', 'Question'),
|
|
|
|
'child_ids': fields.one2many('website.forum.post', 'parent_id', 'Answers'),
|
2014-03-06 11:28:16 +00:00
|
|
|
|
|
|
|
'history_ids': fields.one2many('blog.post.history', 'post_id', 'History', help='Last post modifications'),
|
2014-02-21 13:16:08 +00:00
|
|
|
# TODO FIXME: when website_mail/mail_thread.py inheritance work -> this field won't be necessary
|
|
|
|
'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-03-06 09:40:42 +00:00
|
|
|
|
2014-03-09 18:41:19 +00:00
|
|
|
# TODO: add a store={} on those two fields. Why is it a boolean?
|
|
|
|
'user_vote':fields.function(_get_votes, string="My Vote", type='boolean'),
|
|
|
|
|
|
|
|
# TODO: add a store={} on those two fields
|
|
|
|
'vote_count':fields.function(_get_vote_count, string="Votes", type='integer'),
|
2014-02-21 13:16:08 +00:00
|
|
|
}
|
|
|
|
_defaults = {
|
|
|
|
'state': 'active',
|
2014-02-22 06:02:01 +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_history(self, cr, uid, ids, vals, context=None):
|
2014-03-05 13:07:24 +00:00
|
|
|
History = self.pool['website.forum.post.history']
|
2014-03-03 09:06:16 +00:00
|
|
|
for post in ids:
|
2014-02-23 07:15:16 +00:00
|
|
|
if vals.get('content'):
|
|
|
|
create_date = vals.get('create_date')
|
|
|
|
res = {
|
|
|
|
'name': 'Update %s - %s' % (create_date, vals.get('name')),
|
|
|
|
'content': vals.get('content', ''),
|
2014-03-03 09:06:16 +00:00
|
|
|
'post_id': post
|
2014-02-23 07:15:16 +00:00
|
|
|
}
|
|
|
|
if vals.get('version'):
|
|
|
|
res.update({'version':vals.get('version')})
|
2014-03-05 13:07:24 +00:00
|
|
|
|
2014-02-23 07:15:16 +00:00
|
|
|
if vals.get('tags'):
|
|
|
|
res.update({'tags':vals.get('tags')})
|
2014-03-05 13:07:24 +00:00
|
|
|
|
|
|
|
History.create(cr, uid, res, context=context)
|
|
|
|
|
|
|
|
def create_activity(self, cr, uid, ids, method, context=None):
|
|
|
|
Activity = self.pool['website.forum.activity']
|
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
res = {
|
|
|
|
'name': post.name,
|
|
|
|
'post_id': post.id,
|
|
|
|
'user_id': uid,
|
|
|
|
}
|
|
|
|
|
|
|
|
if post.parent_id:
|
|
|
|
res.update({'type': 'edited_answer'})
|
|
|
|
if method == 'create':
|
|
|
|
res.update({'type': 'answered_question'})
|
|
|
|
if method == 'unlink':
|
|
|
|
res.update({'type': 'deleted_answer'})
|
|
|
|
if method == 'vote':
|
|
|
|
res.update({'type': 'voted_answer'})
|
|
|
|
|
|
|
|
else:
|
|
|
|
res.update({'type': 'edited_question'})
|
|
|
|
if method == 'create':
|
|
|
|
res.update({'type': 'asked_question'})
|
|
|
|
if method == 'unlink':
|
|
|
|
res.update({'type': 'deleted_question'})
|
|
|
|
if method == 'vote':
|
2014-03-06 09:40:42 +00:00
|
|
|
res.update({'type': 'voted_question'})
|
2014-03-05 13:07:24 +00:00
|
|
|
|
|
|
|
Activity.create(cr, uid, res, context=context)
|
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)
|
|
|
|
post_id = super(Post, self).create(cr, uid, vals, context=create_context)
|
2014-03-05 13:07:24 +00:00
|
|
|
self.create_activity(cr, uid, [post_id], method='create', context=context)
|
2014-02-23 07:15:16 +00:00
|
|
|
return post_id
|
|
|
|
|
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
2014-03-05 13:07:24 +00:00
|
|
|
self.create_history(cr, uid, ids, vals, context=context)
|
2014-03-09 18:41:19 +00:00
|
|
|
result = super(Post, self).write(cr, uid, ids, vals, context=context)
|
2014-03-05 13:07:24 +00:00
|
|
|
self.create_activity(cr, uid, ids, method='write', context=context)
|
2014-02-23 07:15:16 +00:00
|
|
|
return result
|
2014-02-21 13:16:08 +00:00
|
|
|
|
2014-03-05 13:07:24 +00:00
|
|
|
def unlink(self, cr, uid, ids, context=None):
|
|
|
|
self.create_activity(cr, uid, ids, method='unlink', context=context)
|
|
|
|
return super(Post, self).unlink(cr, uid, ids, context=context)
|
|
|
|
|
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)
|
|
|
|
badge_user_obj = self.pool.get('gamification.badge.user')
|
|
|
|
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-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-03-03 20:34:25 +00:00
|
|
|
'karma': fields.integer('Karma'), # Use Gamification for this
|
2014-03-07 09:34:30 +00:00
|
|
|
'forum': fields.boolean('Is Forum Member'),
|
2014-03-03 19:17:48 +00:00
|
|
|
|
2014-03-07 09:34:30 +00:00
|
|
|
'badges': 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-03-03 20:34:25 +00:00
|
|
|
_defaults = {
|
2014-03-08 16:25:24 +00:00
|
|
|
'forum': False,
|
|
|
|
'karma': 0
|
2014-03-03 20:34:25 +00:00
|
|
|
}
|
2014-02-22 06:27:15 +00:00
|
|
|
|
2014-02-22 06:02:01 +00:00
|
|
|
class PostHistory(osv.Model):
|
|
|
|
_name = 'website.forum.post.history'
|
2014-02-22 06:27:15 +00:00
|
|
|
_description = 'Post History'
|
2014-02-22 06:02:01 +00:00
|
|
|
_inherit = ['website.seo.metadata']
|
|
|
|
_columns = {
|
2014-03-05 13:07:24 +00:00
|
|
|
'name': fields.char('Update Notes', size=64, required=True),
|
|
|
|
'post_id': fields.many2one('website.forum.post', 'Post', ondelete='cascade'),
|
2014-02-22 06:27:15 +00:00
|
|
|
'create_date': fields.datetime('Created on', select=True, readonly=True),
|
|
|
|
'create_uid': fields.many2one('res.users', 'Created by', select=True, readonly=True),
|
|
|
|
'version': fields.integer('Version'),
|
2014-02-23 07:15:16 +00:00
|
|
|
'content': fields.html('Contents', help='Automatically sanitized HTML contents'),
|
2014-02-22 06:02:01 +00:00
|
|
|
'tags': fields.many2many('website.forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', 'Tag'),
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 13:07:24 +00:00
|
|
|
'post_id': fields.many2one('website.forum.post', 'Post', required=True),
|
2014-02-22 06:02:01 +00:00
|
|
|
'user_id': fields.many2one('res.users', 'User'),
|
2014-03-06 09:40:42 +00:00
|
|
|
'vote': fields.selection([('1', '1'),('-1', '-1')], 'rate'),
|
2014-02-22 06:02:01 +00:00
|
|
|
}
|
|
|
|
|
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-03-06 09:40:42 +00:00
|
|
|
self.pool['website.forum.post'].create_activity(cr, uid, [int(vals.get('post_id'))], method='vote', context=context)
|
2014-03-05 13:07:24 +00:00
|
|
|
return vote_id
|
|
|
|
|
2014-03-07 09:34:30 +00:00
|
|
|
class Badge(osv.Model):
|
|
|
|
_inherit = 'gamification.badge'
|
|
|
|
_columns = {
|
2014-03-08 12:01:43 +00:00
|
|
|
'forum': fields.boolean('Is a Forum Badge'),
|
|
|
|
'level': fields.selection([('bronze', 'bronze'), ('silver', 'silver'), ('gold', 'gold')], 'Badge Level'),
|
2014-03-07 09:34:30 +00:00
|
|
|
}
|
|
|
|
_defaults = {
|
2014-03-08 12:01:43 +00:00
|
|
|
'forum': False,
|
|
|
|
'level': 'bronze'
|
2014-03-07 09:34:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 16:25:24 +00:00
|
|
|
|
|
|
|
# TODO:
|
|
|
|
# remove this object and replace by mail.message of type notes on related post
|
|
|
|
# type = message types
|
2014-02-22 20:26:18 +00:00
|
|
|
class ForumActivity(osv.Model):
|
|
|
|
_name = "website.forum.activity"
|
|
|
|
_description = "Activity"
|
2014-03-05 13:07:24 +00:00
|
|
|
_order = "id desc"
|
2014-02-22 20:26:18 +00:00
|
|
|
_columns = {
|
2014-03-05 13:07:24 +00:00
|
|
|
'name': fields.char('Name', size=64),
|
2014-02-22 20:26:18 +00:00
|
|
|
'post_id': fields.many2one('website.forum.post', 'Post'),
|
|
|
|
'user_id': fields.many2one('res.users', 'User'),
|
|
|
|
'create_date': fields.datetime('Created on', select=True, readonly=True),
|
2014-03-03 19:17:48 +00:00
|
|
|
# Use the gamification module instead!
|
2014-02-22 20:26:18 +00:00
|
|
|
'badge_id': fields.many2one('res.groups', 'Badge'),
|
2014-03-05 13:07:24 +00:00
|
|
|
#NOTE: we can create new ForumActivityType object instead of selection
|
|
|
|
'type': fields.selection([('asked_question', 'asked a question'), ('answered_question', 'answered a question'),
|
|
|
|
('edited_question', 'edited question'), ('edited_answer', 'edited answer'),
|
|
|
|
('commented_question', 'commented question'), ('commented_answer', 'commented answer'),
|
|
|
|
('deleted_question', 'deleted question'), ('deleted_answer', 'deleted answer'),
|
|
|
|
('voted_question', 'voted question'), ('voted_answer', 'voted answer'),
|
|
|
|
('received_badge', 'received badge'),
|
|
|
|
], 'Activity Type'),
|
2014-03-08 16:25:24 +00:00
|
|
|
# merge these 2 fields into one: karma: fields.integer (that can be positive or negative)
|
2014-02-22 20:26:18 +00:00
|
|
|
'karma_add': fields.integer('Added Karma'),
|
|
|
|
'karma_sub': fields.integer('Karma Removed')
|
|
|
|
}
|
|
|
|
|
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']
|
|
|
|
_columns = {
|
2014-02-22 20:26:18 +00:00
|
|
|
'name': fields.char('Name', size=64, required=True),
|
2014-02-22 06:27:15 +00:00
|
|
|
'post_ids': fields.many2many('website.forum.post', 'forum_tag_que_rel', 'tag_id', 'forum_id', 'Questions', readonly=True),
|
2014-03-03 19:17:48 +00:00
|
|
|
'forum_id': fields.many2one('website.forum', 'Forum', required=True)
|
2014-02-22 17:19:37 +00:00
|
|
|
}
|