from urllib2 import urlopen
from openerp import tools
-from openerp import SUPERUSER_ID
from openerp.addons.web import http
from openerp.addons.web.controllers.main import login_redirect
from openerp.addons.web.http import request
_user_per_page = 30
def _get_notifications(self):
- cr, uid, context = request.cr, request.uid, request.context
- Message = request.registry['mail.message']
- badge_st_id = request.registry['ir.model.data'].xmlid_to_res_id(cr, uid, 'gamification.mt_badge_granted')
- if badge_st_id:
- msg_ids = Message.search(cr, uid, [('subtype_id', '=', badge_st_id), ('to_read', '=', True)], context=context)
- msg = Message.browse(cr, uid, msg_ids, context=context)
+ badge_subtype = request.env.ref('gamification.mt_badge_granted')
+ if badge_subtype:
+ msg = request.env['mail.message'].search([('subtype_id', '=', badge_subtype.id), ('to_read', '=', True)])
else:
msg = list()
return msg
def _prepare_forum_values(self, forum=None, **kwargs):
- user = request.registry['res.users'].browse(request.cr, request.uid, request.uid, context=request.context)
values = {
- 'user': user,
- 'is_public_user': user.id == request.website.user_id.id,
+ 'user': request.env.user,
+ 'is_public_user': request.env.user.id == request.website.user_id.id,
'notifications': self._get_notifications(),
'header': kwargs.get('header', dict()),
'searches': kwargs.get('searches', dict()),
if forum:
values['forum'] = forum
elif kwargs.get('forum_id'):
- values['forum'] = request.registry['forum.forum'].browse(request.cr, request.uid, kwargs.pop('forum_id'), context=request.context)
+ values['forum'] = request.env['forum.forum'].browse(kwargs.pop('forum_id'))
values.update(kwargs)
return values
@http.route('/forum/send_validation_email', type='json', auth='user', website=True)
def send_validation_email(self, forum_id=None, **kwargs):
- request.registry['res.users'].send_forum_validation_email(request.cr, request.uid, request.uid, forum_id=forum_id, context=request.context)
+ request.env['res.users'].send_forum_validation_email(request.uid, forum_id=forum_id)
request.session['validation_email_sent'] = True
return True
forum_id = int(forum_id)
except ValueError:
forum_id = None
- done = request.registry['res.users'].process_forum_validation_token(request.cr, request.uid, token, int(id), email, forum_id=forum_id, context=request.context)
+ done = request.env['res.users'].process_forum_validation_token(token, int(id), email, forum_id=forum_id)
if done:
request.session['validation_email_done'] = True
if forum_id:
@http.route(['/forum'], type='http', auth="public", website=True)
def forum(self, **kwargs):
- cr, uid, context = request.cr, request.uid, request.context
- Forum = request.registry['forum.forum']
- obj_ids = Forum.search(cr, uid, [], context=context)
- forums = Forum.browse(cr, uid, obj_ids, context=context)
+ forums = request.env['forum.forum'].search([])
return request.website.render("website_forum.forum_all", {'forums': forums})
@http.route('/forum/new', type='http', auth="user", methods=['POST'], website=True)
def forum_create(self, forum_name="New Forum", **kwargs):
- forum_id = request.registry['forum.forum'].create(request.cr, request.uid, {
- 'name': forum_name,
- }, context=request.context)
- return request.redirect("/forum/%s" % forum_id)
+ forum_id = request.env['forum.forum'].create({'name': forum_name})
+ return request.redirect("/forum/%s" % slug(forum_id))
@http.route('/forum/notification_read', type='json', auth="user", methods=['POST'], website=True)
def notification_read(self, **kwargs):
- request.registry['mail.message'].set_message_read(request.cr, request.uid, [int(kwargs.get('notification_id'))], read=True, context=request.context)
+ request.env['mail.message'].browse([int(kwargs.get('notification_id'))]).set_message_read(read=True)
return True
@http.route(['/forum/<model("forum.forum"):forum>',
'''/forum/<model("forum.forum"):forum>/tag/<model("forum.tag", "[('forum_id','=',forum[0])]"):tag>/questions/page/<int:page>''',
], type='http', auth="public", website=True)
def questions(self, forum, tag=None, page=1, filters='all', sorting=None, search='', post_type=None, **post):
- cr, uid, context = request.cr, request.uid, request.context
- Post = request.registry['forum.post']
- user = request.registry['res.users'].browse(cr, uid, uid, context=context)
+ Post = request.env['forum.post']
domain = [('forum_id', '=', forum.id), ('parent_id', '=', False), ('state', '=', 'active')]
if search:
if filters == 'unanswered':
domain += [('child_ids', '=', False)]
elif filters == 'followed':
- domain += [('message_follower_ids', '=', user.partner_id.id)]
-
+ domain += [('message_follower_ids', '=', request.env.user.partner_id.id)]
if post_type:
- domain += [('type', '=', post_type)]
+ domain += [('post_type', '=', post_type)]
+
if not sorting:
sorting = forum.default_order
- question_count = Post.search(cr, uid, domain, count=True, context=context)
+ question_count = Post.search_count(domain)
+
if tag:
url = "/forum/%s/tag/%s/questions" % (slug(forum), slug(tag))
else:
step=self._post_per_page, scope=self._post_per_page,
url_args=url_args)
- obj_ids = Post.search(cr, uid, domain, limit=self._post_per_page, offset=pager['offset'], order=sorting, context=context)
- question_ids = Post.browse(cr, uid, obj_ids, context=context)
+ question_ids = Post.search(domain, limit=self._post_per_page, offset=pager['offset'], order=sorting)
values = self._prepare_forum_values(forum=forum, searches=post)
values.update({
@http.route('/forum/get_tags', type='http', auth="public", methods=['GET'], website=True)
def tag_read(self, **post):
- tags = request.registry['forum.tag'].search_read(request.cr, request.uid, [], ['name'], context=request.context)
+ tags = request.env['forum.tag'].search_read([], ['name'])
data = [tag['name'] for tag in tags]
return simplejson.dumps(data)
@http.route(['/forum/<model("forum.forum"):forum>/tag'], type='http', auth="public", website=True)
def tags(self, forum, page=1, **post):
- cr, uid, context = request.cr, request.uid, request.context
- Tag = request.registry['forum.tag']
- obj_ids = Tag.search(cr, uid, [('forum_id', '=', forum.id), ('posts_count', '>', 0)], limit=None, order='posts_count DESC', context=context)
- tags = Tag.browse(cr, uid, obj_ids, context=context)
+ tags = request.env['forum.tag'].search([('forum_id', '=', forum.id), ('posts_count', '>', 0)], limit=None, order='posts_count DESC')
values = self._prepare_forum_values(forum=forum, searches={'tags': True}, **post)
values.update({
'tags': tags,
@http.route(['''/forum/<model("forum.forum"):forum>/question/<model("forum.post", "[('forum_id','=',forum[0]),('parent_id','=',False)]"):question>'''], type='http', auth="public", website=True)
def question(self, forum, question, **post):
- cr, uid, context = request.cr, request.uid, request.context
# increment view counter
- request.registry['forum.post'].set_viewed(cr, SUPERUSER_ID, [question.id], context=context)
-
+ question.sudo().set_viewed()
if question.parent_id:
redirect_url = "/forum/%s/question/%s" % (slug(forum), slug(question.parent_id))
return werkzeug.utils.redirect(redirect_url, 301)
-
filters = 'question'
values = self._prepare_forum_values(forum=forum, searches=post)
values.update({
favourite_ids = [(4, request.uid)]
else:
favourite_ids = [(3, request.uid)]
- request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'favourite_ids': favourite_ids}, context=request.context)
+ question.sudo().write({'favourite_ids': favourite_ids})
return favourite
@http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/ask_for_close', type='http', auth="user", methods=['POST'], website=True)
def question_ask_for_close(self, forum, question, **post):
- cr, uid, context = request.cr, request.uid, request.context
- Reason = request.registry['forum.post.reason']
- reason_ids = Reason.search(cr, uid, [], context=context)
- reasons = Reason.browse(cr, uid, reason_ids, context)
+ reasons = request.env['forum.post.reason'].search([])
values = self._prepare_forum_values(**post)
values.update({
@http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/close', type='http', auth="user", methods=['POST'], website=True)
def question_close(self, forum, question, **post):
- request.registry['forum.post'].close(request.cr, request.uid, [question.id], reason_id=int(post.get('reason_id', False)), context=request.context)
+ question.close(reason_id=int(post.get('reason_id', False)))
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
@http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/reopen', type='http', auth="user", methods=['POST'], website=True)
def question_reopen(self, forum, question, **kwarg):
- request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'state': 'active'}, context=request.context)
+ question.state = 'active'
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
@http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/delete', type='http', auth="user", methods=['POST'], website=True)
def question_delete(self, forum, question, **kwarg):
- request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'active': False}, context=request.context)
+ question.active = False
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
@http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/undelete', type='http', auth="user", methods=['POST'], website=True)
def question_undelete(self, forum, question, **kwarg):
- request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'active': True}, context=request.context)
+ question.active = True
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
# Post
# --------------------------------------------------
- @http.route(['/forum/<model("forum.forum"):forum>/<post_type>'], type='http', auth="public", website=True)
- def forum_post(self, forum, post_type, **post):
+ @http.route(['/forum/<model("forum.forum"):forum>/ask'], type='http', auth="public", website=True)
+ def forum_post(self, forum, post_type=None, **post):
if not request.session.uid:
return login_redirect()
- cr, uid, context = request.cr, request.uid, request.context
- user = request.registry['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
+ user = request.env.user
+ if not post_type in ['question', 'link', 'discussion']: # fixme: make dynamic
+ return werkzeug.utils.redirect('/forum/%s' % slug(forum))
if not user.email or not tools.single_email_re.match(user.email):
- return werkzeug.utils.redirect("/forum/%s/user/%s/edit?email_required=1" % (slug(forum), uid))
+ return werkzeug.utils.redirect("/forum/%s/user/%s/edit?email_required=1" % (slug(forum), self._uid))
values = self._prepare_forum_values(forum=forum, searches={}, header={'ask_hide': True})
- return request.website.render("website_forum.%s" % post_type, values)
+ return request.website.render("website_forum.new_%s" % post_type, values)
- @http.route(['/forum/<model("forum.forum"):forum>/<post_type>/new',
- '/forum/<model("forum.forum"):forum>/<model("forum.post"):post_parent>/reply']
- , type='http', auth="public", methods=['POST'], website=True)
- def post_create(self, forum, post_parent='', post_type='', **post):
+ @http.route(['/forum/<model("forum.forum"):forum>/new',
+ '/forum/<model("forum.forum"):forum>/<model("forum.post"):post_parent>/reply'],
+ type='http', auth="public", methods=['POST'], website=True)
+ def post_create(self, forum, post_parent=None, post_type=None, **post):
cr, uid, context = request.cr, request.uid, request.context
if not request.session.uid:
return login_redirect()
post_tag_ids = []
- Tag = request.registry['forum.tag']
+ Tag = request.env['forum.tag']
if post.get('post_tags', False) and post.get('post_tags').strip('[]'):
tags = post.get('post_tags').strip('[]').replace('"', '').split(",")
for tag in tags:
- tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context)
- if tag_ids:
- post_tag_ids.append((4, tag_ids[0]))
+ tag_rec = Tag.search([('name', '=', tag)])
+ if tag_rec:
+ post_tag_ids.append((4, tag_rec.id))
else:
post_tag_ids.append((0, 0, {'name': tag, 'forum_id': forum.id}))
- new_question_id = request.registry['forum.post'].create(cr, uid, {
- 'forum_id': forum.id,
- 'name': post.get('post_name', ''),
- 'content': post.get('content', False),
- 'content_link': post.get('content_link', False),
- 'parent_id': post_parent and post_parent.id or False,
- 'tag_ids': post_tag_ids,
- 'type': post_parent and post_parent.type or post_type,
- }, context=context)
- return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), post_parent and slug(post_parent) or new_question_id))
+ new_question = request.env['forum.post'].create({
+ 'forum_id': forum.id,
+ 'name': post.get('post_name', ''),
+ 'content': post.get('content', False),
+ 'content_link': post.get('content_link', False),
+ 'parent_id': post_parent and post_parent.id or False,
+ 'tag_ids': post_tag_ids,
+ 'post_type': post_parent and post_parent.post_type or post_type, # tde check in selection field
+ })
+ return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), post_parent and slug(post_parent) or new_question.id))
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/comment', type='http', auth="public", methods=['POST'], website=True)
def post_comment(self, forum, post, **kwargs):
if not request.session.uid:
return login_redirect()
question = post.parent_id if post.parent_id else post
- cr, uid, context = request.cr, request.uid, request.context
if kwargs.get('comment') and post.forum_id.id == forum.id:
# TDE FIXME: check that post_id is the question or one of its answers
- request.registry['forum.post'].message_post(
- cr, uid, post.id,
+ post.with_context(mail_create_nosubcribe=True).message_post(
body=kwargs.get('comment'),
type='comment',
- subtype='mt_comment',
- context=dict(context, mail_create_nosubcribe=True))
+ subtype='mt_comment')
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/toggle_correct', type='json', auth="public", website=True)
def post_toggle_correct(self, forum, post, **kwargs):
- cr, uid, context = request.cr, request.uid, request.context
if post.parent_id is False:
return request.redirect('/')
if not request.session.uid:
return {'error': 'anonymous_user'}
# set all answers to False, only one can be accepted
- request.registry['forum.post'].write(cr, uid, [c.id for c in post.parent_id.child_ids if not c.id == post.id], {'is_correct': False}, context=context)
- request.registry['forum.post'].write(cr, uid, [post.id], {'is_correct': not post.is_correct}, context=context)
+ (post.parent_id.child_ids - post).write(dict(is_correct=False))
+ post.is_correct = not post.is_correct
return post.is_correct
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/delete', type='http', auth="user", methods=['POST'], website=True)
def post_delete(self, forum, post, **kwargs):
question = post.parent_id
- request.registry['forum.post'].unlink(request.cr, request.uid, [post.id], context=request.context)
+ post.unlink()
if question:
werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
return werkzeug.utils.redirect("/forum/%s" % slug(forum))
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/save', type='http', auth="user", methods=['POST'], website=True)
def post_save(self, forum, post, **kwargs):
- cr, uid, context = request.cr, request.uid, request.context
post_tags = []
if kwargs.get('post_tag') and kwargs.get('post_tag').strip('[]'):
- Tag = request.registry['forum.tag']
+ Tag = request.env['forum.tag']
tags = kwargs.get('post_tag').strip('[]').replace('"', '').split(",")
for tag in tags:
- tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context)
- if tag_ids:
- post_tags += tag_ids
+ tag_rec = Tag.search([('name', '=', tag)])
+ if tag_rec:
+ post_tags += tag_rec.ids
else:
- new_tag = Tag.create(cr, uid, {'name': tag, 'forum_id': forum.id}, context=context)
- post_tags.append(new_tag)
+ new_tag = Tag.create({'name': tag, 'forum_id': forum.id})
+ post_tags.append(new_tag.id)
vals = {
'tag_ids': [(6, 0, post_tags)],
'name': kwargs.get('post_name'),
'content': kwargs.get('content'),
}
- request.registry['forum.post'].write(cr, uid, [post.id], vals, context=context)
+ post.write(vals)
question = post.parent_id if post.parent_id else post
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
if request.uid == post.create_uid.id:
return {'error': 'own_post'}
upvote = True if not post.user_vote > 0 else False
- return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=upvote, context=request.context)
+ return post.vote(upvote=upvote)
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/downvote', type='json', auth="public", website=True)
def post_downvote(self, forum, post, **kwargs):
if request.uid == post.create_uid.id:
return {'error': 'own_post'}
upvote = True if post.user_vote < 0 else False
- return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=upvote, context=request.context)
+ return post.vote(upvote=upvote)
# User
# --------------------------------------------------
'/forum/<model("forum.forum"):forum>/users/page/<int:page>'],
type='http', auth="public", website=True)
def users(self, forum, page=1, **searches):
- cr, uid, context = request.cr, request.uid, request.context
- User = request.registry['res.users']
-
+ User = request.env['res.users']
step = 30
- tag_count = User.search(cr, SUPERUSER_ID, [('karma', '>', 1), ('website_published', '=', True)], count=True, context=context)
+ tag_count = len(User.search([('karma', '>', 1), ('website_published', '=', True)]))
pager = request.website.pager(url="/forum/%s/users" % slug(forum), total=tag_count, page=page, step=step, scope=30)
-
- obj_ids = User.search(cr, SUPERUSER_ID, [('karma', '>', 1), ('website_published', '=', True)], limit=step, offset=pager['offset'], order='karma DESC', context=context)
+ user_obj = User.sudo().search([('karma', '>', 1), ('website_published', '=', True)], limit=step, offset=pager['offset'], order='karma DESC')
# put the users in block of 3 to display them as a table
- users = [[] for i in range(len(obj_ids)/3+1)]
- for index, user in enumerate(User.browse(cr, SUPERUSER_ID, obj_ids, context=context)):
- users[index/3].append(user)
+ users = [[] for i in range(len(user_obj) / 3 + 1)]
+ for index, user in enumerate(user_obj):
+ users[index / 3].append(user)
searches['users'] = 'True'
values = self._prepare_forum_values(forum=forum, searches=searches)
@http.route(['/forum/<model("forum.forum"):forum>/partner/<int:partner_id>'], type='http', auth="public", website=True)
def open_partner(self, forum, partner_id=0, **post):
- cr, uid, context = request.cr, request.uid, request.context
if partner_id:
- partner = request.registry['res.partner'].browse(cr, SUPERUSER_ID, partner_id, context=context)
- if partner.exists() and partner.user_ids:
+ partner = request.env['res.partner'].sudo().search([('id', '=', partner_id)])
+ if partner and partner.user_ids:
return werkzeug.utils.redirect("/forum/%s/user/%d" % (slug(forum), partner.user_ids[0].id))
return werkzeug.utils.redirect("/forum/%s" % slug(forum))
@http.route(['/forum/user/<int:user_id>/avatar'], type='http', auth="public", website=True)
def user_avatar(self, user_id=0, **post):
- cr, uid, context = request.cr, request.uid, request.context
response = werkzeug.wrappers.Response()
- User = request.registry['res.users']
- Website = request.registry['website']
- user = User.browse(cr, SUPERUSER_ID, user_id, context=context)
+ User = request.env['res.users']
+ Website = request.env['website']
+ user = User.sudo().search([('id', '=', user_id)])
if not user.exists() or (user_id != request.session.uid and user.karma < 1):
return Website._image_placeholder(response)
- return Website._image(cr, SUPERUSER_ID, 'res.users', user.id, 'image', response)
+ return Website._image('res.users', user.id, 'image', response)
@http.route(['/forum/<model("forum.forum"):forum>/user/<int:user_id>'], type='http', auth="public", website=True)
def open_user(self, forum, user_id=0, **post):
- cr, uid, context = request.cr, request.uid, request.context
- User = request.registry['res.users']
- Post = request.registry['forum.post']
- Vote = request.registry['forum.post.vote']
- Activity = request.registry['mail.message']
- Followers = request.registry['mail.followers']
- Data = request.registry["ir.model.data"]
-
- user = User.browse(cr, SUPERUSER_ID, user_id, context=context)
- if not user.exists() or user.karma < 1:
+ User = request.env['res.users']
+ Post = request.env['forum.post']
+ Vote = request.env['forum.post.vote']
+ Activity = request.env['mail.message']
+ Followers = request.env['mail.followers']
+ Data = request.env["ir.model.data"]
+
+ user = User.sudo().search([('id', '=', user_id)])
+ if not user or user.karma < 1:
return werkzeug.utils.redirect("/forum/%s" % slug(forum))
values = self._prepare_forum_values(forum=forum, **post)
if user_id != request.session.uid and not user.website_published:
return request.website.render("website_forum.private_profile", values)
# questions and answers by user
user_questions, user_answers = [], []
- user_question_ids = Post.search(cr, uid, [
- ('parent_id', '=', False),
- ('forum_id', '=', forum.id), ('create_uid', '=', user.id),
- ], order='create_date desc', context=context)
+ user_question_ids = Post.search([
+ ('parent_id', '=', False),
+ ('forum_id', '=', forum.id), ('create_uid', '=', user.id)],
+ order='create_date desc')
count_user_questions = len(user_question_ids)
# displaying only the 20 most recent questions
- user_questions = Post.browse(cr, uid, user_question_ids[:20], context=context)
+ user_questions = user_question_ids[:20]
- user_answer_ids = Post.search(cr, uid, [
- ('parent_id', '!=', False),
- ('forum_id', '=', forum.id), ('create_uid', '=', user.id),
- ], order='create_date desc', context=context)
+ user_answer_ids = Post.search([
+ ('parent_id', '!=', False),
+ ('forum_id', '=', forum.id), ('create_uid', '=', user.id)],
+ order='create_date desc')
count_user_answers = len(user_answer_ids)
# displaying only the 20 most recent answers
- user_answers = Post.browse(cr, uid, user_answer_ids[:20], context=context)
+ user_answers = user_answer_ids[:20]
# showing questions which user following
- obj_ids = Followers.search(cr, SUPERUSER_ID, [('res_model', '=', 'forum.post'), ('partner_id', '=', user.partner_id.id)], context=context)
- post_ids = [follower.res_id for follower in Followers.browse(cr, SUPERUSER_ID, obj_ids, context=context)]
- que_ids = Post.search(cr, uid, [('id', 'in', post_ids), ('forum_id', '=', forum.id), ('parent_id', '=', False)], context=context)
- followed = Post.browse(cr, uid, que_ids, context=context)
+ post_ids = [follower.res_id for follower in Followers.sudo().search([('res_model', '=', 'forum.post'), ('partner_id', '=', user.partner_id.id)])]
+ followed = Post.search([('id', 'in', post_ids), ('forum_id', '=', forum.id), ('parent_id', '=', False)])
- #showing Favourite questions of user.
- fav_que_ids = Post.search(cr, uid, [('favourite_ids', '=', user.id), ('forum_id', '=', forum.id), ('parent_id', '=', False)], context=context)
- favourite = Post.browse(cr, uid, fav_que_ids, context=context)
+ # showing Favourite questions of user.
+ favourite = Post.search([('favourite_ids', '=', user.id), ('forum_id', '=', forum.id), ('parent_id', '=', False)])
- #votes which given on users questions and answers.
- data = Vote.read_group(cr, uid, [('forum_id', '=', forum.id), ('recipient_id', '=', user.id)], ["vote"], groupby=["vote"], context=context)
+ # votes which given on users questions and answers.
+ data = Vote.read_group([('forum_id', '=', forum.id), ('recipient_id', '=', user.id)], ["vote"], groupby=["vote"])
up_votes, down_votes = 0, 0
for rec in data:
if rec['vote'] == '1':
elif rec['vote'] == '-1':
down_votes = rec['vote_count']
- #Votes which given by users on others questions and answers.
- post_votes = Vote.search(cr, uid, [('user_id', '=', user.id)], context=context)
- vote_ids = Vote.browse(cr, uid, post_votes, context=context)
+ # Votes which given by users on others questions and answers.
+ vote_ids = Vote.search([('user_id', '=', user.id)])
- #activity by user.
- model, comment = Data.get_object_reference(cr, uid, 'mail', 'mt_comment')
- activity_ids = Activity.search(cr, uid, [('res_id', 'in', user_question_ids+user_answer_ids), ('model', '=', 'forum.post'), ('subtype_id', '!=', comment)], order='date DESC', limit=100, context=context)
- activities = Activity.browse(cr, uid, activity_ids, context=context)
+ # activity by user.
+ model, comment = Data.get_object_reference('mail', 'mt_comment')
+ activities = Activity.search([('res_id', 'in', (user_question_ids+user_answer_ids).ids), ('model', '=', 'forum.post'), ('subtype_id', '!=', comment)], order='date DESC', limit=100)
posts = {}
for act in activities:
posts[act.res_id] = True
- posts_ids = Post.browse(cr, uid, posts.keys(), context=context)
+ posts_ids = Post.search([('id', 'in', posts.keys())])
posts = dict(map(lambda x: (x.id, (x.parent_id or x, x.parent_id and x or False)), posts_ids))
# TDE CLEANME MASTER: couldn't it be rewritten using a 'menu' key instead of one key for each menu ?
- if user.id == uid:
+ if user == request.env.user:
post['my_profile'] = True
else:
post['users'] = True
values.update({
- 'uid': uid,
+ 'uid': request.env.user.id,
'user': user,
'main_object': user,
'searches': post,
@http.route('/forum/<model("forum.forum"):forum>/user/<model("res.users"):user>/edit', type='http', auth="user", website=True)
def edit_profile(self, forum, user, **kwargs):
- country = request.registry['res.country']
- country_ids = country.search(request.cr, SUPERUSER_ID, [], context=request.context)
- countries = country.browse(request.cr, SUPERUSER_ID, country_ids, context=request.context)
+ countries = request.env['res.country'].search([])
values = self._prepare_forum_values(forum=forum, searches=kwargs)
values.update({
'email_required': kwargs.get('email_required'),
}
if request.uid == user.id: # the controller allows to edit only its own privacy settings; use partner management for other cases
values['website_published'] = kwargs.get('website_published') == 'True'
- request.registry['res.users'].write(request.cr, request.uid, [user.id], values, context=request.context)
+ user.write(values)
return werkzeug.utils.redirect("/forum/%s/user/%d" % (slug(forum), user.id))
# Badges
@http.route('/forum/<model("forum.forum"):forum>/badge', type='http', auth="public", website=True)
def badges(self, forum, **searches):
- cr, uid, context = request.cr, request.uid, request.context
- Badge = request.registry['gamification.badge']
- badge_ids = Badge.search(cr, SUPERUSER_ID, [('challenge_ids.category', '=', 'forum')], context=context)
- badges = Badge.browse(cr, uid, badge_ids, context=context)
+ Badge = request.env['gamification.badge']
+ badges = Badge.sudo().search([('challenge_ids.category', '=', 'forum')])
badges = sorted(badges, key=lambda b: b.stat_count_distinct, reverse=True)
values = self._prepare_forum_values(forum=forum, searches={'badges': True})
values.update({
@http.route(['''/forum/<model("forum.forum"):forum>/badge/<model("gamification.badge"):badge>'''], type='http', auth="public", website=True)
def badge_users(self, forum, badge, **kwargs):
- user_ids = [badge_user.user_id.id for badge_user in badge.owner_ids]
- users = request.registry['res.users'].browse(request.cr, SUPERUSER_ID, user_ids, context=request.context)
+ users = [badge_user.user_id for badge_user in badge.owner_ids]
values = self._prepare_forum_values(forum=forum, searches={'badges': True})
values.update({
'badge': badge,
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/comment/<model("mail.message"):comment>/convert_to_answer', type='http', auth="user", methods=['POST'], website=True)
def convert_comment_to_answer(self, forum, post, comment, **kwarg):
- new_post_id = request.registry['forum.post'].convert_comment_to_answer(request.cr, request.uid, comment.id, context=request.context)
- if not new_post_id:
+ post = request.env['forum.post'].convert_comment_to_answer(comment.id)
+ if not post:
return werkzeug.utils.redirect("/forum/%s" % slug(forum))
- post = request.registry['forum.post'].browse(request.cr, request.uid, new_post_id, context=request.context)
question = post.parent_id if post.parent_id else post
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/convert_to_comment', type='http', auth="user", methods=['POST'], website=True)
def convert_answer_to_comment(self, forum, post, **kwarg):
question = post.parent_id
- new_msg_id = request.registry['forum.post'].convert_answer_to_comment(request.cr, request.uid, post.id, context=request.context)
+ new_msg_id = post.convert_answer_to_comment()[0]
if not new_msg_id:
return werkzeug.utils.redirect("/forum/%s" % slug(forum))
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
def delete_comment(self, forum, post, comment, **kwarg):
if not request.session.uid:
return {'error': 'anonymous_user'}
- return request.registry['forum.post'].unlink_comment(request.cr, request.uid, post.id, comment.id, context=request.context)
+ return post.unlink_comment(comment.id)[0]
<field name="state">open</field>
</record>
+ <!-- Set Admin karma -->
+ <record id="base.user_root" model="res.users">
+ <field name="karma">2500</field>
+ </record>
+
<!-- Answers subtypes -->
<record id="mt_answer_new" model="mail.message.subtype">
<field name="name">New Answer</field>
<record id="question_0" model="forum.post">
<field name="name">How to configure alerts for employee contract expiration</field>
<field name="forum_id" ref="website_forum.forum_help"/>
- <field name="type">question</field>
+ <field name="post_type">question</field>
<field name="views">3</field>
<field name="tag_ids" eval="[(4,ref('website_forum.tags_0')), (4,ref('website_forum.tags_1'))]"/>
</record>
Can I use Odoo as a replacement CMS of Wordpress + eCommerce plugin?
In simple words does Odoo became CMS+ERP platform?</p>]]></field>
- <field name="type">question</field>
+ <field name="post_type">question</field>
<field name="tag_ids" eval="[(4,ref('website_forum.tags_2'))]"/>
</record>
<li>Step 4. To test, set a contract to expire tomorrow under one of your fleets vehicles. Then Save it.</li>
<li>Step 5. Go to Scheduled Actions.. Set interval number to 1. Interval Unit to Minutes. Then Set the Next Execution date to 2 minutes from now. If your SMTP is configured correctly you will start to get a mail every minute with the reminder.</li></ul>]]></field>
<field name="parent_id" ref="question_0" />
- <field name="type">question</field>
+ <field name="post_type">question</field>
</record>
<record id="answer_1" model="forum.post">
<field name="forum_id" ref="website_forum.forum_help"/>
The CMS editor in Odoo web is nice but I prefer Drupal for customization and there is a Drupal module for Odoo. I think WP is better than Odoo web too.
</field>
<field name="parent_id" ref="question_1"/>
- <field name="type">question</field>
+ <field name="post_type">question</field>
</record>
<!-- Article-->
<field name="content_link">https://www.odoo.com</field>
<field name="name">Discover Odoo, an open source ERP.</field>
<field name="forum_id" ref="website_forum.forum_help"/>
- <field name="type">link</field>
+ <field name="post_type">link</field>
<field name="views">6</field>
<field name="tag_ids" eval="[(4,ref('website_forum.tags_3'))]"/>
</record>
(I'm using Ubuntu Server.)</field>
<field name="forum_id" ref="website_forum.forum_help"/>
- <field name="type">discussion</field>
+ <field name="post_type">discussion</field>
<field name="views">5</field>
</record>
</ol>]]>
</field>
<field name="parent_id" ref="discussion_0"/>
- <field name="type">discussion</field>
+ <field name="post_type">discussion</field>
</record>
<!-- Post Vote -->
# -*- coding: utf-8 -*-
from datetime import datetime
+import math
import uuid
from werkzeug.exceptions import Forbidden
-import openerp
-from openerp import api, tools
+from openerp import _
+from openerp import api, fields, models
+from openerp import modules
+from openerp import tools
from openerp import SUPERUSER_ID
from openerp.addons.website.models.website import slug
from openerp.exceptions import Warning
-from openerp.osv import osv, fields
-from openerp.tools import html2plaintext
-from openerp.tools.translate import _
class KarmaError(Forbidden):
pass
-class Forum(osv.Model):
- """TDE TODO: set karma values for actions dynamic for a given forum"""
+class Forum(models.Model):
_name = 'forum.forum'
- _description = 'Forums'
+ _description = 'Forum'
_inherit = ['mail.thread', 'website.seo.metadata']
def init(self, cr):
- """ Add forum uuid for user email validation. """
+ """ Add forum uuid for user email validation.
+
+ TDE TODO: move me somewhere else, auto_init ? """
forum_uuids = self.pool['ir.config_parameter'].search(cr, SUPERUSER_ID, [('key', '=', 'website_forum.uuid')])
if not forum_uuids:
self.pool['ir.config_parameter'].set_param(cr, SUPERUSER_ID, 'website_forum.uuid', str(uuid.uuid4()), ['base.group_system'])
- _columns = {
- 'name': fields.char('Forum Name', required=True, translate=True),
- 'faq': fields.html('Guidelines'),
- 'description': fields.html('Description'),
- 'introduction_message': fields.html('Introduction Message'),
- 'relevancy_option_first': fields.float('First Relevancy Parameter'),
- 'relevancy_option_second': fields.float('Second Relevancy Parameter'),
- 'default_order': fields.selection([
- ('create_date desc','Newest'),
- ('write_date desc','Last Updated'),
- ('vote_count desc','Most Voted'),
- ('relevancy desc','Relevancy'),
- ('child_count desc','Answered'),
- ], 'Default Order', required=True),
- 'default_allow': fields.selection([('post_link','Link'),('ask_question','Question'),('post_discussion','Discussion')], 'Default Post', required=True),
- 'allow_link': fields.boolean('Links', help="When clicking on the post, it redirects to an external link"),
- 'allow_question': fields.boolean('Questions', help="Users can answer only once per question. Contributors can edit answers and mark the right ones."),
- 'allow_discussion': fields.boolean('Discussions'),
- # karma generation
- 'karma_gen_question_new': fields.integer('Asking a question'),
- 'karma_gen_question_upvote': fields.integer('Question upvoted'),
- 'karma_gen_question_downvote': fields.integer('Question downvoted'),
- 'karma_gen_answer_upvote': fields.integer('Answer upvoted'),
- 'karma_gen_answer_downvote': fields.integer('Answer downvoted'),
- 'karma_gen_answer_accept': fields.integer('Accepting an answer'),
- 'karma_gen_answer_accepted': fields.integer('Answer accepted'),
- 'karma_gen_answer_flagged': fields.integer('Answer flagged'),
- # karma-based actions
- 'karma_ask': fields.integer('Ask a new question'),
- 'karma_answer': fields.integer('Answer a question'),
- 'karma_edit_own': fields.integer('Edit its own posts'),
- 'karma_edit_all': fields.integer('Edit all posts'),
- 'karma_close_own': fields.integer('Close its own posts'),
- 'karma_close_all': fields.integer('Close all posts'),
- 'karma_unlink_own': fields.integer('Delete its own posts'),
- 'karma_unlink_all': fields.integer('Delete all posts'),
- 'karma_upvote': fields.integer('Upvote'),
- 'karma_downvote': fields.integer('Downvote'),
- 'karma_answer_accept_own': fields.integer('Accept an answer on its own questions'),
- 'karma_answer_accept_all': fields.integer('Accept an answer to all questions'),
- 'karma_editor_link_files': fields.integer('Linking files (Editor)'),
- 'karma_editor_clickable_link': fields.integer('Add clickable links (Editor)'),
- 'karma_comment_own': fields.integer('Comment its own posts'),
- 'karma_comment_all': fields.integer('Comment all posts'),
- 'karma_comment_convert_own': fields.integer('Convert its own answers to comments and vice versa'),
- 'karma_comment_convert_all': fields.integer('Convert all answers to comments and vice versa'),
- 'karma_comment_unlink_own': fields.integer('Unlink its own comments'),
- 'karma_comment_unlink_all': fields.integer('Unlink all comments'),
- 'karma_retag': fields.integer('Change question tags'),
- 'karma_flag': fields.integer('Flag a post as offensive'),
- }
-
- def _get_default_faq(self, cr, uid, context=None):
- fname = openerp.modules.get_module_resource('website_forum', 'data', 'forum_default_faq.html')
+ @api.model
+ def _get_default_faq(self):
+ fname = modules.get_module_resource('website_forum', 'data', 'forum_default_faq.html')
with open(fname, 'r') as f:
return f.read()
return False
- _defaults = {
- 'default_order': 'write_date desc',
- 'allow_question': True,
- 'default_allow': 'ask_question',
- 'allow_link': False,
- 'allow_discussion': False,
- 'description': 'This community is for professionals and enthusiasts of our products and services.',
- 'faq': _get_default_faq,
- 'karma_gen_question_new': 0, # set to null for anti spam protection
- 'introduction_message': """<h1 class="mt0">Welcome!</h1>
- <p> This community is for professionals and enthusiasts of our products and services.
- Share and discuss the best content and new marketing ideas,
- build your professional profile and become a better marketer together.
- </p>""",
- 'relevancy_option_first': 0.8,
- 'relevancy_option_second': 1.8,
- 'karma_gen_question_upvote': 5,
- 'karma_gen_question_downvote': -2,
- 'karma_gen_answer_upvote': 10,
- 'karma_gen_answer_downvote': -2,
- 'karma_gen_answer_accept': 2,
- 'karma_gen_answer_accepted': 15,
- 'karma_gen_answer_flagged': -100,
- 'karma_ask': 3, # set to not null for anti spam protection
- 'karma_answer': 3, # set to not null for anti spam protection
- 'karma_edit_own': 1,
- 'karma_edit_all': 300,
- 'karma_close_own': 100,
- 'karma_close_all': 500,
- 'karma_unlink_own': 500,
- 'karma_unlink_all': 1000,
- 'karma_upvote': 5,
- 'karma_downvote': 50,
- 'karma_answer_accept_own': 20,
- 'karma_answer_accept_all': 500,
- 'karma_editor_link_files': 20,
- 'karma_editor_clickable_link': 20,
- 'karma_comment_own': 3,
- 'karma_comment_all': 5,
- 'karma_comment_convert_own': 50,
- 'karma_comment_convert_all': 500,
- 'karma_comment_unlink_own': 50,
- 'karma_comment_unlink_all': 500,
- 'karma_retag': 75,
- 'karma_flag': 500,
- }
-
- def create(self, cr, uid, values, context=None):
- if context is None:
- context = {}
- create_context = dict(context, mail_create_nolog=True)
- return super(Forum, self).create(cr, uid, values, context=create_context)
-
-
-class Post(osv.Model):
+ # description and use
+ name = fields.Char('Forum Name', required=True, translate=True)
+ faq = fields.Html('Guidelines', default=_get_default_faq, translate=True)
+ description = fields.Html(
+ 'Description',
+ default='<p> This community is for professionals and enthusiasts of our products and services.'
+ 'Share and discuss the best content and new marketing ideas,'
+ 'build your professional profile and become a better marketer together.</p>')
+ default_order = fields.Selection([
+ ('create_date desc', 'Newest'),
+ ('write_date desc', 'Last Updated'),
+ ('vote_count desc', 'Most Voted'),
+ ('relevancy desc', 'Relevancy'),
+ ('child_count desc', 'Answered')],
+ string='Default Order', required=True, default='write_date desc')
+ relevancy_post_vote = fields.Float('First Relevancy Parameter', default=0.8)
+ relevancy_time_decay = fields.Float('Second Relevancy Parameter', default=1.8)
+ default_post_type = fields.Selection([
+ ('question', 'Question'),
+ ('discussion', 'Discussion'),
+ ('link', 'Link')],
+ string='Default Post', required=True, default='question')
+ allow_question = fields.Boolean('Questions', help="Users can answer only once per question. Contributors can edit answers and mark the right ones.", default=True)
+ allow_discussion = fields.Boolean('Discussions', default=False)
+ allow_link = fields.Boolean('Links', help="When clicking on the post, it redirects to an external link", default=False)
+ # karma generation
+ karma_gen_question_new = fields.Integer(string='Asking a question', default=2)
+ karma_gen_question_upvote = fields.Integer(string='Question upvoted', default=5)
+ karma_gen_question_downvote = fields.Integer(string='Question downvoted', default=-2)
+ karma_gen_answer_upvote = fields.Integer(string='Answer upvoted', default=10)
+ karma_gen_answer_downvote = fields.Integer(string='Answer downvoted', default=-2)
+ karma_gen_answer_accept = fields.Integer(string='Accepting an answer', default=2)
+ karma_gen_answer_accepted = fields.Integer(string='Answer accepted', default=15)
+ karma_gen_answer_flagged = fields.Integer(string='Answer flagged', default=-100)
+ # karma-based actions
+ karma_ask = fields.Integer(string='Ask a new question', default=3)
+ karma_answer = fields.Integer(string='Answer a question', default=3)
+ karma_edit_own = fields.Integer(string='Edit its own posts', default=1)
+ karma_edit_all = fields.Integer(string='Edit all posts', default=300)
+ karma_close_own = fields.Integer(string='Close its own posts', default=100)
+ karma_close_all = fields.Integer(string='Close all posts', default=500)
+ karma_unlink_own = fields.Integer(string='Delete its own posts', default=500)
+ karma_unlink_all = fields.Integer(string='Delete all posts', default=1000)
+ karma_upvote = fields.Integer(string='Upvote', default=5)
+ karma_downvote = fields.Integer(string='Downvote', default=50)
+ karma_answer_accept_own = fields.Integer(string='Accept an answer on its own questions', default=20)
+ karma_answer_accept_all = fields.Integer(string='Accept an answers to all questions', default=500)
+ karma_editor_link_files = fields.Integer(string='Linking files (Editor)', default=20)
+ karma_editor_clickable_link = fields.Integer(string='Add clickable links (Editor)', default=20)
+ karma_comment_own = fields.Integer(string='Comment its own posts', default=1)
+ karma_comment_all = fields.Integer(string='Comment all posts', default=1)
+ karma_comment_convert_own = fields.Integer(string='Convert its own answers to comments and vice versa', default=50)
+ karma_comment_convert_all = fields.Integer(string='Convert all answers to answers and vice versa', default=500)
+ karma_comment_unlink_own = fields.Integer(string='Unlink its own comments', default=50)
+ karma_comment_unlink_all = fields.Integer(string='Unlinnk all comments', default=500)
+ karma_retag = fields.Integer(string='Change question tags', default=75)
+ karma_flag = fields.Integer(string='Flag a post as offensive', default=500)
+
+ @api.model
+ def create(self, values):
+ return super(Forum, self.with_context(mail_create_nolog=True)).create(values)
+
+
+class Post(models.Model):
_name = 'forum.post'
_description = 'Forum Post'
_inherit = ['mail.thread', 'website.seo.metadata']
_order = "is_correct DESC, vote_count DESC, write_date DESC"
- def _get_post_relevancy(self, cr, uid, ids, field_name, arg, context):
- res = dict.fromkeys(ids, 0)
- for post in self.browse(cr, uid, ids, context=context):
- days = (datetime.today() - datetime.strptime(post.create_date, tools.DEFAULT_SERVER_DATETIME_FORMAT)).days
- relavency = abs(post.vote_count - 1) ** post.forum_id.relevancy_option_first / ( days + 2) ** post.forum_id.relevancy_option_second
- res[post.id] = relavency if (post.vote_count - 1) >= 0 else -relavency
- return res
-
- def _get_user_vote(self, cr, uid, ids, field_name, arg, context):
- res = dict.fromkeys(ids, 0)
- vote_ids = self.pool['forum.post.vote'].search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
- for vote in self.pool['forum.post.vote'].browse(cr, uid, vote_ids, context=context):
- res[vote.post_id.id] = vote.vote
- return res
-
- def _get_vote_count(self, cr, uid, ids, field_name, arg, context):
- res = dict.fromkeys(ids, 0)
- for post in self.browse(cr, uid, ids, context=context):
- for vote in post.vote_ids:
- res[post.id] += int(vote.vote)
- return res
-
- def _get_post_from_vote(self, cr, uid, ids, context=None):
- result = {}
- for vote in self.pool['forum.post.vote'].browse(cr, uid, ids, context=context):
- result[vote.post_id.id] = True
- return result.keys()
-
- def _get_user_favourite(self, cr, uid, ids, field_name, arg, context):
- res = dict.fromkeys(ids, False)
- for post in self.browse(cr, uid, ids, context=context):
- if uid in [f.id for f in post.favourite_ids]:
- res[post.id] = True
- return res
-
- def _get_favorite_count(self, cr, uid, ids, field_name, arg, context):
- res = dict.fromkeys(ids, 0)
- for post in self.browse(cr, uid, ids, context=context):
- res[post.id] += len(post.favourite_ids)
- return res
-
- def _get_post_from_hierarchy(self, cr, uid, ids, context=None):
- post_ids = set(ids)
- for post in self.browse(cr, SUPERUSER_ID, ids, context=context):
- if post.parent_id:
- post_ids.add(post.parent_id.id)
- return list(post_ids)
-
- 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)
- else:
- res[post.id] = len(post.child_ids)
- return res
-
- def _get_uid_answered(self, cr, uid, ids, field_name, arg, context=None):
- res = dict.fromkeys(ids, False)
- for post in self.browse(cr, uid, ids, context=context):
- res[post.id] = any(answer.create_uid.id == uid for answer in post.child_ids)
- return res
-
- def _get_has_validated_answer(self, cr, uid, ids, field_name, arg, context=None):
- res = dict.fromkeys(ids, False)
- ans_ids = self.search(cr, uid, [('parent_id', 'in', ids), ('is_correct', '=', True)], context=context)
- for answer in self.browse(cr, uid, ans_ids, context=context):
- res[answer.parent_id.id] = True
- return res
-
- def _is_self_reply(self, cr, uid, ids, field_name, arg, context=None):
- res = dict.fromkeys(ids, False)
- for post in self.browse(cr, uid, ids, context=context):
- res[post.id] = post.parent_id and post.parent_id.create_uid == post.create_uid or False
- return res
-
- def _get_post_karma_rights(self, cr, uid, ids, field_name, arg, context=None):
- user = self.pool['res.users'].browse(cr, uid, uid, context=context)
- res = dict.fromkeys(ids, False)
- for post in self.browse(cr, uid, ids, context=context):
- res[post.id] = {
- 'karma_ask': post.forum_id.karma_ask,
- 'karma_answer': post.forum_id.karma_answer,
- '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,
- 'karma_edit': post.create_uid.id == uid and post.forum_id.karma_edit_own or post.forum_id.karma_edit_all,
- 'karma_close': post.create_uid.id == uid and post.forum_id.karma_close_own or post.forum_id.karma_close_all,
- 'karma_unlink': post.create_uid.id == uid and post.forum_id.karma_unlink_own or post.forum_id.karma_unlink_all,
- 'karma_upvote': post.forum_id.karma_upvote,
- 'karma_downvote': post.forum_id.karma_downvote,
- 'karma_comment': post.create_uid.id == uid and post.forum_id.karma_comment_own or post.forum_id.karma_comment_all,
- 'karma_comment_convert': post.create_uid.id == uid and post.forum_id.karma_comment_convert_own or post.forum_id.karma_comment_convert_all,
- }
- res[post.id].update({
- 'can_ask': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_ask'],
- 'can_answer': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_answer'],
- 'can_accept': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_accept'],
- 'can_edit': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_edit'],
- 'can_close': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_close'],
- 'can_unlink': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_unlink'],
- 'can_upvote': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_upvote'],
- 'can_downvote': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_downvote'],
- 'can_comment': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_comment'],
- 'can_comment_convert': uid == SUPERUSER_ID or user.karma >= res[post.id]['karma_comment_convert'],
- })
- return res
-
- _columns = {
- 'name': fields.char('Title'),
- 'forum_id': fields.many2one('forum.forum', 'Forum', required=True),
- 'content': fields.html('Content'),
- 'content_link': fields.char('URL', help="URL of Link Articles"),
- 'tag_ids': fields.many2many('forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', 'Tags'),
- 'state': fields.selection([('active', 'Active'), ('close', 'Close'), ('offensive', 'Offensive')], 'Status'),
- 'views': fields.integer('Number of Views'),
- 'active': fields.boolean('Active'),
- 'type': fields.selection([('question', 'Question'), ('link', 'Article'), ('discussion', 'Discussion')], 'Type'),
- 'relevancy': fields.function(
- _get_post_relevancy, string="Relevancy", type='float',
- store={
- 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
- 'forum.post.vote': (_get_post_from_vote, [], 10),
- }),
- 'is_correct': fields.boolean('Valid Answer', help='Correct Answer or Answer on this question accepted.'),
- 'website_message_ids': fields.one2many(
- 'mail.message', 'res_id',
- domain=lambda self: [
- '&', ('model', '=', self._name), ('type', 'in', ['email', 'comment'])
- ],
- string='Post Messages', help="Comments on forum post",
- ),
- # history
- 'create_date': fields.datetime('Asked on', select=True, readonly=True),
- 'create_uid': fields.many2one('res.users', 'Created by', select=True, readonly=True),
- 'write_date': fields.datetime('Update on', select=True, readonly=True),
- 'write_uid': fields.many2one('res.users', 'Updated by', select=True, readonly=True),
- # vote fields
- 'vote_ids': fields.one2many('forum.post.vote', 'post_id', 'Votes'),
- 'user_vote': fields.function(_get_user_vote, string='My Vote', type='integer'),
- 'vote_count': fields.function(
- _get_vote_count, string="Votes", type='integer',
- store={
- 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
- 'forum.post.vote': (_get_post_from_vote, [], 10),
- }),
- # favorite fields
- 'favourite_ids': fields.many2many('res.users', string='Favourite'),
- 'user_favourite': fields.function(_get_user_favourite, string="My Favourite", type='boolean'),
- 'favourite_count': fields.function(
- _get_favorite_count, string='Favorite Count', type='integer',
- store={
- 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['favourite_ids'], 10),
- }),
- # hierarchy
- 'parent_id': fields.many2one('forum.post', 'Question', ondelete='cascade'),
- 'self_reply': fields.function(
- _is_self_reply, 'Reply to own question', type='boolean',
- store={
- 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['parent_id', 'create_uid'], 10),
- }),
- 'child_ids': fields.one2many('forum.post', 'parent_id', 'Answers'),
- 'child_count': fields.function(
- _get_child_count, string="Answers", type='integer',
- store={
- 'forum.post': (_get_post_from_hierarchy, ['parent_id', 'child_ids'], 10),
- }),
- 'uid_has_answered': fields.function(
- _get_uid_answered, string='Has Answered', type='boolean',
- ),
- 'has_validated_answer': fields.function(
- _get_has_validated_answer, string='Has a Validated Answered', type='boolean',
- store={
- 'forum.post': (_get_post_from_hierarchy, ['parent_id', 'child_ids', 'is_correct'], 10),
- }
- ),
- # closing
- 'closed_reason_id': fields.many2one('forum.post.reason', 'Reason'),
- 'closed_uid': fields.many2one('res.users', 'Closed by', select=1),
- 'closed_date': fields.datetime('Closed on', readonly=True),
- # karma
- 'karma_ask': fields.function(_get_post_karma_rights, string='Karma to ask', type='integer', multi='_get_post_karma_rights'),
- 'karma_answer': fields.function(_get_post_karma_rights, string='Karma to answer', type='integer', multi='_get_post_karma_rights'),
- 'karma_accept': fields.function(_get_post_karma_rights, string='Karma to accept this answer', type='integer', multi='_get_post_karma_rights'),
- 'karma_edit': fields.function(_get_post_karma_rights, string='Karma to edit', type='integer', multi='_get_post_karma_rights'),
- 'karma_close': fields.function(_get_post_karma_rights, string='Karma to close', type='integer', multi='_get_post_karma_rights'),
- 'karma_unlink': fields.function(_get_post_karma_rights, string='Karma to unlink', type='integer', multi='_get_post_karma_rights'),
- 'karma_upvote': fields.function(_get_post_karma_rights, string='Karma to upvote', type='integer', multi='_get_post_karma_rights'),
- 'karma_downvote': fields.function(_get_post_karma_rights, string='Karma to downvote', type='integer', multi='_get_post_karma_rights'),
- 'karma_comment': fields.function(_get_post_karma_rights, string='Karma to comment', type='integer', multi='_get_post_karma_rights'),
- 'karma_comment_convert': fields.function(_get_post_karma_rights, string='karma to convert as a comment', type='integer', multi='_get_post_karma_rights'),
- # access rights
- 'can_ask': fields.function(_get_post_karma_rights, string='Can Ask', type='boolean', multi='_get_post_karma_rights'),
- 'can_answer': fields.function(_get_post_karma_rights, string='Can Answer', type='boolean', multi='_get_post_karma_rights'),
- 'can_accept': fields.function(_get_post_karma_rights, string='Can Accept', type='boolean', multi='_get_post_karma_rights'),
- 'can_edit': fields.function(_get_post_karma_rights, string='Can Edit', type='boolean', multi='_get_post_karma_rights'),
- 'can_close': fields.function(_get_post_karma_rights, string='Can Close', type='boolean', multi='_get_post_karma_rights'),
- 'can_unlink': fields.function(_get_post_karma_rights, string='Can Unlink', type='boolean', multi='_get_post_karma_rights'),
- 'can_upvote': fields.function(_get_post_karma_rights, string='Can Upvote', type='boolean', multi='_get_post_karma_rights'),
- 'can_downvote': fields.function(_get_post_karma_rights, string='Can Downvote', type='boolean', multi='_get_post_karma_rights'),
- 'can_comment': fields.function(_get_post_karma_rights, string='Can Comment', type='boolean', multi='_get_post_karma_rights'),
- 'can_comment_convert': fields.function(_get_post_karma_rights, string='Can Convert to Comment', type='boolean', multi='_get_post_karma_rights'),
- }
-
- _defaults = {
- 'state': 'active',
- 'views': 0,
- 'active': True,
- 'type': 'question',
- 'vote_ids': list(),
- 'favourite_ids': list(),
- 'child_ids': list(),
- }
-
- 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)
- post = self.browse(cr, uid, post_id, context=context)
+ name = fields.Char('Title')
+ forum_id = fields.Many2one('forum.forum', string='Forum', required=True)
+ content = fields.Html('Content')
+ content_link = fields.Char('URL', help="URL of Link Articles")
+ tag_ids = fields.Many2many('forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', string='Tags')
+ state = fields.Selection([('active', 'Active'), ('close', 'Close'), ('offensive', 'Offensive')], string='Status', default='active')
+ views = fields.Integer('Number of Views', default=0)
+ active = fields.Boolean('Active', default=True)
+ post_type = fields.Selection([
+ ('question', 'Question'),
+ ('link', 'Article'),
+ ('discussion', 'Discussion')],
+ string='Type', default='question')
+ website_message_ids = fields.One2many(
+ 'mail.message', 'res_id',
+ domain=lambda self: ['&', ('model', '=', self._name), ('type', 'in', ['email', 'comment'])],
+ string='Post Messages', help="Comments on forum post",
+ )
+ # history
+ create_date = fields.Datetime('Asked on', select=True, readonly=True)
+ create_uid = fields.Many2one('res.users', string='Created by', select=True, readonly=True)
+ write_date = fields.Datetime('Update on', select=True, readonly=True)
+ write_uid = fields.Many2one('res.users', string='Updated by', select=True, readonly=True)
+ relevancy = fields.Float('Relevancy', compute="_compute_relevancy", store=True)
+
+ @api.one
+ @api.depends('vote_count', 'forum_id.relevancy_post_vote', 'forum_id.relevancy_time_decay')
+ def _compute_relevancy(self):
+ days = (datetime.today() - datetime.strptime(self.create_date, tools.DEFAULT_SERVER_DATETIME_FORMAT)).days
+ self.relevancy = math.copysign(1, self.vote_count) * (abs(self.vote_count - 1) ** self.forum_id.relevancy_post_vote / (days + 2) ** self.forum_id.relevancy_time_decay)
+
+ # vote
+ vote_ids = fields.One2many('forum.post.vote', 'post_id', string='Votes')
+ user_vote = fields.Integer('My Vote', compute='_get_user_vote')
+ vote_count = fields.Integer('Votes', compute='_get_vote_count', store=True)
+
+ @api.multi
+ def _get_user_vote(self):
+ votes = self.env['forum.post.vote'].search_read([('post_id', 'in', self._ids), ('user_id', '=', self._uid)], ['vote', 'post_id'])
+ mapped_vote = dict([(v['post_id'][0], v['vote']) for v in votes])
+ for vote in self:
+ vote.user_vote = mapped_vote.get(vote.id, 0)
+
+ @api.multi
+ @api.depends('vote_ids')
+ def _get_vote_count(self):
+ read_group_res = self.env['forum.post.vote'].read_group([('post_id', 'in', self._ids)], ['post_id', 'vote'], ['post_id', 'vote'], lazy=False)
+ result = dict.fromkeys(self._ids, 0)
+ for data in read_group_res:
+ result[data['post_id'][0]] += data['__count'] * int(data['vote'])
+ for post in self:
+ post.vote_count = result[post.id]
+
+ # favorite
+ favourite_ids = fields.Many2many('res.users', string='Favourite')
+ user_favourite = fields.Boolean('Is Favourite', compute='_get_user_favourite')
+ favourite_count = fields.Integer('Favorite Count', compute='_get_favorite_count', store=True)
+
+ @api.one
+ def _get_user_favourite(self):
+ self.user_favourite = self._uid in self.favourite_ids.ids
+
+ @api.one
+ @api.depends('favourite_ids')
+ def _get_favorite_count(self):
+ self.favourite_count = len(self.favourite_ids)
+
+ # hierarchy
+ is_correct = fields.Boolean('Correct', help='Correct answer or answer accepted')
+ parent_id = fields.Many2one('forum.post', string='Question', ondelete='cascade')
+ self_reply = fields.Boolean('Reply to own question', compute='_is_self_reply', store=True)
+ child_ids = fields.One2many('forum.post', 'parent_id', string='Answers')
+ child_count = fields.Integer('Number of answers', compute='_get_child_count', store=True)
+ uid_has_answered = fields.Boolean('Has Answered', compute='_get_uid_has_answered')
+ has_validated_answer = fields.Boolean('Is answered', compute='_get_has_validated_answer', store=True)
+
+ @api.multi
+ @api.depends('create_uid', 'parent_id')
+ def _is_self_reply(self):
+ self_replies = self.search([('parent_id.create_uid', '=', self._uid)])
+ for post in self:
+ post.is_self_reply = post in self_replies
+
+ @api.one
+ @api.depends('child_ids')
+ def _get_child_count(self):
+ self.child_count = len(self.child_ids)
+
+ @api.one
+ def _get_uid_has_answered(self):
+ self.uid_has_answered = any(answer.create_uid.id == self._uid for answer in self.child_ids)
+
+ @api.multi
+ @api.depends('child_ids', 'is_correct')
+ def _get_has_validated_answer(self):
+ correct_posts = [ans.parent_id for ans in self.search([('parent_id', 'in', self._ids), ('is_correct', '=', True)])]
+ for post in self:
+ post.is_correct = post in correct_posts
+
+ # closing
+ closed_reason_id = fields.Many2one('forum.post.reason', string='Reason')
+ closed_uid = fields.Many2one('res.users', string='Closed by', select=1)
+ closed_date = fields.Datetime('Closed on', readonly=True)
+ # karma calculation and access
+ karma_accept = fields.Integer('Convert comment to answer', compute='_get_post_karma_rights')
+ karma_edit = fields.Integer('Karma to edit', compute='_get_post_karma_rights')
+ karma_close = fields.Integer('Karma to close', compute='_get_post_karma_rights')
+ karma_unlink = fields.Integer('Karma to unlink', compute='_get_post_karma_rights')
+ karma_comment = fields.Integer('Karma to comment', compute='_get_post_karma_rights')
+ karma_comment_convert = fields.Integer('Karma to convert comment to answer', compute='_get_post_karma_rights')
+ can_ask = fields.Boolean('Can Ask', compute='_get_post_karma_rights')
+ can_answer = fields.Boolean('Can Answer', compute='_get_post_karma_rights')
+ can_accept = fields.Boolean('Can Accept', compute='_get_post_karma_rights')
+ can_edit = fields.Boolean('Can Edit', compute='_get_post_karma_rights')
+ can_close = fields.Boolean('Can Close', compute='_get_post_karma_rights')
+ can_unlink = fields.Boolean('Can Unlink', compute='_get_post_karma_rights')
+ can_upvote = fields.Boolean('Can Upvote', compute='_get_post_karma_rights')
+ can_downvote = fields.Boolean('Can Downvote', compute='_get_post_karma_rights')
+ can_comment = fields.Boolean('Can Comment', compute='_get_post_karma_rights')
+ can_comment_convert = fields.Boolean('Can Convert to Comment', compute='_get_post_karma_rights')
+
+ @api.one
+ def _get_post_karma_rights(self):
+ user = self.env.user
+
+ self.karma_accept = self.parent_id and self.parent_id.create_uid.id == self._uid and self.forum_id.karma_answer_accept_own or self.forum_id.karma_answer_accept_all
+ self.karma_edit = self.create_uid.id == self._uid and self.forum_id.karma_edit_own or self.forum_id.karma_edit_all
+ self.karma_close = self.create_uid.id == self._uid and self.forum_id.karma_close_own or self.forum_id.karma_close_all
+ self.karma_unlink = self.create_uid.id == self._uid and self.forum_id.karma_unlink_own or self.forum_id.karma_unlink_all
+ self.karma_comment = self.create_uid.id == self._uid and self.forum_id.karma_comment_own or self.forum_id.karma_comment_all
+ self.karma_comment_convert = self.create_uid.id == self._uid and self.forum_id.karma_comment_convert_own or self.forum_id.karma_comment_convert_all
+
+ self.can_ask = user.karma >= self.forum_id.karma_ask
+ self.can_answer = user.karma >= self.forum_id.karma_answer
+ self.can_accept = user.karma >= self.karma_accept
+ self.can_edit = user.karma >= self.karma_edit
+ self.can_close = user.karma >= self.karma_close
+ self.can_unlink = user.karma >= self.karma_unlink
+ self.can_upvote = user.karma >= self.forum_id.karma_upvote
+ self.can_downvote = user.karma >= self.forum_id.karma_downvote
+ self.can_comment = user.karma >= self.karma_comment
+ self.can_comment_convert = user.karma >= self.karma_comment_convert
+
+ @api.model
+ def create(self, vals):
+ post = super(Post, self.with_context(mail_create_nolog=True)).create(vals)
# deleted or closed questions
- if post.parent_id and (post.parent_id.state == 'close' or post.parent_id.active == False):
- osv.except_osv(_('Error !'), _('Posting answer on [Deleted] or [Closed] question is prohibited'))
+ if post.parent_id and (post.parent_id.state == 'close' or post.parent_id.active is False):
+ raise Warning(_('Posting answer on a [Deleted] or [Closed] question is not possible'))
# karma-based access
if not post.parent_id and not post.can_ask:
raise KarmaError('Not enough karma to create a new question')
elif post.parent_id and not post.can_answer:
raise KarmaError('Not enough karma to answer to a question')
# messaging and chatter
- base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
+ base_url = self.env['ir.config_parameter'].get_param('web.base.url')
if post.parent_id:
body = _(
'<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>' %
(post.parent_id.name, base_url, slug(post.parent_id.forum_id), slug(post.parent_id))
)
- 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)
+ post.parent_id.message_post(subject=_('Re: %s') % post.parent_id.name, body=body, subtype='website_forum.mt_answer_new')
else:
body = _(
'<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>' %
(post.name, post.forum_id.name, base_url, slug(post.forum_id), slug(post))
)
- self.message_post(cr, uid, post_id, subject=post.name, body=body, subtype='website_forum.mt_question_new', context=context)
- self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], post.forum_id.karma_gen_question_new, context=context)
- return post_id
+ post.message_post(subject=post.name, body=body, subtype='website_forum.mt_question_new')
+ self.env.user.sudo().add_karma(post.forum_id.karma_gen_question_new)
+ return post
- def write(self, cr, uid, ids, vals, context=None):
- posts = self.browse(cr, uid, ids, context=context)
+ @api.multi
+ def write(self, vals):
if 'state' in vals:
- if vals['state'] in ['active', 'close'] and any(not post.can_close for post in posts):
+ if vals['state'] in ['active', 'close'] and any(not post.can_close for post in self):
raise KarmaError('Not enough karma to close or reopen a post.')
if 'active' in vals:
- if any(not post.can_unlink for post in posts):
+ if any(not post.can_unlink for post in self):
raise KarmaError('Not enough karma to delete or reactivate a post')
if 'is_correct' in vals:
- if any(not post.can_accept for post in posts):
+ if any(not post.can_accept for post in self):
raise KarmaError('Not enough karma to accept or refuse an answer')
# update karma except for self-acceptance
mult = 1 if vals['is_correct'] else -1
- for post in self.browse(cr, uid, ids, context=context):
- if vals['is_correct'] != post.is_correct and post.create_uid.id != uid:
- self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id], post.forum_id.karma_gen_answer_accepted * mult, context=context)
- self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], post.forum_id.karma_gen_answer_accept * mult, context=context)
- 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):
+ for post in self:
+ if vals['is_correct'] != post.is_correct and post.create_uid.id != self._uid:
+ post.create_uid.sudo().add_karma(post.forum_id.karma_gen_answer_accepted * mult)
+ self.env.user.sudo().add_karma(post.forum_id.karma_gen_answer_accept * mult)
+ 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 self):
raise KarmaError('Not enough karma to edit a post.')
- res = super(Post, self).write(cr, uid, ids, vals, context=context)
+ res = super(Post, self).write(vals)
# if post content modify, notify followers
if 'content' in vals or 'name' in vals:
- for post in posts:
+ for post in self:
if post.parent_id:
body, subtype = _('Answer Edited'), 'website_forum.mt_answer_edit'
- obj_id = post.parent_id.id
+ obj_id = post.parent_id
else:
body, subtype = _('Question Edited'), 'website_forum.mt_question_edit'
- obj_id = post.id
- self.message_post(cr, uid, obj_id, body=body, subtype=subtype, context=context)
+ obj_id = post
+ obj_id.message_post(body=body, subtype=subtype)
return res
- def close(self, cr, uid, ids, reason_id, context=None):
- if any(post.parent_id for post in self.browse(cr, uid, ids, context=context)):
+ @api.multi
+ def close(self, reason_id):
+ if any(post.parent_id for post in self):
return False
- return self.pool['forum.post'].write(cr, uid, ids, {
+ self.write({
'state': 'close',
- 'closed_uid': uid,
+ 'closed_uid': self._uid,
'closed_date': datetime.today().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT),
'closed_reason_id': reason_id,
- }, context=context)
+ })
+ return True
- def unlink(self, cr, uid, ids, context=None):
- posts = self.browse(cr, uid, ids, context=context)
- if any(not post.can_unlink for post in posts):
+ @api.multi
+ def unlink(self):
+ if any(not post.can_unlink for post in self):
raise KarmaError('Not enough karma to unlink a post')
# if unlinking an answer with accepted answer: remove provided karma
- for post in posts:
+ for post in self:
if post.is_correct:
- self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id], post.forum_id.karma_gen_answer_accepted * -1, context=context)
- self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], post.forum_id.karma_gen_answer_accept * -1, context=context)
- return super(Post, self).unlink(cr, uid, ids, context=context)
-
- def vote(self, cr, uid, ids, upvote=True, context=None):
- Vote = self.pool['forum.post.vote']
- vote_ids = Vote.search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
+ post.create_uid.sudo().add_karma(post.forum_id.karma_gen_answer_accepted * -1)
+ self.env.user.sudo().add_karma(post.forum_id.karma_gen_answer_accepted * -1)
+ return super(Post, self).unlink()
+
+ @api.multi
+ def vote(self, upvote=True):
+ Vote = self.env['forum.post.vote']
+ vote_ids = Vote.search([('post_id', 'in', self._ids), ('user_id', '=', self._uid)])
new_vote = '1' if upvote else '-1'
voted_forum_ids = set()
if vote_ids:
- for vote in Vote.browse(cr, uid, vote_ids, context=context):
+ for vote in vote_ids:
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)
+ vote.vote = new_vote
voted_forum_ids.add(vote.post_id.id)
- for post_id in set(ids) - voted_forum_ids:
- for post_id in ids:
- 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]], 'user_vote': new_vote}
+ for post_id in set(self._ids) - voted_forum_ids:
+ for post_id in self._ids:
+ Vote.create({'post_id': post_id, 'vote': new_vote})
+ return {'vote_count': self.vote_count, 'user_vote': new_vote}
- def convert_answer_to_comment(self, cr, uid, id, context=None):
+ @api.one
+ def convert_answer_to_comment(self):
""" Tools to convert an answer (forum.post) to a comment (mail.message).
The original post is unlinked and a new comment is posted on the question
using the post create_uid as the comment's author. """
- post = self.browse(cr, SUPERUSER_ID, id, context=context)
- if not post.parent_id:
+ if not self.parent_id:
return False
# karma-based action check: use the post field that computed own/all value
- if not post.can_comment_convert:
+ if not self.can_comment_convert:
raise KarmaError('Not enough karma to convert an answer to a comment')
# post the message
- question = post.parent_id
+ question = self.parent_id
values = {
- 'author_id': post.create_uid.partner_id.id,
- 'body': html2plaintext(post.content),
+ 'author_id': self.create_uid.partner_id.id,
+ 'body': tools.html2plaintext(self.content),
'type': 'comment',
'subtype': 'mail.mt_comment',
- 'date': post.create_date,
+ 'date': self.create_date,
}
- message_id = self.pool['forum.post'].message_post(
- cr, uid, question.id,
- context=dict(context, mail_create_nosubcribe=True),
- **values)
+ new_message = self.browse(question.id).with_context(mail_create_nosubcribe=True).message_post(**values)
# unlink the original answer, using SUPERUSER_ID to avoid karma issues
- self.pool['forum.post'].unlink(cr, SUPERUSER_ID, [post.id], context=context)
+ self.sudo().unlink()
- return message_id
+ return new_message
- def convert_comment_to_answer(self, cr, uid, message_id, default=None, context=None):
+ @api.model
+ def convert_comment_to_answer(self, message_id, default=None):
""" Tool to convert a comment (mail.message) into an answer (forum.post).
The original comment is unlinked and a new answer from the comment's author
is created. Nothing is done if the comment's author already answered the
question. """
- comment = self.pool['mail.message'].browse(cr, SUPERUSER_ID, message_id, context=context)
- post = self.pool['forum.post'].browse(cr, uid, comment.res_id, context=context)
- user = self.pool['res.users'].browse(cr, uid, uid, context=context)
+ comment = self.env['mail.message'].sudo().browse(message_id)
+ post = self.browse(comment.res_id)
if not comment.author_id or not comment.author_id.user_ids: # only comment posted by users can be converted
return False
# karma-based action check: must check the message's author to know if own / all
- 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
- can_convert = uid == SUPERUSER_ID or user.karma >= karma_convert
+ karma_convert = comment.author_id.id == self.env.user.partner_id.id and post.forum_id.karma_comment_convert_own or post.forum_id.karma_comment_convert_all
+ can_convert = self.env.user.karma >= karma_convert
if not can_convert:
raise KarmaError('Not enough karma to convert a comment to an answer')
'parent_id': question.id,
}
# done with the author user to have create_uid correctly set
- new_post_id = self.pool['forum.post'].create(cr, post_create_uid.id, post_values, context=context)
+ new_post = self.sudo(post_create_uid.id).create(post_values)
# delete comment
- self.pool['mail.message'].unlink(cr, SUPERUSER_ID, [comment.id], context=context)
+ comment.unlink()
- return new_post_id
+ return new_post
- def unlink_comment(self, cr, uid, id, message_id, context=None):
- comment = self.pool['mail.message'].browse(cr, SUPERUSER_ID, message_id, context=context)
- post = self.pool['forum.post'].browse(cr, uid, id, context=context)
- user = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
- if not comment.model == 'forum.post' or not comment.res_id == id:
+ @api.one
+ def unlink_comment(self, message_id):
+ user = self.env.user
+ comment = self.env['mail.message'].sudo().browse(message_id)
+ if not comment.model == 'forum.post' or not comment.res_id == self.id:
return False
-
# karma-based action check: must check the message's author to know if own or all
- 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
- can_unlink = uid == SUPERUSER_ID or user.karma >= karma_unlink
+ karma_unlink = comment.author_id.id == user.partner_id.id and self.forum_id.karma_comment_unlink_own or self.forum_id.karma_comment_unlink_all
+ can_unlink = user.karma >= karma_unlink
if not can_unlink:
raise KarmaError('Not enough karma to unlink a comment')
+ return comment.unlink()
- return self.pool['mail.message'].unlink(cr, SUPERUSER_ID, [message_id], context=context)
-
- def set_viewed(self, cr, uid, ids, context=None):
- cr.execute("""UPDATE forum_post SET views = views+1 WHERE id IN %s""", (tuple(ids),))
+ @api.multi
+ def set_viewed(self):
+ self._cr.execute("""UPDATE forum_post SET views = views+1 WHERE id IN %s""", (self._ids,))
return True
- def _get_access_link(self, cr, uid, mail, partner, context=None):
- post = self.pool['forum.post'].browse(cr, uid, mail.res_id, context=context)
+ @api.model
+ def _get_access_link(self, mail, partner):
+ post = self.browse(mail.res_id)
res_id = post.parent_id and "%s#answer-%s" % (post.parent_id.id, post.id) or post.id
return "/forum/%s/question/%s" % (post.forum_id.id, res_id)
else:
post_id = thread_id
post = self.browse(cr, uid, post_id, context=context)
+ # TDE FIXME: trigger browse because otherwise the function field is not compted - check with RCO
+ tmp1, tmp2 = post.karma_comment, post.can_comment
+ user = self.pool['res.users'].browse(cr, uid, uid)
+ tmp3 = user.karma
+ # TDE END FIXME
if not post.can_comment:
raise KarmaError('Not enough karma to comment')
return super(Post, self).message_post(cr, uid, thread_id, type=type, subtype=subtype, context=context, **kwargs)
-class PostReason(osv.Model):
+class PostReason(models.Model):
_name = "forum.post.reason"
_description = "Post Closing Reason"
_order = 'name'
- _columns = {
- 'name': fields.char('Post Reason', required=True, translate=True),
- }
+
+ name = fields.Char(string='Closing Reason', required=True, translate=True)
-class Vote(osv.Model):
+class Vote(models.Model):
_name = 'forum.post.vote'
_description = 'Vote'
- _columns = {
- 'post_id': fields.many2one('forum.post', 'Post', ondelete='cascade', required=True),
- 'user_id': fields.many2one('res.users', 'User', required=True),
- 'vote': fields.selection([('1', '1'), ('-1', '-1'), ('0', '0')], 'Vote', required=True),
- 'create_date': fields.datetime('Create Date', select=True, readonly=True),
-
- # TODO master: store these two
- 'forum_id': fields.related('post_id', 'forum_id', type='many2one', relation='forum.forum', string='Forum'),
- 'recipient_id': fields.related('post_id', 'create_uid', type='many2one', relation='res.users', string='To', help="The user receiving the vote"),
- }
- _defaults = {
- 'user_id': lambda self, cr, uid, ctx: uid,
- 'vote': lambda *args: '1',
- }
+
+ post_id = fields.Many2one('forum.post', string='Post', ondelete='cascade', required=True)
+ user_id = fields.Many2one('res.users', string='User', required=True, default=lambda self: self._uid)
+ vote = fields.Selection([('1', '1'), ('-1', '-1'), ('0', '0')], string='Vote', required=True, default='1')
+ create_date = fields.Datetime('Create Date', select=True, readonly=True)
+ forum_id = fields.Many2one('forum.forum', string='Forum', related="post_id.forum_id", store=True)
+ recipient_id = fields.Many2one('res.users', string='To', related="post_id.create_uid", store=True)
def _get_karma_value(self, old_vote, new_vote, up_karma, down_karma):
_karma_upd = {
}
return _karma_upd[old_vote][new_vote]
- def create(self, cr, uid, vals, context=None):
- vote_id = super(Vote, self).create(cr, uid, vals, context=context)
- vote = self.browse(cr, uid, vote_id, context=context)
+ @api.model
+ def create(self, vals):
+ vote = super(Vote, self).create(vals)
# own post check
if vote.user_id.id == vote.post_id.create_uid.id:
elif vote.vote == '-1' and not vote.post_id.can_downvote:
raise KarmaError('Not enough karma to downvote.')
- # karma update
if vote.post_id.parent_id:
karma_value = self._get_karma_value('0', vote.vote, vote.forum_id.karma_gen_answer_upvote, vote.forum_id.karma_gen_answer_downvote)
else:
karma_value = self._get_karma_value('0', vote.vote, vote.forum_id.karma_gen_question_upvote, vote.forum_id.karma_gen_question_downvote)
- self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [vote.recipient_id.id], karma_value, context=context)
- return vote_id
+ vote.recipient_id.sudo().add_karma(karma_value)
+ return vote
- def write(self, cr, uid, ids, values, context=None):
+ @api.multi
+ def write(self, values):
if 'vote' in values:
- for vote in self.browse(cr, uid, ids, context=context):
+ for vote in self:
# own post check
if vote.user_id.id == vote.post_id.create_uid.id:
raise Warning('Not allowed to vote for its own post')
karma_value = self._get_karma_value(vote.vote, values['vote'], vote.forum_id.karma_gen_answer_upvote, vote.forum_id.karma_gen_answer_downvote)
else:
karma_value = self._get_karma_value(vote.vote, values['vote'], vote.forum_id.karma_gen_question_upvote, vote.forum_id.karma_gen_question_downvote)
- self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [vote.recipient_id.id], karma_value, context=context)
- res = super(Vote, self).write(cr, uid, ids, values, context=context)
+ vote.recipient_id.sudo().add_karma(karma_value)
+ res = super(Vote, self).write(values)
return res
-class Tags(osv.Model):
+class Tags(models.Model):
_name = "forum.tag"
- _description = "Tag"
+ _description = "Forum Tag"
_inherit = ['website.seo.metadata']
- def _get_posts_count(self, cr, uid, ids, field_name, arg, context=None):
- return dict((tag_id, self.pool['forum.post'].search_count(cr, uid, [('tag_ids', 'in', tag_id)], context=context)) for tag_id in ids)
-
- def _get_tag_from_post(self, cr, uid, ids, context=None):
- return list(set(
- [tag.id for post in self.pool['forum.post'].browse(cr, SUPERUSER_ID, ids, context=context) for tag in post.tag_ids]
- ))
-
- _columns = {
- 'name': fields.char('Name', required=True),
- 'forum_id': fields.many2one('forum.forum', 'Forum', required=True),
- 'post_ids': fields.many2many('forum.post', 'forum_tag_rel', 'tag_id', 'post_id', 'Posts'),
- 'posts_count': fields.function(
- _get_posts_count, type='integer', string="Number of Posts",
- store={
- 'forum.post': (_get_tag_from_post, ['tag_ids'], 10),
- }
- ),
- 'create_uid': fields.many2one('res.users', 'Created by', readonly=True),
- }
+ name = fields.Char('Name', required=True)
+ create_uid = fields.Many2one('res.users', string='Created by', readonly=True)
+ forum_id = fields.Many2one('forum.forum', string='Forum', required=True)
+ post_ids = fields.Many2many('forum.post', 'forum_tag_rel', 'forum_tag_id', 'forum_id', string='Posts')
+ posts_count = fields.Integer('Number of Posts', compute='_get_posts_count', store=True)
+
+ @api.multi
+ @api.depends("post_ids.tag_ids")
+ def _get_posts_count(self):
+ for tag in self:
+ tag.posts_count = len(tag.post_ids)
# -*- coding: utf-8 -*-
-from openerp.osv import osv, fields
+from openerp import models, fields, api
-class gamification_challenge(osv.Model):
+class gamification_challenge(models.Model):
_inherit = 'gamification.challenge'
- def _get_categories(self, cr, uid, context=None):
- res = super(gamification_challenge, self)._get_categories(cr, uid, context=context)
+ @api.model
+ def _get_categories(self):
+ res = super(gamification_challenge, self)._get_categories()
res.append(('forum', 'Website / Forum'))
return res
-class Badge(osv.Model):
+class Badge(models.Model):
_inherit = 'gamification.badge'
- _columns = {
- 'level': fields.selection([('bronze', 'bronze'), ('silver', 'silver'), ('gold', 'gold')], 'Forum Badge Level'),
- }
+
+ level = fields.Selection([('bronze', 'bronze'), ('silver', 'silver'), ('gold', 'gold')], string='Forum Badge Level')
+
+
+class UserBadge(models.Model):
+ _inherit = 'gamification.badge.user'
+
+ level = fields.Selection(
+ [('bronze', 'bronze'),
+ ('silver', 'silver'),
+ ('gold', 'gold')],
+ string='Forum Badge Level',
+ related="badge_id.level", store=True)
# -*- coding: utf-8 -*-
from datetime import datetime
-from urllib import urlencode
-
import hashlib
+from urllib import urlencode
-from openerp import SUPERUSER_ID
-from openerp.osv import osv, fields
+from openerp import models, fields, api
-class Users(osv.Model):
+class Users(models.Model):
_inherit = 'res.users'
def __init__(self, pool, cr):
['country_id', 'city', 'website', 'website_description', 'website_published']))
return init_res
- 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['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
+ create_date = fields.Datetime('Create Date', readonly=True, copy=False, select=True)
+ karma = fields.Integer('Karma', default=0)
+ badge_ids = fields.One2many('gamification.badge.user', 'user_id', string='Badges', copy=False)
+ gold_badge = fields.Integer('Gold badges count', compute="_get_user_badge_level")
+ silver_badge = fields.Integer('Silver badges count', compute="_get_user_badge_level")
+ bronze_badge = fields.Integer('Bronze badges count', compute="_get_user_badge_level")
- _columns = {
- 'create_date': fields.datetime('Create Date', select=True, readonly=True),
- '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'),
- }
+ @api.multi
+ @api.depends('badge_ids')
+ def _get_user_badge_level(self):
+ """ Return total badge per level of users
+ TDE CLEANME: shouldn't check type is forum ? """
+ badge_groups = self.env['gamification.badge.user'].read_group(
+ [('level', 'in', ['gold', 'silver', 'bronze'])],
+ ['user_id', 'level', 'badge_id'],
+ ['user_id', 'level'],
+ lazy=False)
+ badge_data = dict()
+ for group in badge_groups:
+ badge_data.setdefault(group['user_id'][0], dict())[group['level']] = group['__count']
+ for user in self:
+ user.gold_badge = badge_data.get(user.id) and badge_data[user.id].get('gold', 0) or 0
+ user.silver_badge = badge_data.get(user.id) and badge_data[user.id].get('silver', 0) or 0
+ user.bronze_badge = badge_data.get(user.id) and badge_data[user.id].get('bronze', 0) or 0
- _defaults = {
- 'karma': 0,
- }
-
- def _generate_forum_token(self, cr, uid, user_id, email):
+ @api.model
+ def _generate_forum_token(self, user_id, email):
"""Return a token for email validation. This token is valid for the day
and is a hash based on a (secret) uuid generated by the forum module,
the user_id, the email and currently the day (to be updated if necessary). """
- forum_uuid = self.pool.get('ir.config_parameter').get_param(cr, SUPERUSER_ID, 'website_forum.uuid')
+ forum_uuid = self.env['ir.config_parameter'].sudo().get_param('website_forum.uuid')
return hashlib.sha256('%s-%s-%s-%s' % (
datetime.now().replace(hour=0, minute=0, second=0, microsecond=0),
forum_uuid,
user_id,
email)).hexdigest()
- def send_forum_validation_email(self, cr, uid, user_id, forum_id=None, context=None):
- user = self.pool['res.users'].browse(cr, uid, user_id, context=context)
- token = self._generate_forum_token(cr, uid, user_id, user.email)
- activation_template_id = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'website_forum.validation_email')
+ @api.one
+ def send_forum_validation_email(self, forum_id=None):
+ token = self._generate_forum_token(self.id, self.email)
+ activation_template_id = self.env['ir.model.data'].xmlid_to_res_id('website_forum.validation_email')
if activation_template_id:
params = {
'token': token,
- 'id': user_id,
- 'email': user.email}
+ 'id': self.id,
+ 'email': self.email}
if forum_id:
params['forum_id'] = forum_id
- base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
+ base_url = self.env['ir.config_parameter'].get_param('web.base.url')
token_url = base_url + '/forum/validate_email?%s' % urlencode(params)
- tpl_ctx = dict(context, token_url=token_url)
- self.pool['email.template'].send_mail(cr, SUPERUSER_ID, activation_template_id, user_id, force_send=True, context=tpl_ctx)
+ self.env['email.template'].sudo().with_context(token_url=token_url).send_mail(activation_template_id, self.id, force_send=True)
return True
- def process_forum_validation_token(self, cr, uid, token, user_id, email, forum_id=None, context=None):
- validation_token = self.pool['res.users']._generate_forum_token(cr, uid, user_id, email)
- user = self.pool['res.users'].browse(cr, SUPERUSER_ID, user_id, context=context)
- if token == validation_token and user.karma == 0:
+ @api.one
+ def process_forum_validation_token(self, token, email, forum_id=None, context=None):
+ validation_token = self._generate_forum_token(self.id, email)
+ if token == validation_token and self.karma == 0:
karma = 3
- if not forum_id:
- forum_ids = self.pool['forum.forum'].search(cr, uid, [], limit=1, context=context)
- if forum_ids:
- forum_id = forum_ids[0]
+ forum = None
if forum_id:
- forum = self.pool['forum.forum'].browse(cr, uid, forum_id, context=context)
+ forum = self.env['forum.forum'].browse(forum_id)
+ else:
+ forum_ids = self.env['forum.forum'].search([], limit=1)
+ if forum_ids:
+ forum = forum_ids[0]
+ if forum:
# karma gained: karma to ask a question and have 2 downvotes
karma = forum.karma_ask + (-2 * forum.karma_gen_question_downvote)
- return user.write({'karma': karma})
+ return self.write({'karma': karma})
return False
- 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)
+ @api.multi
+ def add_karma(self, karma):
+ for user in self:
+ user.karma += karma
return True
- def get_serialised_gamification_summary(self, cr, uid, excluded_categories=None, context=None):
+ @api.model
+ def get_serialised_gamification_summary(self, excluded_categories=None):
if isinstance(excluded_categories, list):
if 'forum' not in excluded_categories:
excluded_categories.append('forum')
else:
excluded_categories = ['forum']
- return super(Users, self).get_serialised_gamification_summary(cr, uid, excluded_categories=excluded_categories, context=context)
+ return super(Users, self).get_serialised_gamification_summary()
<field name="allow_question"/>
<field name="allow_link"/>
<field name="allow_discussion"/>
- <field name="default_allow"/>
+ <field name="default_post_type"/>
</group>
<group string="Orders">
<field name="default_order"/>
- <label for="relevancy_option_first" string="Relevancy Computation"/>
+ <label for="relevancy_post_vote" string="Relevancy Computation"/>
<div>
- (votes - 1) ** <field name="relevancy_option_first" class="oe_inline"/> / (days + 2) ** <field name="relevancy_option_second" class="oe_inline"/>
+ (votes - 1) ** <field name="relevancy_post_vote" class="oe_inline"/> / (days + 2) ** <field name="relevancy_time_decay" class="oe_inline"/>
</div>
</group>
</page>
<t t-call="website.layout">
<div t-if="is_public_user and not no_introduction_message" class="alert alert-success alert-dismissable">
<div class="container">
- <div t-field="forum.introduction_message"/>
+ <h1 class="mt0">Welcome!</h1>
+ <div t-field="forum.description"/>
<a class='btn btn-primary' t-attf-href="/web?redirect=#{ request.httprequest.url }">Register</a>
<button type="button" class="btn btn-link js_close_intro" data-dismiss="alert" aria-hidden="true">Hide Intro</button>
</div>
</div>
<div class="col-sm-3" id="right-column">
<div t-if="not header.get('ask_hide')" t-attf-class="btn-group btn-block mb16 #{(user.karma < forum.karma_ask) and 'karma_required' or ''}" t-attf-data-karma="#{forum.karma_ask}">
- <a type="button" class="btn btn-primary btn-lg col-sm-10" t-attf-href="/forum/#{slug(forum)}/#{forum.default_allow}">
- <t t-if="forum.default_allow == 'ask_question'">Ask a Question</t>
- <t t-if="forum.default_allow == 'post_link'">Submit a Post</t>
- <t t-if="forum.default_allow == 'post_discussion'">New Discussion</t>
+ <a type="button" class="btn btn-primary btn-lg col-sm-10" t-attf-href="/forum/#{slug(forum)}/ask?post_type=#{forum.default_post_type}">
+ <t t-if="forum.default_post_type == 'question'">Ask a Question</t>
+ <t t-if="forum.default_post_type == 'link'">Submit a Link</t>
+ <t t-if="forum.default_post_type == 'discussion'">Launch a Discussion</t>
</a>
<button type="button" class="btn btn-primary btn-lg col-sm-2 dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Select Post</span>
</button>
<ul class="dropdown-menu" role="menu">
- <li t-if="forum.allow_link"><a t-attf-href="/forum/#{slug(forum)}/post_link">Submit a Post</a></li>
- <li t-if="forum.allow_question"><a t-attf-href="/forum/#{slug(forum)}/ask_question">Ask a Question</a></li>
- <li t-if="forum.allow_discussion"><a t-attf-href="/forum/#{slug(forum)}/post_discussion">Launch a Discussion</a></li>
+ <li t-if="forum.allow_question"><a t-attf-href="/forum/#{slug(forum)}/ask?post_type=question">Ask a Question</a></li>
+ <li t-if="forum.allow_link"><a t-attf-href="/forum/#{slug(forum)}/ask?post_type=link">Submit a Link</a></li>
+ <li t-if="forum.allow_discussion"><a t-attf-href="/forum/#{slug(forum)}/ask?post_type=discussion">Launch a Discussion</a></li>
</ul>
</div>
<div class="panel panel-default">
</div>
<div class="col-md-10 clearfix">
<div class="question-name">
- <t t-if="question.type == 'link'">
+ <t t-if="question.post_type == 'link'">
<a t-att-href="question.content_link" t-raw="question.name"/>
</t>
- <t t-if="question.type in ('question', 'discussion')">
+ <t t-if="question.post_type in ('question', 'discussion')">
<a t-attf-href="/forum/#{ slug(forum) }/question/#{ slug(question) }" t-field="question.name"/>
</t>
<span t-if="not question.active"><b> [Deleted]</b></span>
</template>
<!-- Edition: Post Article -->
-<template id="post_link">
+<template id="new_link">
<t t-call="website_forum.header">
<h1 class="mt0">Submit a Link</h1>
<p class="mb32">
We keep a high level of quality in showcased posts, only 20% of the submited
posts will be featured.
</p>
- <form t-attf-action="/forum/#{ slug(forum) }/link/new" method="post" role="form" class="tag_text form-horizontal">
+ <form t-attf-action="/forum/#{ slug(forum) }/new?post_type=link" method="post" role="form" class="tag_text form-horizontal">
<input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
<div class="form-group">
<label class="col-sm-2 control-label" for="content_link">URL to Share</label>
</template>
<!-- Edition: Post your Discussion Topic -->
-<template id="post_discussion">
+<template id="new_discussion">
<t t-call="website_forum.header">
<h1 class="mt0">Post Your Discussion Topic</h1>
<p>
<b>Share</b> Start Something Awesome
</p>
- <form t-attf-action="/forum/#{ slug(forum) }/discussion/new" method="post" role="form" class="tag_text">
+ <form t-attf-action="/forum/#{slug(forum)}/new?post_type=discussion" method="post" role="form" class="tag_text">
<input type="text" name="post_name" required="True" t-attf-value="#{post_name}"
class="form-control mb16" placeholder="Your Discussion Title..."/>
<input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
<br/>
<input type="text" name="post_tags" placeholder="Tags" class="form-control load_tags"/>
<br/>
- <button class="btn btn-primary" id="btn_ask_your_question">Post Your Topic</button>
+ <button class="btn btn-primary">Post Your Topic</button>
</form>
</t>
</template>
<!-- Edition: ask your question -->
-<template id="ask_question">
+<template id="new_question">
<t t-call="website_forum.header">
<t t-set="head">
<script type="text/javascript">
<li>Avoid unnecessary introductions (Hi,... Please... Thanks...),</li>
<li>Provide enough details and, if possible, give an example.</li>
</ul>
- <form t-attf-action="/forum/#{ slug(forum) }/question/new" method="post" role="form" class="tag_text">
+ <form t-attf-action="/forum/#{slug(forum)}/new?post_type=question" method="post" role="form" class="tag_text">
<input type="text" name="post_name" required="True" t-attf-value="#{post_name}"
class="form-control mb16" placeholder="Your Question Title..."/>
<input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
<input type="text" name="post_tags" placeholder="Tags" class="form-control load_tags"/>
<br/>
<button t-attf-class="btn btn-primary #{(user.karma < forum.karma_ask) and 'karma_required' or ''}"
- id="btn_ask_your_question" t-att-data-karma="forum.karma_ask">Post Your Question</button>
+ t-att-data-karma="forum.karma_ask">Post Your Question</button>
</form>
</t>
</template>
<!-- Edition: edit a post -->
<template id="edit_post">
<t t-call="website_forum.header">
- <h3 t-if="not is_answer">Edit <span t-field="post.type"/></h3>
+ <h3 t-if="not is_answer">Edit <span t-field="post.post_type"/></h3>
<h3 t-if="is_answer">Edit reply</h3>
<form t-attf-action="/forum/#{slug(forum)}/post/#{slug(post)}/save" method="post" role="form" class="tag_text">
<div t-if="not is_answer">
<input type="text" name="post_name" required="True"
t-attf-value="#{post.name}" class="form-control mb8" placeholder="Edit your Post"/>
- <h5 t-if="post.type == 'question'" class="mt20">Please enter a descriptive question (should finish by a '?')</h5>
+ <h5 t-if="post.post_type == 'question'" class="mt20">Please enter a descriptive question (should finish by a '?')</h5>
</div>
<input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
<textarea name="content" id="content" required="True" class="form-control load_editor">
<h3 class="mt10">Your Reply</h3>
<input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
<textarea name="content" t-attf-id="content-#{str(object.id)}" class="form-control load_editor" required="True"/>
- <button class="btn btn-primary" id="btn_ask_your_question">Post Your Reply</button>
+ <button class="btn btn-primary">Post Your Reply</button>
</form>
</div>
</template>
<!-- Edition: post an answer -->
<template id="post_answer">
<h3 class="mt10">Your Reply</h3>
- <p t-if="question.type == 'question'">
+ <p t-if="question.post_type == 'question'">
<b>Please try to give a substantial answer.</b> If you wanted to comment on the question or answer, just
<b>use the commenting tool.</b> Please remember that you can always <b>revise your answers</b>
- no need to answer the same question twice. Also, please <b>don't forget to vote</b>
<input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
<textarea name="content" t-attf-id="content-#{str(question.id)}" class="form-control load_editor" required="True"/>
<button t-attf-class="btn btn-primary mt16 #{not question.can_answer and 'karma_required' or ''}"
- id="btn_ask_your_question" t-att-data-karma="question.karma_answer">Post Your Reply</button>
+ t-att-data-karma="question.forum_id.karma_answer">Post Your Reply</button>
</form>
</template>
<template id="vote">
<div t-attf-class="box oe_grey">
<a t-attf-class="vote_up fa fa-thumbs-up no-decoration #{post.user_vote == 1 and 'text-success' or ''} #{((post.user_vote == 1 and not post.can_downvote) or not post.can_upvote) and 'karma_required' or ''}"
- t-attf-data-karma="#{post.user_vote == 1 and post.karma_downvote or post.karma_upvote}"
+ t-attf-data-karma="#{post.user_vote == 1 and post.forum_id.karma_downvote or post.forum_id.karma_upvote}"
t-attf-data-href="/forum/#{slug(post.forum_id)}/post/#{slug(post)}/upvote"/>
<span id="vote_count" t-esc="post.vote_count"/>
<a t-attf-class="vote_down fa fa-thumbs-down no-decoration #{post.user_vote == -1 and 'text-warning' or ''} #{((post.user_vote == -1 and not post.can_upvote) or not post.can_downvote) and 'karma_required' or ''}"
- t-attf-data-karma="#{post.user_vote == -1 and post.karma_upvote or post.karma_downvote}"
+ t-attf-data-karma="#{post.user_vote == -1 and post.forum_id.karma_upvote or post.forum_id.karma_downvote}"
t-attf-data-href="/forum/#{slug(post.forum_id)}/post/#{slug(post)}/downvote"/>
<div t-if="vote_count > 1" class="subtitle">
votes
</div>
<div t-attf-class="col-md-10 #{not question.active and 'alert alert-danger' or ''}">
<h1 class="mt0">
- <t t-if="question.type == 'link'">
+ <t t-if="question.post_type == 'link'">
<a t-att-href="question.content_link" t-raw="question.name"/>
</t>
- <t t-if="question.type in ('question', 'discussion')">
+ <t t-if="question.post_type in ('question', 'discussion')">
<a t-attf-href="/forum/#{ slug(forum) }/question/#{ slug(question) }" t-field="question.name"/>
</t>
<span t-if="not question.active"><b> [Deleted]</b></span>
</h1>
<div class="alert alert-info text-center" t-if="question.state == 'close'">
<p class="mt16">
- <b>The <i t-field="question.type"/> has been closed<t t-if="question.closed_reason_id"> for reason: <i t-esc="question.closed_reason_id.name"/></t></b>
+ <b>The <i t-field="question.post_type"/> has been closed<t t-if="question.closed_reason_id"> for reason: <i t-esc="question.closed_reason_id.name"/></t></b>
</p>
<t t-if="question.closed_uid">
<b>by <a t-attf-href="/forum/#{ slug(forum) }/user/#{ question.closed_uid.id }"
</t>
</div>
</div>
- <div t-if="question.type != 'link'"><t t-raw="question.content"/></div>
+ <div t-if="question.post_type != 'link'"><t t-raw="question.content"/></div>
<div class="mt16 clearfix">
<div class="pull-right">
<div class="text-right">
</t>
</div>
<ul class="list-inline" id="options">
- <li t-if="question.type == 'question'">
+ <li t-if="question.post_type == 'question'">
<a style="cursor: pointer" t-att-data-toggle="question.can_comment and 'collapse' or ''"
t-attf-class="fa fa-comment-o #{not question.can_comment and 'karma_required text-muted' or ''}"
t-attf-data-karma="#{not question.can_comment and question.karma_comment or 0}"
<t t-set="answer" t-value="post_answer"/>
</t>
</div>
- <div t-if="question.type != 'question' or question.type == 'question' and not question.uid_has_answered and question.state != 'close' and question.active != False">
+ <div t-if="question.post_type != 'question' or question.post_type == 'question' and not question.uid_has_answered and question.state != 'close' and question.active != False">
<t t-call="website_forum.post_answer"/>
</div>
- <div t-if="question.type == 'question' and question.uid_has_answered" class="mb16">
+ <div t-if="question.post_type == 'question' and question.uid_has_answered" class="mb16">
<a class="btn btn-primary" t-attf-href="/forum/#{slug(forum)}/question/#{slug(question)}/edit_answer">Edit Your Previous Answer</a>
<span class="text-muted">(only one answer per question is allowed)</span>
</div>
<t t-call="website_forum.vote">
<t t-set="post" t-value="answer"/>
</t>
- <div t-if="question.type == 'question'" class="text-muted mt8">
+ <div t-if="question.post_type == 'question'" class="text-muted mt8">
<a t-attf-class="accept_answer fa fa-2x fa-check-circle no-decoration #{answer.is_correct and 'oe_answer_true' or 'oe_answer_false'} #{not answer.can_accept and 'karma_required' or ''}"
t-attf-data-karma="#{answer.karma_accept}"
t-attf-data-href="/forum/#{slug(question.forum_id)}/post/#{slug(answer)}/toggle_correct"/>
<t t-raw="answer.content"/>
<div class="mt16">
<ul class="list-inline pull-right">
- <li t-if="question.type == 'question'">
+ <li t-if="question.post_type == 'question'">
<a t-attf-class="fa fa-comment-o #{not answer.can_comment and 'karma_required text-muted' or ''}"
t-attf-data-karma="#{not answer.can_comment and answer.karma_comment or 0}"
style="cursor: pointer" t-att-data-toggle="answer.can_comment and 'collapse' or ''"
t-attf-data-target="#comment#{ answer._name.replace('.','') + '-' + str(answer.id) }"> Comment
</a>
</li>
- <li t-if="question.type != 'question' and not answer.parent_id or answer.parent_id and not answer.parent_id.parent_id">
+ <li t-if="question.post_type != 'question' and not answer.parent_id or answer.parent_id and not answer.parent_id.parent_id">
<a t-attf-class="fa fa-comment-o #{not answer.can_comment and 'karma_required text-muted' or ''}"
t-attf-data-karma="#{not answer.can_comment and answer.karma_comment or 0}"
style="cursor: pointer" data-toggle="collapse"
<t t-set="karma" t-value="not answer.can_unlink and answer.karma_unlink or 0"/>
</t>
</li>
- <li t-if="question.type == 'question'">
+ <li t-if="question.post_type == 'question'">
<t t-call="website_forum.link_button">
<t t-set="url" t-value="'/forum/' + slug(forum) + '/post/' + slug(answer) + '/convert_to_comment'"/>
<t t-set="label" t-value="'Convert as a comment'"/>
</div>
</div>
</div>
- <t t-if="answer.type == 'question'" t-call="website_forum.post_comment">
+ <t t-if="answer.post_type == 'question'" t-call="website_forum.post_comment">
<t t-set="object" t-value="answer"/>
</t>
- <div t-if="answer.type != 'question' and question.state != 'close' and question.active != False">
+ <div t-if="answer.post_type != 'question' and question.state != 'close' and question.active != False">
<t t-call="website_forum.post_reply">
<t t-set="object" t-value="answer"/>
</t>