diff --git a/addons/website_forum/__openerp__.py b/addons/website_forum/__openerp__.py index 439c3df15a4..0fce7b6fe05 100644 --- a/addons/website_forum/__openerp__.py +++ b/addons/website_forum/__openerp__.py @@ -39,6 +39,7 @@ Ask questions, get answers, no distractions 'views/forum.xml', 'views/res_users.xml', 'views/website_forum.xml', + 'views/ir_qweb.xml', 'security/ir.model.access.csv', 'data/badges_question.xml', 'data/badges_answer.xml', diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py index 6804e293b4c..6225698094d 100644 --- a/addons/website_forum/controllers/main.py +++ b/addons/website_forum/controllers/main.py @@ -1,103 +1,27 @@ # -*- coding: utf-8 -*- +from datetime import datetime import werkzeug.urls import simplejson from openerp import tools from openerp import SUPERUSER_ID from openerp.addons.web import http -from openerp.tools import html2plaintext - -from openerp.tools.translate import _ -from datetime import datetime, timedelta +from openerp.addons.web.controllers.main import login_redirect from openerp.addons.web.http import request - -from dateutil.relativedelta import relativedelta from openerp.addons.website.controllers.main import Website as controllers from openerp.addons.website.models.website import slug -from openerp.addons.web.controllers.main import login_redirect +from openerp.tools import html2plaintext +from openerp.tools.translate import _ controllers = controllers() class WebsiteForum(http.Controller): + _post_per_page = 10 + _user_per_page = 30 - @http.route(['/forum'], type='http', auth="public", website=True, multilang=True) - def forum(self, **searches): - cr, uid, context = request.cr, request.uid, request.context - Forum = request.registry['website.forum'] - obj_ids = Forum.search(cr, uid, [], context=context) - forums = Forum.browse(cr, uid, obj_ids, context=context) - return request.website.render("website_forum.forum_index", {'forums': forums}) - - @http.route('/forum/new', type='http', auth="user", multilang=True, website=True) - def forum_create(self, forum_name="New Forum", **kwargs): - forum_id = request.registry['website.forum'].create(request.cr, request.uid, { - 'name': forum_name, - }, context=request.context) - return request.redirect("/forum/%s" % slug(forum_id)) - - @http.route(['/forum/', - '/forum//page/', - '/forum//tag//questions' - ], type='http', auth="public", website=True, multilang=True) - def questions(self, forum, tag='', page=1, filters='', sorting='', search='', **searches): - cr, uid, context = request.cr, request.uid, request.context - Forum = request.registry['website.forum.post'] - user = request.registry['res.users'].browse(cr, uid, uid, context=context) - - order = "id desc" - - domain = [('forum_id', '=', forum.id), ('parent_id', '=', False)] - if search: - domain += ['|', ('name', 'ilike', search), ('content', 'ilike', search)] - - #filter questions for tag. - if tag: - if not filters: - filters = 'tag' - domain += [('tag_ids', 'in', tag.id)] - - if not filters: - filters = 'all' - if filters == 'unanswered': - domain += [ ('child_ids', '=', False) ] - if filters == 'followed': - domain += [ ('message_follower_ids', '=', user.partner_id.id) ] - - # Note: default sorting should be based on last activity - if not sorting or sorting == 'date': - sorting = 'date' - order = 'write_date desc' - if sorting == 'answered': - order = 'child_count desc' - if sorting == 'vote': - order = 'vote_count desc' - - step = 10 - question_count = Forum.search(cr, uid, domain, count=True, context=context) - pager = request.website.pager(url="/forum/%s" % slug(forum), total=question_count, page=page, step=step, scope=10) - - obj_ids = Forum.search(cr, uid, domain, limit=step, offset=pager['offset'], order=order, context=context) - question_ids = Forum.browse(cr, uid, obj_ids, context=context) - - values = { - 'uid': request.session.uid, - 'total_questions': question_count, - 'question_ids': question_ids, - 'notifications': self._get_notifications(), - 'forum': forum, - 'pager': pager, - 'tag': tag, - 'filters': filters, - 'sorting': sorting, - 'search': search, - 'searches': searches, - } - - return request.website.render("website_forum.index", values) - - def _get_notifications(self, **kwargs): + def _get_notifications(self): cr, uid, context = request.cr, request.uid, request.context Message = request.registry['mail.message'] BadgeUser = request.registry['gamification.badge.user'] @@ -105,89 +29,412 @@ class WebsiteForum(http.Controller): badgeuser_ids = BadgeUser.search(cr, uid, [('user_id', '=', uid)], context=context) notification_ids = Message.search(cr, uid, [('res_id', 'in', badgeuser_ids), ('model', '=', 'gamification.badge.user'), ('to_read', '=', True)], context=context) notifications = Message.browse(cr, uid, notification_ids, context=context) - user = request.registry['res.users'].browse(cr, uid, uid, context=context) - return {"user": user, "notifications": notifications} + return notifications + + def _prepare_forum_values(self, forum=None, **kwargs): + user = request.registry['res.users'].browse(request.cr, request.uid, request.uid, context=request.context) + public_uid = request.registry['website'].get_public_user(request.cr, request.uid, request.context) + values = {'user': user, 'is_public_user': user.id == public_uid, + 'notifications': self._get_notifications(), + 'header': kwargs.get('header', dict()), + 'searches': kwargs.get('searches', dict())} + values.update(kwargs) + if forum: + values['forum'] = forum + elif kwargs.get('forum_id'): + values['forum'] = request.registry['forum.forum'].browse(request.cr, request.uid, kwargs['forum_id'], context=request.context) + return values + + # Forum + # -------------------------------------------------- + + @http.route(['/forum'], type='http', auth="public", website=True, multilang=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) + return request.website.render("website_forum.forum_all", {'forums': forums}) + + @http.route('/forum/new', type='http', auth="user", multilang=True, 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" % slug(forum_id)) @http.route('/forum/notification_read', type='json', auth="user", multilang=True, methods=['POST'], website=True) - def notification_read(self, **kwarg): - request.registry['mail.message'].set_message_read(request.cr, request.uid, [int(kwarg.get('notification_id'))], read=True, context=request.context) + 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) return True - @http.route(['/forum//faq'], type='http', auth="public", website=True, multilang=True) + @http.route(['/forum/', + '/forum//page/', + '/forum//tag//questions' + ], type='http', auth="public", website=True, multilang=True) + def questions(self, forum, tag=None, page=1, filters='all', sorting='date', search='', **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) + + domain = [('forum_id', '=', forum.id), ('parent_id', '=', False)] + if search: + domain += ['|', ('name', 'ilike', search), ('content', 'ilike', search)] + if tag: + domain += [('tag_ids', 'in', tag.id)] + if filters == 'unanswered': + domain += [('child_ids', '=', False)] + elif filters == 'followed': + domain += [('message_follower_ids', '=', user.partner_id.id)] + else: + filters = 'all' + + if sorting == 'answered': + order = 'child_count desc' + elif sorting == 'vote': + order = 'vote_count desc' + else: + sorting = 'date' + order = 'write_date desc' + + question_count = Post.search(cr, uid, domain, count=True, context=context) + pager = request.website.pager(url="/forum/%s" % slug(forum), total=question_count, page=page, step=self._post_per_page, scope=self._post_per_page) + + obj_ids = Post.search(cr, uid, domain, limit=self._post_per_page, offset=pager['offset'], order=order, context=context) + question_ids = Post.browse(cr, uid, obj_ids, context=context) + + values = self._prepare_forum_values(forum=forum, searches=post) + values.update({ + 'question_ids': question_ids, + 'pager': pager, + 'tag': tag, + 'filters': filters, + 'sorting': sorting, + 'search': search, + }) + return request.website.render("website_forum.forum_index", values) + + @http.route(['/forum//faq'], type='http', auth="public", website=True, multilang=True) def faq(self, forum, **post): - values = { - 'searches': {}, - 'forum':forum, - 'notifications': self._get_notifications(), - } + values = self._prepare_forum_values(forum=forum, searches=dict(), **post) return request.website.render("website_forum.faq", values) - @http.route(['/forum//question/'], type='http', auth="public", website=True, multilang=True) + @http.route('/forum/get_tags', type='http', auth="public", multilang=True, methods=['GET'], website=True) + def tag_read(self, **post): + tags = request.registry['forum.tag'].search_read(request.cr, request.uid, [], ['name'], context=request.context) + data = [tag['name'] for tag in tags] + return simplejson.dumps(data) + + @http.route(['/forum//tag'], type='http', auth="public", website=True, multilang=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)], limit=None, context=context) + tags = Tag.browse(cr, uid, obj_ids, context=context) + values = self._prepare_forum_values(forum=forum, searches={'tags': True}, **post) + values.update({ + 'tags': tags, + }) + return request.website.render("website_forum.tag", values) + + # Questions + # -------------------------------------------------- + + @http.route(['/forum//ask'], type='http', auth="public", website=True, multilang=True) + def question_ask(self, forum, **post): + if not request.session.uid: + return login_redirect() + values = self._prepare_forum_values(forum=forum, searches={}, header={'ask_hide': True}) + return request.website.render("website_forum.ask_question", values) + + @http.route('/forum//question/new', type='http', auth="user", multilang=True, methods=['POST'], website=True) + def register_question(self, forum, **post): + cr, uid, context = request.cr, request.uid, request.context + Tag = request.registry['forum.tag'] + question_tag_ids = [] + if post.get('question_tags').strip('[]'): + tags = post.get('question_tags').strip('[]').replace('"', '').split(",") + for tag in tags: + tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context) + if tag_ids: + question_tag_ids.append((4, tag_ids[0])) + else: + question_tag_ids.append((0, 0, {'name': tag, 'forum_id': forum.id})) + + new_question_id = request.registry['forum.post'].create( + request.cr, request.uid, { + 'forum_id': forum.id, + 'name': post.get('question_name'), + 'content': post.get('content'), + 'tag_ids': question_tag_ids, + }, context=context) + return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), new_question_id)) + + @http.route(['/forum//question/'], type='http', auth="public", website=True, multilang=True) def question(self, forum, question, **post): cr, uid, context = request.cr, request.uid, request.context # increment view counter - request.registry['website.forum.post'].set_viewed(cr, uid, [question.id], context=context) - - #Check that user have answered question or not. - answer_done = False - for answer in question.child_ids: - if answer.user_id.id == request.uid: - answer_done = True + request.registry['forum.post'].set_viewed(cr, SUPERUSER_ID, [question.id], context=context) filters = 'question' - user = request.registry['res.users'].browse(cr, uid, uid, context=None) - values = { + values = self._prepare_forum_values(forum=forum, searches=post) + values.update({ 'question': question, - 'question_data': True, - 'notifications': self._get_notifications(), - 'searches': post, + 'header': {'question_data': True}, 'filters': filters, - 'following': question.message_is_follower, - 'answer_done': answer_done, 'reversed': reversed, - 'forum': forum, - 'user': user, - } + }) return request.website.render("website_forum.post_description_full", values) - @http.route(['/forum//comment'], type='http', auth="public", methods=['POST'], website=True) - def post_comment(self, forum, post_id, **kwargs): + @http.route('/forum//post//comment', type='http', auth="public", methods=['POST'], website=True) + def post_comment(self, forum, post, **kwargs): + # tde: fix post / question if not request.session.uid: return login_redirect() cr, uid, context = request.cr, request.uid, request.context if kwargs.get('comment'): - user = request.registry['res.users'].browse(cr, SUPERUSER_ID, uid, context=context) - group_ids = user.groups_id - group_id = request.registry["ir.model.data"].get_object_reference(cr, uid, 'website_mail', 'group_comment')[1] - if group_id in [group.id for group in group_ids]: - Post = request.registry['website.forum.post'] - Post.message_post( - cr, uid, int(post_id), + # TDE FIXME: check that post_id is the question or one of its answers + if request.registry['res.users'].has_group(cr, uid, 'website_mail.group_comment'): + request.registry['forum.post'].message_post( + cr, uid, post.id, body=kwargs.get('comment'), type='comment', subtype='mt_comment', - content_subtype='plaintext', - author_id=user.partner_id.id, context=dict(context, mail_create_nosubcribe=True)) + return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(post))) - post = request.registry['website.forum.post'].browse(cr, uid, int(post_id), context=context) - question_id = post.parent_id.id if post.parent_id else post.id - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),question_id)) + @http.route('/forum//question//toggle_favourite', type='json', auth="user", multilang=True, website=True) + def question_toggle_favorite_tmp(self, forum_id, question_id, **post): + if not request.session.uid: + return {'error': 'anonymous_user'} + # forum = request.registry['forum.forum'].browse(request.cr, request.uid, forum_id, context=request.context) + question = request.registry['forum.post'].browse(request.cr, request.uid, question_id, context=request.context) + # TDE: add check for not public + favourite = False if question.user_favourite else True + print favourite + if favourite: + 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) + print favourite_ids + return favourite - @http.route(['/forum//user/'], type='http', auth="public", website=True, multilang=True) - def open_user(self, forum, user, **post): + @http.route('/forum//question//toggle_favourite', type='json', auth="user", multilang=True, methods=['POST'], website=True) + def question_toggle_favorite(self, forum, question, **post): + # TDE: add check for not public + favourite = False if question.user_favourite else True + if favourite: + 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) + return favourite + + @http.route('/forum//question//ask_for_close', type='http', auth="user", multilang=True, 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) + + values = self._prepare_forum_values(**post) + values.update({ + 'post': question, + 'forum': forum, + 'reasons': reasons, + }) + return request.website.render("website_forum.close_question", values) + + @http.route('/forum//question//edit_answer', type='http', auth="user", website=True, multilang=True) + def question_edit_answer(self, forum, question, **kwargs): + for record in question.child_ids: + if record.create_uid.id == request.uid: + answer = record + break + return werkzeug.utils.redirect("/forum/%s/post/%s/edit" % (slug(forum), slug(answer))) + + @http.route('/forum//question//close', type='http', auth="user", multilang=True, methods=['POST'], website=True) + def question_close(self, forum, question, **post): + request.registry['forum.post'].write(request.cr, request.uid, [question.id], { + 'state': 'close', + 'closed_uid': request.uid, + 'closed_date': datetime.today().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT), + 'closed_reason_id': post.get('reason_id', False), + }, context=request.context) + return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question))) + + @http.route('/forum//question//reopen', type='http', auth="user", multilang=True, 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) + return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question))) + + @http.route('/forum//question//delete', type='http', auth="user", multilang=True, website=True) + def question_delete(self, forum, question, **kwarg): + #instead of unlink record just change 'active' to false so user can undelete it. + request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'active': False}, context=request.context) + return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question))) + + @http.route('/forum//question//undelete', type='http', auth="user", multilang=True, 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) + return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question))) + + # Post + # -------------------------------------------------- + + @http.route('/forum//post//new', type='http', auth="public", multilang=True, methods=['POST'], website=True) + def post_new(self, forum, post, **kwargs): + if not request.session.uid: + return login_redirect() + request.registry['forum.post'].create( + request.cr, request.uid, { + 'forum_id': forum.id, + 'parent_id': post.id, + 'content': kwargs.get('content'), + }, context=request.context) + return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(post))) + + @http.route('/forum//post//toggle_correct', type='json', auth="public", website=True) + def post_toggle_correct(self, form, post, **kwargs): + cr, uid, context = request.cr, request.uid, request.context + if not request.session.uid: + return {'error': 'anonymous_user'} + # user = request.registry['res.users'].browse(cr, uid, uid, context=None) + + # # if user have not access to accept answer then reise warning + # if not (post.parent_id.create_uid.id == uid or user.karma >= 500): + # return {'error': '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], {'is_correct': False}, context=context) + request.registry['forum.post'].write(cr, uid, [post.id, post.parent_id.id], {'is_correct': not post.correct}, context=context) + return not post.correct + + @http.route('/forum//post//delete', type='http', auth="user", multilang=True, 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) + if question: + werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question))) + return werkzeug.utils.redirect("/forum/%s" % slug(forum)) + + @http.route('/forum//post//edit', type='http', auth="user", website=True, multilang=True) + def post_edit(self, forum, post, **kwargs): + cr, uid, context = request.cr, request.uid, request.context + + tags = "" + for tag_name in post.tag_ids: + tags += tag_name.name + "," + + values = self._prepare_forum_values(forum=forum) + values.update({ + 'question': post.parent_id, + 'tags': tags, + 'answer': post, + 'is_answer': True if post else False, + 'searches': kwargs + }) + return request.website.render("website_forum.edit_post", values) + + @http.route('/forum//post/save', type='http', auth="user", multilang=True, methods=['POST'], website=True) + def post_save(self, forum, **post): + cr, uid, context = request.cr, request.uid, request.context + vals = { + 'content': post.get('content'), + } + question_tags = [] + if post.get('question_tag') and post.get('question_tag').strip('[]'): + Tag = request.registry['forum.tag'] + tags = post.get('question_tag').strip('[]').replace('"','').split(",") + for tag in tags: + tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context) + if tag_ids: + question_tags += tag_ids + else: + new_tag = Tag.create(cr, uid, {'name' : tag,'forum_id' : forum.id}, context=context) + question_tags.append(new_tag) + vals.update({'tag_ids': [(6, 0, question_tags)], 'name': post.get('question_name')}) + + post_id = post.get('answer_id') if post.get('answer_id') else post.get('question_id') + new_question_id = request.registry['forum.post'].write( cr, uid, [int(post_id)], vals, context=context) + return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('question_id'))) + + @http.route('/forum//post//upvote', type='json', auth="public", multilang=True, website=True) + def post_upvote_tmp(self, forum_id, post_id, **kwargs): + if not request.session.uid: + return {'error': 'anonymous_user'} + # forum = request.registry['forum.forum'].browse(request.cr, request.uid, forum_id, context=request.context) + post = request.registry['forum.post'].browse(request.cr, request.uid, post_id, context=request.context) + return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=True, context=request.context) + + @http.route('/forum//post//upvote', type='json', auth="public", multilang=True, website=True) + def post_vote(self, forum, post, **kwargs): + if not request.session.uid: + return {'error': 'anonymous_user'} + return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=True, context=request.context) + + @http.route('/forum//post//downvote', type='json', auth="public", multilang=True, website=True) + def post_downvote_tmp(self, forum_id, post_id, **kwargs): + if not request.session.uid: + return {'error': 'anonymous_user'} + # forum = request.registry['forum.forum'].browse(request.cr, request.uid, forum_id, context=request.context) + post = request.registry['forum.post'].browse(request.cr, request.uid, post_id, context=request.context) + return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=False, context=request.context) + + @http.route('/forum//post//downvote', type='json', auth="public", multilang=True, website=True) + def post_downvote(self, forum, post, **kwargs): + if not request.session.uid: + return {'error': 'anonymous_user'} + return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=False, context=request.context) + + # User + # -------------------------------------------------- + + @http.route('/forum//users', type='http', auth="public", website=True, multilang=True) + def users(self, forum, page=1, **searches): cr, uid, context = request.cr, request.uid, request.context User = request.registry['res.users'] - Post = request.registry['website.forum.post'] - Vote = request.registry['website.forum.post.vote'] + + step = 30 + tag_count = User.search(cr, SUPERUSER_ID, [('karma', '>', 1)], count=True, context=context) + pager = request.website.pager(url="/forum/users", total=tag_count, page=page, step=step, scope=30) + + obj_ids = User.search(cr, SUPERUSER_ID, [('karma', '>', 1)], limit=step, offset=pager['offset'], context=context) + users = User.browse(cr, SUPERUSER_ID, obj_ids, context=context) + searches['users'] = 'True' + + values = self._prepare_forum_values(forum=forum, searches=searches) + values .update({ + 'users': users, + 'notifications': self._get_notifications(), + 'pager': pager, + }) + + return request.website.render("website_forum.users", values) + + @http.route(['/forum//user/'], type='http', auth="public", website=True, multilang=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"] - #questions and answers by user. + user_id = User.search(cr, SUPERUSER_ID, [('id', '=', user_id), ('karma', '>', '1')], context=context) + if not user_id: + return werkzeug.utils.redirect("/forum/%s" % slug(forum)) + user = User.browse(cr, SUPERUSER_ID, user_id[0], context=context) + + # questions and answers by user user_questions, user_answers = [], [] - user_post_ids = Post.search(cr, uid, [('forum_id', '=', forum.id), ('user_id', '=', user.id), - '|', ('active', '=', False), ('active', '=', True)], context=context) + user_post_ids = Post.search( + cr, uid, [ + ('forum_id', '=', forum.id), ('create_uid', '=', user.id), + '|', ('active', '=', False), ('active', '=', True)], context=context) user_posts = Post.browse(cr, uid, user_post_ids, context=context) for record in user_posts: if record.parent_id: @@ -195,8 +442,8 @@ class WebsiteForum(http.Controller): else: user_questions.append(record) - #showing questions which user following - obj_ids = Followers.search(cr, SUPERUSER_ID, [('res_model', '=', 'website.forum.post'),('partner_id' , '=' , user.partner_id.id)], context=context) + # 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) @@ -206,7 +453,7 @@ class WebsiteForum(http.Controller): favourite = Post.browse(cr, uid, fav_que_ids, context=context) #votes which given on users questions and answers. - data = Vote.read_group(cr, uid, [('post_id.forum_id', '=', forum.id), ('post_id.user_id', '=', user.id)], ["vote"], groupby=["vote"], context=context) + data = Vote.read_group(cr, uid, [('post_id.forum_id', '=', forum.id), ('post_id.create_uid', '=', user.id)], ["vote"], groupby=["vote"], context=context) up_votes, down_votes = 0, 0 for rec in data: if rec['vote'] == '1': @@ -221,7 +468,7 @@ class WebsiteForum(http.Controller): #activity by user. model, comment = Data.get_object_reference(cr, uid, 'mail', 'mt_comment') - activity_ids = Activity.search(cr, uid, [('res_id', 'in', user_post_ids), ('model', '=', 'website.forum.post'), ('subtype_id', '!=', comment)], context=context) + activity_ids = Activity.search(cr, uid, [('res_id', 'in', user_post_ids), ('model', '=', 'forum.post'), ('subtype_id', '!=', comment)], context=context) activities = Activity.browse(cr, uid, activity_ids, context=context) posts = {} @@ -232,7 +479,8 @@ class WebsiteForum(http.Controller): post['users'] = 'True' - values = { + values = self._prepare_forum_values(**post) + values.update({ 'uid': uid, 'user': user, 'main_object': user, @@ -248,308 +496,12 @@ class WebsiteForum(http.Controller): 'activities': activities, 'posts': posts, 'vote_post': vote_ids, - 'notifications': self._get_notifications(), - } + }) return request.website.render("website_forum.user_detail_full", values) - @http.route(['/forum//ask'], type='http', auth="public", website=True, multilang=True) - def question_ask(self, forum, **post): - if not request.session.uid: - return login_redirect() - user = request.registry['res.users'].browse(request.cr, request.uid, request.uid, context=request.context) - values = { - 'searches': {}, - 'forum': forum, - 'user': user, - 'ask_question': True, - 'notifications': self._get_notifications(), - } - return request.website.render("website_forum.ask_question", values) - - @http.route('/forum//question/ask', type='http', auth="user", multilang=True, methods=['POST'], website=True) - def register_question(self, forum, **question): - cr, uid, context = request.cr, request.uid, request.context - create_context = dict(context) - - Tag = request.registry['website.forum.tag'] - question_tags = [] - if question.get('question_tags').strip('[]'): - tags = question.get('question_tags').strip('[]').replace('"','').split(",") - for tag in tags: - tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context) - if tag_ids: - question_tags.append((4,tag_ids[0])) - else: - question_tags.append((0,0,{'name' : tag,'forum_id' : forum.id})) - - new_question_id = request.registry['website.forum.post'].create( - request.cr, request.uid, { - 'user_id': uid, - 'forum_id': forum.id, - 'name': question.get('question_name'), - 'content': question.get('content'), - 'tag_idss' : question_tags, - 'state': 'active', - 'active': True, - }, context=create_context) - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),new_question_id)) - - @http.route('/forum//question/postanswer', type='http', auth="public", multilang=True, methods=['POST'], website=True) - def post_answer(self, forum , post_id, **question): - if not request.session.uid: - return login_redirect() - - cr, uid, context = request.cr, request.uid, request.context - - create_context = dict(context) - new_question_id = request.registry['website.forum.post'].create( - cr, uid, { - 'forum_id': forum.id, - 'user_id': uid, - 'parent_id': post_id, - 'content': question.get('content'), - 'state': 'active', - 'active': True, - }, context=create_context) - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post_id)) - - @http.route(['/forum//question//editanswer'] - , type='http', auth="user", website=True, multilang=True) - def edit_answer(self, forum, question, **kwargs): - for record in question.child_ids: - if record.user_id.id == request.uid: - answer = record - return werkzeug.utils.redirect("/forum/%s/question/%s/edit/%s" % (slug(forum), question.id, answer.id)) - - @http.route(['/forum//edit/question/', - '/forum//question//edit/'] - , type='http', auth="user", website=True, multilang=True) - def edit_post(self, forum, question, answer=None, **kwargs): - cr, uid, context = request.cr, request.uid, request.context - - User = request.registry['res.users'] - user = User.browse(cr, uid, uid, context=context) - - tags = "" - for tag_name in question.tag_ids: - tags += tag_name.name + "," - - values = { - 'question': question, - 'user': user, - 'tags': tags, - 'answer': answer, - 'is_answer': True if answer else False, - 'notifications': self._get_notifications(), - 'forum': forum, - 'searches': kwargs - } - return request.website.render("website_forum.edit_post", values) - - @http.route('/forum//post/save', type='http', auth="user", multilang=True, methods=['POST'], website=True) - def save_edited_post(self, forum, **post): - cr, uid, context = request.cr, request.uid, request.context - vals = { - 'content': post.get('content'), - } - question_tags = [] - if post.get('question_tag') and post.get('question_tag').strip('[]'): - Tag = request.registry['website.forum.tag'] - tags = post.get('question_tag').strip('[]').replace('"','').split(",") - for tag in tags: - tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context) - if tag_ids: - question_tags += tag_ids - else: - new_tag = Tag.create(cr, uid, {'name' : tag,'forum_id' : forum.id}, context=context) - question_tags.append(new_tag) - vals.update({'tag_ids': [(6, 0, question_tags)], 'name': post.get('question_name')}) - - post_id = post.get('answer_id') if post.get('answer_id') else post.get('question_id') - new_question_id = request.registry['website.forum.post'].write( cr, uid, [int(post_id)], vals, context=context) - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('question_id'))) - - @http.route(['/forum//tag'], type='http', auth="public", website=True, multilang=True) - def tags(self, forum, page=1, **searches): - cr, uid, context = request.cr, request.uid, request.context - Tag = request.registry['website.forum.tag'] - obj_ids = Tag.search(cr, uid, [('forum_id', '=', forum.id)], limit=None, context=context) - tags = Tag.browse(cr, uid, obj_ids, context=context) - values = { - 'tags': tags, - 'notifications': self._get_notifications(), - 'forum': forum, - 'searches': {'tags': True} - } - return request.website.render("website_forum.tag", values) - - @http.route(['/forum//badge'], type='http', auth="public", website=True, multilang=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, uid, [('level', '!=', False)], context=context) - badges = Badge.browse(cr, uid, badge_ids, context=context) - values = { - 'badges': badges, - 'notifications': {}, - 'forum': forum, - 'searches': {'badges': True} - } - return request.website.render("website_forum.badge", values) - - @http.route(['/forum//badge/'], type='http', auth="public", website=True, multilang=True) - def badge_users(self, forum, badge, **kwargs): - users = [badge_user.user_id for badge_user in badge.owner_ids] - kwargs['badges'] = 'True' - - values = { - 'badge': badge, - 'notifications': {}, - 'users': users, - 'forum': forum, - 'searches': kwargs - } - return request.website.render("website_forum.badge_user", values) - - @http.route(['/forum//users', '/forum/users/page/'], type='http', auth="public", website=True, multilang=True) - def users(self, forum, page=1, **searches): - cr, uid, context = request.cr, request.uid, request.context - User = request.registry['res.users'] - - step = 30 - tag_count = User.search(cr, uid, [('karma', '>', 1)], count=True, context=context) - pager = request.website.pager(url="/forum/users", total=tag_count, page=page, step=step, scope=30) - - obj_ids = User.search(cr, uid, [('karma', '>', 1)], limit=step, offset=pager['offset'], context=context) - users = User.browse(cr, uid, obj_ids, context=context) - searches['users'] = 'True' - - values = { - 'users': users, - 'notifications': self._get_notifications(), - 'pager': pager, - 'forum': forum, - 'searches': searches, - } - - return request.website.render("website_forum.users", values) - - @http.route('/forum/post_vote', type='json', auth="public", multilang=True, methods=['POST'], website=True) - def post_vote(self, **post): - if not request.session.uid: - return {'error': 'anonymous_user'} - cr, uid, context, post_id = request.cr, request.uid, request.context, int(post.get('post_id')) - return request.registry['website.forum.post'].vote(cr, uid, [post_id], upvote=(post.get('vote') == '1'), context=context) - - @http.route('/forum/post_delete', type='json', auth="user", multilang=True, methods=['POST'], website=True) - def delete_answer(self, **kwarg): - request.registry['website.forum.post'].unlink(request.cr, request.uid, [int(kwarg.get('post_id'))], context=request.context) - return True - - @http.route('/forum//delete/question/', type='http', auth="user", multilang=True, website=True) - def delete_question(self, forum, post, **kwarg): - #instead of unlink record just change 'active' to false so user can undelete it. - request.registry['website.forum.post'].write( request.cr, request.uid, [post.id], { - 'active': False, - }, context=request.context) - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id)) - - @http.route('/forum//undelete/question/', type='http', auth="user", multilang=True, website=True) - def undelete_question(self, forum, post, **kwarg): - request.registry['website.forum.post'].write( request.cr, request.uid, [post.id], { - 'active': True, - }, context=request.context) - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id)) - - @http.route('/forum/message_delete', type='json', auth="user", multilang=True, methods=['POST'], website=True) - def delete_comment(self, **kwarg): - request.registry['mail.message'].unlink(request.cr, SUPERUSER_ID, [int(kwarg.get('message_id'))], context=request.context) - return True - - @http.route('/forum/selecthistory', type='json', auth="user", multilang=True, methods=['POST'], website=True) - def post_history(self, **kwarg): - cr, uid, context = request.cr, request.uid, request.context - post_history = request.registry['website.forum.post.history'].browse(cr, uid, int(kwarg.get('history_id')), context=context) - tags = "" - for tag_name in post_history.tags: - tags += tag_name.name + "," - data = { - 'name': post_history.post_name, - 'content': post_history.content, - 'tags': tags, - } - return data - - @http.route('/forum/correct_answer', type='json', auth="public", multilang=True, methods=['POST'], website=True) - def correct_answer(self, **kwarg): - cr, uid, context = request.cr, request.uid, request.context - if not request.session.uid: - return {'error': 'anonymous_user'} - - Post = request.registry['website.forum.post'] - post = Post.browse(cr, uid, int(kwarg.get('post_id')), context=context) - user = request.registry['res.users'].browse(cr, uid, uid, context=None) - - #if user have not access to accept answer then reise warning - if not (post.parent_id.user_id.id == uid or user.karma >= 500): - return {'error': 'user'} - - #Note: only one answer can be right. - correct = False if post.correct else True - for child in post.parent_id.child_ids: - if child.correct and child.id != post.id: - Post.write( cr, uid, [child.id], { 'correct': False }, context=context) - Post.write( cr, uid, [post.id, post.parent_id.id], { 'correct': correct }, context=context) - return correct - - @http.route('/forum/favourite_question', type='json', auth="user", multilang=True, methods=['POST'], website=True) - def favourite_question(self, **kwarg): - cr, uid, context = request.cr, request.uid, request.context - Post = request.registry['website.forum.post'] - post = Post.browse(cr, uid, int(kwarg.get('post_id')), context=context) - favourite = False if post.user_favourite else True - favourite_ids = [(4, uid)] - if post.user_favourite: - favourite_ids = [(3, uid)] - Post.write( cr, uid, [post.id], { 'favourite_ids': favourite_ids }, context=context) - return favourite - - @http.route('/forum//close/question/', type='http', auth="user", multilang=True, website=True) - def close_question(self, forum, post, **kwarg): - cr, uid, context = request.cr, request.uid, request.context - Reason = request.registry['website.forum.post.reason'] - reason_ids = Reason.search(cr, uid, [], context=context) - reasons = Reason.browse(cr, uid, reason_ids, context) - - values = { - 'post': post, - 'forum': forum, - 'searches': kwarg, - 'reasons': reasons, - 'notifications': self._get_notifications(), - } - return request.website.render("website_forum.close_question", values) - - @http.route('/forum//question/close', type='http', auth="user", multilang=True, methods=['POST'], website=True) - def close(self, forum, **post): - request.registry['website.forum.post'].write( request.cr, request.uid, [int(post.get('post_id'))], { - 'state': 'close', - 'closed_by': request.uid, - 'closed_date': datetime.today().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT), - 'reason_id': post.get('reason'), - }, context=request.context) - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('post_id'))) - - @http.route('/forum//reopen/question/', type='http', auth="user", multilang=True, website=True) - def reopen(self, forum, post, **kwarg): - request.registry['website.forum.post'].write( request.cr, request.uid, [post.id], { - 'state': 'active', - }, context=request.context) - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id)) - - @http.route('/forum//edit/profile/', type='http', auth="user", multilang=True, website=True) + @http.route('/forum//user//edit', type='http', auth="user", multilang=True, website=True) def edit_profile(self, forum, user, **kwarg): - cr,context = request.cr, request.context + cr, context = request.cr, request.context country = request.registry['res.country'] country_ids = country.search(cr, SUPERUSER_ID, [], context=context) countries = country.browse(cr, SUPERUSER_ID, country_ids, context) @@ -562,7 +514,7 @@ class WebsiteForum(http.Controller): } return request.website.render("website_forum.edit_profile", values) - @http.route('/forum//save/profile', type='http', auth="user", multilang=True, website=True) + @http.route('/forum//user//save', type='http', auth="user", multilang=True, website=True) def save_edited_profile(self, forum, **post): cr, uid, context = request.cr, request.uid, request.context user = request.registry['res.users'].browse(cr, uid, int(post.get('user_id')),context=context) @@ -576,45 +528,55 @@ class WebsiteForum(http.Controller): }, context=context) return werkzeug.utils.redirect("/forum/%s/user/%s" % (slug(forum),post.get('user_id'))) - @http.route('/forum//post//commet//converttoanswer', type='http', auth="public", multilang=True, website=True) - def convert_to_answer(self, forum, post, comment, **kwarg): + # Badges + # -------------------------------------------------- + + @http.route(['/forum//badge'], type='http', auth="public", website=True, multilang=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, uid, [('level', '!=', False)], context=context) + badges = Badge.browse(cr, uid, badge_ids, context=context) + values = self._prepare_forum_values(forum=forum, searches={'badges': True}) + values.update({ + 'badges': badges, + }) + return request.website.render("website_forum.badge", values) + + @http.route(['/forum//badge/'], type='http', auth="public", website=True, multilang=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) + kwargs['badges'] = 'True' + + values = self._prepare_forum_values(forum=forum) + values.update({ + 'badge': badge, + 'users': users, + }) + return request.website.render("website_forum.badge_user", values) + + # Messaging + # -------------------------------------------------- + + @http.route('/forum//post//comment//convert_to_answer', type='http', auth="public", multilang=True, website=True) + def convert_comment_to_answer(self, forum, post, comment, **kwarg): values = { 'content': comment.body, } request.registry['mail.message'].unlink(request.cr, request.uid, [comment.id], context=request.context) return self.post_answer(forum, post.parent_id and post.parent_id.id or post.id, **values) - @http.route('/forum//post//converttocomment', type='http', auth="user", multilang=True, website=True) - def convert_to_comment(self, forum, post, **kwarg): + @http.route('/forum//post//convert_to_comment', type='http', auth="user", multilang=True, website=True) + def convert_answer_to_comment(self, forum, post, **kwarg): values = { 'comment': html2plaintext(post.content), } - question = post.parent_id.id - request.registry['website.forum.post'].unlink(request.cr, SUPERUSER_ID, [post.id], context=request.context) + question = post.parent_id + request.registry['forum.post'].unlink(request.cr, SUPERUSER_ID, [post.id], context=request.context) return self.post_comment(forum, question, **values) - @http.route('/forum/get_tags', type='http', auth="public", multilang=True, methods=['GET'], website=True) - def tag_read(self, **kwarg): - tags = request.registry['website.forum.tag'].search_read(request.cr, request.uid, [], ['name'], context=request.context) - data = [tag['name'] for tag in tags] - return simplejson.dumps(data) - - @http.route('/forum//question//subscribe', type='http', auth="public", multilang=True, website=True) - def subscribe(self, forum, post, **kwarg): - cr, uid, context = request.cr, request.uid, request.context - if not request.session.uid: - return login_redirect() - partner_id = request.registry['res.users'].browse(cr, uid, request.uid, context=context).partner_id.id - post_ids = [child.id for child in post.child_ids] - post_ids.append(post.id) - request.registry['website.forum.post'].message_subscribe( cr, uid, post_ids, [partner_id], context=context) - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id)) - - @http.route('/forum//question//unsubscribe', type='http', auth="user", multilang=True, website=True) - def unsubscribe(self, forum, post, **kwarg): - cr, uid, context = request.cr, request.uid, request.context - partner_id = request.registry['res.users'].browse(cr, uid, request.uid, context=context).partner_id.id - post_ids = [child.id for child in post.child_ids] - post_ids.append(post.id) - request.registry['website.forum.post'].message_unsubscribe( cr, uid, post_ids, [partner_id], context=context) - return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id)) + @http.route('/forum/message_delete', type='json', auth="user", multilang=True, methods=['POST'], website=True) + def delete_comment(self, **kwarg): + request.registry['mail.message'].unlink(request.cr, SUPERUSER_ID, [int(kwarg.get('message_id'))], context=request.context) + return True diff --git a/addons/website_forum/data/badges_answer.xml b/addons/website_forum/data/badges_answer.xml index bc4af4700c5..547bf4c4ff9 100644 --- a/addons/website_forum/data/badges_answer.xml +++ b/addons/website_forum/data/badges_answer.xml @@ -15,7 +15,7 @@ count boolean - [('user_id', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 3)] + [('create_uid', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 3)] higher @@ -45,7 +45,7 @@ count boolean - [('user_id', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 4)] + [('create_uid', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 4)] higher @@ -75,7 +75,7 @@ count boolean - [('user_id', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 6)] + [('create_uid', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 6)] higher @@ -105,7 +105,7 @@ count boolean - [('user_id', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 15)] + [('create_uid', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 15)] higher @@ -137,7 +137,7 @@ count boolean - [('user_id', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 3), ('is_correct', '=', True)] + [('create_uid', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 3), ('is_correct', '=', True)] higher @@ -167,7 +167,7 @@ count boolean - [('user_id', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 15), ('is_correct', '=', True)] + [('create_uid', '=', user.id), ('parent_id', '!=', False), ('vote_count', '>=', 15), ('is_correct', '=', True)] higher @@ -199,7 +199,7 @@ def get_posts(cr, uid, context=None): res = 1 Post = self.pool['website.forum.post'] - user_posts = Post.search(cr, uid, [('user_id','=',object.user_id.id), ('parent_id', '!=', False), ('vote_count', '>=', 2)], context=context) + user_posts = Post.search(cr, uid, [('create_uid','=',object.user_id.id), ('parent_id', '!=', False), ('vote_count', '>=', 2)], context=context) for post in Post.browse(cr, uid, user_posts, context=context): print post.parent_id.create_date question_date = datetime.strptime(post.parent_id.create_date, "%Y-%m-%d %H:%M:%S") @@ -240,7 +240,7 @@ result = get_posts(cr, uid, context=context) count boolean - [('user_id', '=', user.id), ('parent_id', '!=', False), ('parent_id.user_id', '=', user.id), ('vote_count', '>=', 3)] + [('create_uid', '=', user.id), ('parent_id', '!=', False), ('parent_id.create_uid', '=', user.id), ('vote_count', '>=', 3)] higher diff --git a/addons/website_forum/data/badges_moderation.xml b/addons/website_forum/data/badges_moderation.xml index dde6bd30196..acb49e00597 100644 --- a/addons/website_forum/data/badges_moderation.xml +++ b/addons/website_forum/data/badges_moderation.xml @@ -45,7 +45,7 @@ count boolean - [('user_id', '=', user.id), ('vote', '=', '-1')] + [('create_uid', '=', user.id), ('vote', '=', '-1')] higher @@ -76,7 +76,7 @@ count boolean - [('user_id', '=', user.id), ('vote_count', '>=', 3), ('active', '=', False)] + [('create_uid', '=', user.id), ('vote_count', '>=', 3), ('active', '=', False)] higher @@ -107,7 +107,7 @@ count boolean - [('user_id','=',user.id), '|', ('name','!=',False), ('content','!=',False)] + [('create_uid','=',user.id), '|', ('name','!=',False), ('content','!=',False)] higher @@ -140,7 +140,7 @@ count boolean - [('user_id','=',user.id), ('tags','!=',False)] + [('create_uid','=',user.id), ('tags','!=',False)] higher --> @@ -176,7 +176,7 @@ count boolean - [('user_id','=',user.id), '|', ('name','!=',False), ('content','!=',False)] + [('create_uid','=',user.id), '|', ('name','!=',False), ('content','!=',False)] higher --> @@ -211,8 +211,7 @@ end_date = date.today() - timedelta(days=1) cr.execute('SELECT COUNT(id), create_date from forum_post_vote where create_date >= %s and create_date <= %s and user_id = %s GROUP BY create_date',(start_date,end_date, object.user_id.id,)) data = cr.dictfetchall() -result = int(len(data) >= 15) - +result = int(len(data) >= 15) boolean higher @@ -278,7 +277,7 @@ result = int(len(data) >= 15) count boolean - [('user_id','=',user.id), ('vote_count', '<=', -3), ('active', '=', False)] + [('create_uid','=',user.id), ('vote_count', '<=', -3), ('active', '=', False)] higher diff --git a/addons/website_forum/data/badges_question.xml b/addons/website_forum/data/badges_question.xml index d012fdb7734..90ee9b3ecf3 100644 --- a/addons/website_forum/data/badges_question.xml +++ b/addons/website_forum/data/badges_question.xml @@ -1,4 +1,4 @@ - + @@ -107,7 +107,7 @@ count boolean - [('create_uid', '=', user.id), ('parent_id', '=', False), ('favorite_count', '>=', 1)] + [('create_uid', '=', user.id), ('parent_id', '=', False), ('favourite_count', '>=', 1)] higher @@ -137,7 +137,7 @@ count boolean - [('create_uid', '=', user.id), ('parent_id', '=', False), ('favorite_count', '>=', 5)] + [('create_uid', '=', user.id), ('parent_id', '=', False), ('favourite_count', '>=', 5)] higher @@ -167,7 +167,7 @@ count boolean - [('create_uid', '=', user.id), ('parent_id', '=', False), ('favorite_count', '>=', 25)] + [('create_uid', '=', user.id), ('parent_id', '=', False), ('favourite_count', '>=', 25)] higher @@ -199,7 +199,7 @@ count boolean - [('user_id', '=', user.id), ('parent_id', '=', False), ('vote_count', '>=', 1)] + [('create_uid', '=', user.id), ('parent_id', '=', False), ('vote_count', '>=', 1)] higher @@ -229,7 +229,7 @@ count boolean - [('user_id','=',user.id), ('parent_id', '=', False), ('vote_count', '>=', 4)] + [('create_uid', '=', user.id), ('parent_id', '=', False), ('vote_count', '>=', 4)] higher @@ -259,7 +259,7 @@ count boolean - [('user_id', '=', user.id), ('parent_id', '=', False), ('vote_count', '>=', 6)] + [('create_uid', '=', user.id), ('parent_id', '=', False), ('vote_count', '>=', 6)] higher @@ -289,7 +289,7 @@ count boolean - [('user_id','=',user.id), ('parent_id', '=', False), ('vote_count', '>=', 15)] + [('create_uid', '=', user.id), ('parent_id', '=', False), ('vote_count', '>=', 15)] higher @@ -320,7 +320,7 @@ count boolean - [('user_id','=',user.id), ('parent_id', '=', False), ('is_correct', '=', True)] + [('create_uid', '=', user.id), ('parent_id', '=', False), ('is_correct', '=', True)] higher diff --git a/addons/website_forum/data/forum_demo.xml b/addons/website_forum/data/forum_demo.xml index 57cf87d4f08..fe8e21619ba 100644 --- a/addons/website_forum/data/forum_demo.xml +++ b/addons/website_forum/data/forum_demo.xml @@ -26,7 +26,6 @@ 5 - CMS replacement for ERP and eCommerce @@ -38,7 +37,6 @@ Can I use OpenERP as a replacement CMS of Wordpress + eCommerce plugin? In simple words does OpenERP became CMS+ERP platform? - @@ -53,7 +51,7 @@ In simple words does OpenERP became CMS+ERP platform? 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. - + @@ -61,7 +59,7 @@ In simple words does OpenERP became CMS+ERP platform? The CMS editor in OpernERP web is nice but I prefer drupal for customization and there is a drupal module for OpenERP. I think WP is better than OpenERP web too. - + diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py index 4388c77e951..4c1e6c42040 100644 --- a/addons/website_forum/models/forum.py +++ b/addons/website_forum/models/forum.py @@ -65,14 +65,14 @@ class Post(osv.Model): 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.user_id.id for f in post.favourite_ids]: + 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.favorite_ids) + res[post.id] += len(post.favourite_ids) return res def _get_post_from_hierarchy(self, cr, uid, ids, context=None): @@ -91,6 +91,12 @@ class Post(osv.Model): 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 + _columns = { 'name': fields.char('Title', size=128), 'forum_id': fields.many2one('forum.forum', 'Forum', required=True), @@ -122,12 +128,12 @@ class Post(osv.Model): 'forum.post.vote': (_get_post_from_vote, [], 10), }), # favorite fields - 'favourite_ids': fields.many2many('res.users', 'Favourite'), + 'favourite_ids': fields.many2many('res.users', string='Favourite'), 'user_favourite': fields.function(_get_user_favourite, string="My Favourite", type='boolean'), - 'favorite_count': fields.function( + 'favourite_count': fields.function( _get_favorite_count, string='Favorite Count', type='integer', store={ - 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['favorite_ids'], 10), + 'forum.post': (lambda self, cr, uid, ids, c={}: ids, ['favourite_ids'], 10), }), # hierarchy 'parent_id': fields.many2one('forum.post', 'Question', ondelete='cascade'), @@ -137,6 +143,9 @@ class Post(osv.Model): 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', + ), # closing 'closed_reason_id': fields.many2one('forum.post.reason', 'Reason'), 'closed_uid': fields.many2one('res.users', 'Closed by', select=1), @@ -195,8 +204,8 @@ class Post(osv.Model): return {'error': 'lessthen_10_karma'} # user can not vote on own post posts = self.browse(cr, uid, ids, context=context) - if any(post.create_uid.id == uid for post in posts): - return {'error': 'own_post'} + # if any(post.create_uid.id == uid for post in posts): + # return {'error': 'own_post'} vote_ids = Vote.search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context) if vote_ids: diff --git a/addons/website_forum/static/src/css/Makefile b/addons/website_forum/static/src/css/Makefile index 867a1fedb38..ceee4ae3f7a 100644 --- a/addons/website_forum/static/src/css/Makefile +++ b/addons/website_forum/static/src/css/Makefile @@ -1,2 +1,5 @@ -sass: - sass --compass --unix-newlines -t expanded website_forum.sass:website_forum.css +all: website_forum.css +%.css: %.sass + sass -t expanded --compass --unix-newlines $< $@ +watch: + sass -t expanded --compass --unix-newlines --watch .:. diff --git a/addons/website_forum/static/src/css/website_forum.css b/addons/website_forum/static/src/css/website_forum.css index 6548e8d01b1..e83242c1484 100644 --- a/addons/website_forum/static/src/css/website_forum.css +++ b/addons/website_forum/static/src/css/website_forum.css @@ -1,17 +1,16 @@ .box { padding-left: 8px; padding-right: 8px; + margin-left: 4px; + margin-right: 4px; border-radius: 4px; + min-width: 80px; } .box span { font-size: 200%; font-weight: bold; } -.question div.pull-left { - margin-right: 14px; - min-width: 80px; -} .question .question-name { font-size: 150%; } @@ -64,13 +63,11 @@ a.no-decoration { } .oe_comment_grey { - -moz-box-shadow: 0px 4px 6px 2px #eeeeee; - -webkit-box-shadow: 0px 4px 6px 2px #eeeeee; - box-shadow: 0px 4px 6px 2px #eeeeee; + color: whitesmoke; } -.forum_user_flag { - max-width: 25px; +.country_flag { + max-height: 16px; display: inline-block; margin-left: 2px; } diff --git a/addons/website_forum/static/src/css/website_forum.sass b/addons/website_forum/static/src/css/website_forum.sass index 555250bd6f3..d3c239b7dc4 100644 --- a/addons/website_forum/static/src/css/website_forum.sass +++ b/addons/website_forum/static/src/css/website_forum.sass @@ -1,15 +1,15 @@ .box padding-left: 8px padding-right: 8px + margin-left: 4px + margin-right: 4px border-radius: 4px + min-width: 80px span font-size: 200% font-weight: bold .question - div.pull-left - margin-right: 14px - min-width: 80px .question-name font-size: 150% @@ -52,8 +52,8 @@ a.no-decoration .oe_comment_grey color: #f5f5f5 -.forum_user_flag - max-width: 25px +.country_flag + max-height: 16px display: inline-block margin-left: 2px diff --git a/addons/website_forum/static/src/js/website_forum.js b/addons/website_forum/static/src/js/website_forum.js index 907e5d47a01..202b0a7d90d 100644 --- a/addons/website_forum/static/src/js/website_forum.js +++ b/addons/website_forum/static/src/js/website_forum.js @@ -3,11 +3,11 @@ $(document).ready(function () { $('.vote_up ,.vote_down').on('click', function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); - var value = $link.attr("value") - - openerp.jsonRpc("/forum/post_vote", 'call', { - 'post_id': $link.attr("id"), - 'vote': value}) + var url_target = $link.data("value") == '1' ? 'upvote' : 'downvote'; + var forum_id = $link.data('forum-id'); + var post_id = $link.data('post-id'); + + openerp.jsonRpc("/forum/" + forum_id + "/post/" + post_id + "/" + url_target, 'call', {}) .then(function (data) { if (data['error']){ if (data['error'] == 'own_post'){ @@ -38,7 +38,7 @@ $(document).ready(function () { $link.parent().find(".text-success").removeClass("text-success"); $link.parent().find(".text-warning").removeClass("text-warning"); } else { - if (value == 1) { + if (data['vote_count'] == 1) { $link.addClass("text-success"); } else { $link.addClass("text-warning"); @@ -49,75 +49,65 @@ $(document).ready(function () { return true; }); - $('.delete').on('click', function (ev) { - ev.preventDefault(); - var $link = $(ev.currentTarget); - openerp.jsonRpc("/forum/post_delete", 'call', { - 'post_id': $link.attr("id")}) - .then(function (data) { - $("div#answer_" + $link.attr('id')).remove(); - }); - return false; - }); - $('.accept_answer').on('click', function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); - openerp.jsonRpc("/forum/correct_answer", 'call', { - 'post_id': $link.attr("id")}) - .then(function (data) { - if (data['error']) { - if (data['error'] == 'anonymous_user'){ - var $warning = $('
'+ - ''+ - 'Sorry, anonymous users cannot choose correct answer.'+ - '
'); - } else if (data['error'] == 'user'){ - var $warning = $('
'+ - ''+ - 'Sorry, the user who asked this question can only accept the answer as correct.'+ - '
'); - } - correct_answer_alert = $link.parent().find("#correct_answer_alert"); - if (correct_answer_alert.length == 0) { - $link.parent().append($warning); - } - } else { - $link.parents().find(".oe_answer_true").removeClass("oe_answer_true alert alert-info").addClass('oe_answer_false'); - $link.parents().find(".answer_correct").removeClass("answer_correct alert alert-info") - if (data) { - $link.removeClass("oe_answer_false").addClass('oe_answer_true'); - $("div#answer_" + $link.attr('id')).addClass("answer_correct alert alert-info"); - console.log("aaaaa",$("div#answer_" + $link.attr('id'))) - } + var forum_id = $link.data('forum-id'); + var post_id = $link.data('post-id'); + openerp.jsonRpc("/forum/" + forum_id + "/post/" + post_id + "/toggle_correct", 'call', {}).then(function (data) { + if (data['error']) { + if (data['error'] == 'anonymous_user'){ + var $warning = $('
'+ + ''+ + 'Sorry, anonymous users cannot choose correct answer.'+ + '
'); + } else if (data['error'] == 'user'){ + var $warning = $('
'+ + ''+ + 'Sorry, the user who asked this question can only accept the answer as correct.'+ + '
'); } - }); + correct_answer_alert = $link.parent().find("#correct_answer_alert"); + if (correct_answer_alert.length == 0) { + $link.parent().append($warning); + } + } else { + $link.parents().find(".oe_answer_true").removeClass("oe_answer_true alert alert-info").addClass('oe_answer_false'); + $link.parents().find(".answer_correct").removeClass("answer_correct alert alert-info") + if (data) { + $link.removeClass("oe_answer_false").addClass('oe_answer_true'); + $("div#answer_" + $link.attr('id')).addClass("answer_correct alert alert-info"); + console.log("aaaaa",$("div#answer_" + $link.attr('id'))) + } + } + }); return true; }); $('.favourite_question').on('click', function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); - openerp.jsonRpc("/forum/favourite_question", 'call', { - 'post_id': $link.attr("id")}) - .then(function (data) { - if (data) { - $link.addClass("forum_favourite_question") - } else { - $link.removeClass("forum_favourite_question") - } - }); + var forum_id = $link.data('forum-id'); + var post_id = $link.data('post-id'); + openerp.jsonRpc("/forum/" + forum_id + "/question/" + post_id + "/toggle_favourite", 'call', {}).then(function (data) { + if (data) { + $link.addClass("forum_favourite_question") + } else { + $link.removeClass("forum_favourite_question") + } + }); return true; }); $('.comment_delete').on('click', function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); - openerp.jsonRpc("/forum/message_delete", 'call', { - 'message_id': $link.attr("id")}) - .then(function (data) { - $link.parents('#comment').remove(); - }); + var forum_id = $link.data('forum-id'); + var post_id = $link.data('post-id'); + var message_id = $link.data('message-id'); + openerp.jsonRpc("/forum/message_delete", 'call', {}).then(function (data) { + $link.parents('#comment').remove(); + }); return true; }); @@ -155,20 +145,6 @@ $(document).ready(function () { }); } - $('.post_history').change(function (ev) { - var $option = $(ev.currentTarget); - openerp.jsonRpc("/forum/selecthistory", 'call', { - 'history_id': $option.attr("value")}) - .then(function (data) { - var $input = $('') - $option.parent().find(".text-core").replaceWith($input); - set_tags(data['tags']); - $option.parent().find("#question_name").attr('value', data['name']); - CKEDITOR.instances['content'].setData(data['content']) - }) - return true; - }); - if ($('textarea.load_editor').length) { var editor = CKEDITOR.instances['content']; editor.on('instanceReady', CKEDITORLoadComplete); diff --git a/addons/website_forum/views/ir_qweb.xml b/addons/website_forum/views/ir_qweb.xml new file mode 100644 index 00000000000..04188b54234 --- /dev/null +++ b/addons/website_forum/views/ir_qweb.xml @@ -0,0 +1,24 @@ + + + + + diff --git a/addons/website_forum/views/website_forum.xml b/addons/website_forum/views/website_forum.xml index bcfa4447537..dc2d7e36834 100644 --- a/addons/website_forum/views/website_forum.xml +++ b/addons/website_forum/views/website_forum.xml @@ -2,272 +2,662 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + - + + + +