From 3fc29404db9ec271eaae4db6b387672bfaee7b2b Mon Sep 17 00:00:00 2001 From: "Turkesh Patel (Open ERP)" Date: Tue, 18 Mar 2014 14:31:54 +0530 Subject: [PATCH] [IMP] improved code to fix error on creating new form from contents, allow user to delete own comments, there should be only one correct answer per question. bzr revid: tpa@tinyerp.com-20140318090154-m8e20wtosxdnqnfy --- addons/website_forum/controllers/main.py | 20 ++++++++++++++++--- addons/website_forum/models/website_forum.py | 7 +++++++ .../static/src/js/website_forum.editor.js | 16 ++++++++++++--- addons/website_forum/views/website_forum.xml | 7 +++++-- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py index 64f36fc48c4..c6a828280a4 100644 --- a/addons/website_forum/controllers/main.py +++ b/addons/website_forum/controllers/main.py @@ -46,7 +46,7 @@ class website_forum(http.Controller): values = { 'forums': forums } return request.website.render("website_forum.forum_index", values) - @http.route('/forum/add_forum/', type='http', auth="user", multilang=True, methods=['POST'], website=True) + @http.route('/forum/add_forum/', type='http', auth="user", multilang=True, website=True) def add_forum(self, forum_name="New Forum", **kwargs): forum_id = request.registry['website.forum'].create(request.cr, request.uid, { 'name': forum_name, @@ -74,7 +74,9 @@ class website_forum(http.Controller): if filters == 'followed': domain += [ ('create_uid', '=', uid) ] - if sorting == 'date': + # 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' @@ -89,6 +91,7 @@ class website_forum(http.Controller): question_ids = Forum.browse(cr, uid, obj_ids, context=context) values = { + 'uid': uid, 'total_questions': question_count, 'question_ids': question_ids, 'forum': forum, @@ -344,12 +347,17 @@ class website_forum(http.Controller): 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): request.registry['website.forum.post'].unlink(request.cr, request.uid, [post.id], context=request.context) return werkzeug.utils.redirect("/forum/%s/" % (slug(forum))) + @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, request.uid, [int(kwarg.get('message_id'))], context=request.context) + return True + @http.route('/forum//edit/question/', type='http', auth="user", multilang=True, website=True) def edit_question(self, forum, post, **kwarg): values = { @@ -384,6 +392,12 @@ class website_forum(http.Controller): post = Post.browse(cr, uid, int(kwarg.get('post_id')), context=context) if post.create_uid.id == uid: correct = False if post.correct else True + #Note: only one answer can be right. + for child in post.parent_id.child_ids: + if child.correct: + Post.write( cr, uid, [child.id], { + 'correct': False, + }, context=context) Post.write( cr, uid, [int(kwarg.get('post_id'))], { 'correct': correct, }, context=context) diff --git a/addons/website_forum/models/website_forum.py b/addons/website_forum/models/website_forum.py index 95db9dbf125..6703a4032c7 100644 --- a/addons/website_forum/models/website_forum.py +++ b/addons/website_forum/models/website_forum.py @@ -284,6 +284,13 @@ class Badge(osv.Model): 'level': fields.selection([('bronze', 'bronze'), ('silver', 'silver'), ('gold', 'gold')], 'Forum Badge Level'), } +class MailMessage(osv.Model): + _inherit = 'mail.message' + _columns = { + 'create_uid': fields.many2one('res.users', 'Created by', readonly=True ), + } + + class Tags(osv.Model): _name = "website.forum.tag" _description = "Tag" diff --git a/addons/website_forum/static/src/js/website_forum.editor.js b/addons/website_forum/static/src/js/website_forum.editor.js index d5e459cb8a3..af006df9240 100644 --- a/addons/website_forum/static/src/js/website_forum.editor.js +++ b/addons/website_forum/static/src/js/website_forum.editor.js @@ -73,14 +73,24 @@ $(document).ready(function () { openerp.jsonRpc("/forum/correct_answer/", 'call', { 'post_id': $link.attr("id")}) .then(function (data) { + par = $link.parents().find(".oe_answer_true") + $link.parents().find(".oe_answer_true").removeClass("oe_answer_true").addClass('oe_answer_false') if (data) { $link.removeClass("oe_answer_false").addClass('oe_answer_true'); } - else { - $link.removeClass("oe_answer_true").addClass('oe_answer_false'); - } }); return false; }); + $('.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(); + }); + return false; + }); + }); \ No newline at end of file diff --git a/addons/website_forum/views/website_forum.xml b/addons/website_forum/views/website_forum.xml index 1b802de9aba..b568bbc087f 100644 --- a/addons/website_forum/views/website_forum.xml +++ b/addons/website_forum/views/website_forum.xml @@ -416,10 +416,13 @@