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),
|
2014-03-09 18:58:47 +00:00
|
|
|
'faq': fields.html('Guidelines'),
|
|
|
|
'right_column': fields.html('Right Column'),
|
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 = {
|
2014-03-09 18:58:47 +00:00
|
|
|
'faq': _get_default_faq,
|
|
|
|
'right_column': """<div class="panel panel-default">
|
|
|
|
<div class="panel-heading">
|
|
|
|
<h3 class="panel-title">About This Forum</h3>
|
|
|
|
</div>
|
|
|
|
<div class="panel-body">
|
|
|
|
This community is for professional and enthusiast about our
|
|
|
|
products and services.<br/>
|
|
|
|
<a t-attf-href="/forum/1/faq" class="fa fa-arrow-right"> Read Guidelines</a>
|
|
|
|
</div>
|
|
|
|
</div>"""
|
2014-03-09 18:41:19 +00:00
|
|
|
}
|
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
|
|
|
|
2014-03-11 09:25:17 +00:00
|
|
|
def _get_vote(self, cr, uid, ids, context=None):
|
|
|
|
result = {}
|
|
|
|
for vote in self.pool.get('website.forum.post.vote').browse(cr, uid, ids, context=context):
|
|
|
|
result[vote.post_id.id] = True
|
|
|
|
return result.keys()
|
|
|
|
|
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-11 09:25:17 +00:00
|
|
|
'user_vote':fields.function(_get_votes, string="My Vote", type='boolean',
|
|
|
|
store={
|
|
|
|
'website.forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
|
|
|
|
'website.forum.post.vote': (_get_vote, [], 10),
|
|
|
|
}
|
|
|
|
),
|
2014-03-09 18:41:19 +00:00
|
|
|
|
2014-03-11 09:25:17 +00:00
|
|
|
'vote_count':fields.function(_get_vote_count, string="Votes", type='integer',
|
|
|
|
store={
|
|
|
|
'website.forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
|
|
|
|
'website.forum.post.vote': (_get_vote, [], 10),
|
|
|
|
}
|
|
|
|
),
|
2014-02-21 13:16:08 +00:00
|
|
|
}
|
|
|
|
_defaults = {
|
|
|
|
'state': 'active',
|
2014-03-11 09:25:17 +00:00
|
|
|
'vote_count': 0,
|
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-12 08:46:47 +00:00
|
|
|
hist_obj = self.pool['website.forum.post.history']
|
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
hist_obj.create(cr, uid, {
|
|
|
|
'post_id': post.id,
|
|
|
|
'content': post.content,
|
|
|
|
'name': post.name,
|
|
|
|
'tags': [(6,0, [x.id for x in post.tags])],
|
|
|
|
'date': post.write_date or post.create_date,
|
|
|
|
'user_id': post.write_uid and post.write_uid.id or post.create_uid.id
|
|
|
|
}, context=context)
|
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)
|
|
|
|
post_id = super(Post, self).create(cr, uid, vals, context=create_context)
|
2014-03-11 12:59:49 +00:00
|
|
|
body = "asked a question"
|
|
|
|
if vals.get("parent_id"):
|
|
|
|
body = "answered a question"
|
|
|
|
self.message_post(cr, uid, [post_id], body=body, 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-11 12:59:49 +00:00
|
|
|
for post in self.browse(cr, uid, ids, context=context):
|
|
|
|
body = "edited question"
|
|
|
|
if post.parent_id:
|
|
|
|
body = "edited answer"
|
|
|
|
self.message_post(cr, uid, ids, body=body, context=context)
|
2014-02-23 07:15:16 +00:00
|
|
|
return result
|
2014-02-21 13:16:08 +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)
|
|
|
|
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-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-03-12 08:46:47 +00:00
|
|
|
'karma': fields.integer('Karma'), # Use a function field 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-12 08:46:47 +00:00
|
|
|
'name': fields.char('Post Title')
|
2014-03-05 13:07:24 +00:00
|
|
|
'post_id': fields.many2one('website.forum.post', 'Post', ondelete='cascade'),
|
2014-03-12 08:46:47 +00:00
|
|
|
'date': fields.datetime('Created on', select=True, readonly=True),
|
|
|
|
'user_id': fields.many2one('res.users', 'Created by', select=True, readonly=True),
|
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-12 08:46:47 +00:00
|
|
|
'vote': fields.selection([('1', '1'),('-1', '-1'),('0','0')], 'Vote'),
|
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,
|
|
|
|
'vote': lambda *args: 1
|
|
|
|
}
|
|
|
|
# TODO: improve this: translate strings _()
|
|
|
|
# no need to have different text for question/answer
|
|
|
|
# need different text for upvote, downvote
|
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-11 12:59:49 +00:00
|
|
|
Post = self.pool["website.forum.post"]
|
|
|
|
record = Post.browse(cr, uid, vals.get('post_id'), context=context)
|
|
|
|
body = "voted question"
|
|
|
|
if record.parent_id:
|
|
|
|
body = "voted answer"
|
|
|
|
Post.message_post(cr, uid, [record.id], body=body, context=context)
|
2014-03-05 13:07:24 +00:00
|
|
|
return vote_id
|
|
|
|
|
2014-03-12 08:46:47 +00:00
|
|
|
def vote(self, cr, uid, post_id, vote, context=None):
|
|
|
|
assert int(vote) in (1, -1, 0), "vote can be -1 or 1, nothing else"
|
|
|
|
post_obj = self.pool.get('website.forum.post')
|
|
|
|
vote_ids = self.search(cr, uid, [('post_id', '=', post_id), ('user_id','=',uid)], context=context)
|
|
|
|
if vote_ids:
|
|
|
|
self.write(cr, uid, vote_uid, {
|
|
|
|
'vote': vote)
|
|
|
|
}, context=context)
|
|
|
|
else:
|
|
|
|
self.create(cr, uid, {
|
|
|
|
'post_id': post_id,
|
|
|
|
'vote': vote,
|
|
|
|
}, context=context)
|
|
|
|
return post_obj.browse(cr, uid, post_id, context=context).vote_count
|
|
|
|
|
2014-03-07 09:34:30 +00:00
|
|
|
class Badge(osv.Model):
|
|
|
|
_inherit = 'gamification.badge'
|
|
|
|
_columns = {
|
2014-03-12 08:46:47 +00:00
|
|
|
# TODO: remove this forum field and use level is not False instead
|
2014-03-08 12:01:43 +00:00
|
|
|
'forum': fields.boolean('Is a Forum Badge'),
|
2014-03-12 08:46:47 +00:00
|
|
|
'level': fields.selection([('bronze', 'bronze'), ('silver', 'silver'), ('gold', 'gold')], 'Forum Badge Level'),
|
2014-03-07 09:34:30 +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-12 08:46:47 +00:00
|
|
|
def _get_posts_count(self, cr, uid, ids, field_name, arg, context=None):
|
2014-03-11 09:25:17 +00:00
|
|
|
result = {}
|
|
|
|
Post = self.pool['website.forum.post']
|
|
|
|
for tag in ids:
|
2014-03-12 08:46:47 +00:00
|
|
|
result[tag] = Post.search_count(cr, uid , [('tags', '=', tag)], context=context)
|
2014-03-11 09:25:17 +00:00
|
|
|
return result
|
|
|
|
|
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-03-12 08:46:47 +00:00
|
|
|
'posts_count': fields.function(_get_posts_count, type='integer', string="# of Posts"),
|
2014-02-22 17:19:37 +00:00
|
|
|
}
|