diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py index d05601025c2..f43a2540f8e 100644 --- a/addons/website_forum/controllers/main.py +++ b/addons/website_forum/controllers/main.py @@ -341,18 +341,26 @@ class website_forum(http.Controller): }, 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, post, **kwargs): + @http.route(['/forum//question//editanswer', + '/forum//answer//edit/'] + , type='http', auth="user", website=True, multilang=True) + def edit_answer(self, forum, post, answer='', **kwargs): cr, uid, context = request.cr, request.uid, request.context request.registry['res.users'].write(cr, SUPERUSER_ID, uid, {'forum': True}, context=context) - for answer in post.child_ids: - if answer.user_id.id == request.uid: - post_answer = answer + for record in post.child_ids: + if record.user_id.id == request.uid and not answer: + answer = record + + history_obj = request.registry['website.forum.post.history'] + history_ids = history_obj.search(cr, uid, [('post_id','=', answer.id)], order = "id desc", context=context) + post_history = history_obj.browse(cr, uid, history_ids, context=context) + values = { 'post': post, - 'post_answer': post_answer, + 'post_answer': answer, 'notifications': self._get_notifications(), 'forum': forum, + 'post_history': post_history, 'searches': kwargs } return request.website.render("website_forum.edit_answer", values) @@ -363,7 +371,7 @@ class website_forum(http.Controller): request.registry['res.users'].write(cr, SUPERUSER_ID, uid, {'forum': True}, context=context) answer_id = int(post.get('answer_id')) new_question_id = request.registry['website.forum.post'].write( cr, uid, [answer_id], { - 'content': post.get('answer_content'), + 'content': post.get('content'), }, context=context) return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('post_id'))) @@ -467,6 +475,11 @@ class website_forum(http.Controller): @http.route('/forum//edit/question/', type='http', auth="user", multilang=True, website=True) def edit_question(self, forum, post, **kwarg): + cr, uid, context = request.cr, request.uid, request.context + history_obj = request.registry['website.forum.post.history'] + history_ids = history_obj.search(cr, uid, [('post_id','=', post.id)], order = "id desc", context=context) + post_history = history_obj.browse(cr, uid, history_ids, context=context) + tags = "" for tag_name in post.tags: tags += tag_name.name + "," @@ -477,9 +490,24 @@ class website_forum(http.Controller): 'forum': forum, 'searches': kwarg, 'notifications': self._get_notifications(), + 'post_history': post_history, } return request.website.render("website_forum.edit_question", values) + @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//question/savequestion/', type='http', auth="user", multilang=True, methods=['POST'], website=True) def save_edited_question(self, forum, **post): cr, uid, context = request.cr, request.uid, request.context @@ -494,23 +522,12 @@ class website_forum(http.Controller): question_tags.append((0,0,{'name' : tag,'forum_id' : forum.id})) request.registry['website.forum.post'].write(cr, uid, [int(post.get('post_id'))], { - 'content': post.get('answer_content'), + 'content': post.get('content'), 'name': post.get('question_name'), 'tags' : question_tags, }, context=context) return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('post_id'))) - @http.route('/forum//answer//edit/', type='http', auth="user", multilang=True, website=True) - def edit_ans(self, forum, post, answer, **kwarg): - values = { - 'post': post, - 'post_answer': answer, - 'forum': forum, - 'searches': kwarg, - 'notifications': self._get_notifications(), - } - return request.website.render("website_forum.edit_answer", values) - @http.route('/forum/correct_answer/', type='json', auth="user", multilang=True, methods=['POST'], website=True) def correct_answer(self, **kwarg): cr, uid, context = request.cr, request.uid, request.context @@ -637,3 +654,4 @@ class website_forum(http.Controller): 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)) + diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py index f0f14d4b2fc..031d1647306 100644 --- a/addons/website_forum/models/forum.py +++ b/addons/website_forum/models/forum.py @@ -22,9 +22,11 @@ import re import openerp +from openerp import tools from openerp import SUPERUSER_ID from openerp.osv import osv, fields from openerp.tools.translate import _ +from datetime import date, datetime from openerp.addons.website.models.website import slug @@ -198,16 +200,19 @@ class Post(osv.Model): 'active': True, } - def create_history(self, cr, uid, ids, vals, context=None): + def create_history(self, cr, uid, ids, context=None): hist_obj = self.pool['website.forum.post.history'] for post in self.browse(cr, uid, ids, context=context): + history_count = hist_obj.search(cr, uid, [('post_id','=', post.id)], count=True, context=context) + date = datetime.today().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT) + name = "No.%s Revision" %(history_count+1) if history_count else "initial version" hist_obj.create(cr, uid, { 'post_id': post.id, 'content': post.content, - 'name': post.name, + 'name': '%s - (%s) %s' % (history_count+1, date, name), + 'post_name': post.name, 'tags': [(6,0, [x.id for x in post.tags])], - 'date': post.write_date or post.create_date, - 'user_id': post.write_uid and post.write_uid.id or post.user_id.id + 'user_id': post.write_uid and post.write_uid.id or post.user_id.id, }, context=context) def create(self, cr, uid, vals, context=None): @@ -223,13 +228,15 @@ class Post(osv.Model): #add 2 karma to user when asks question. self.pool['res.users'].write(cr, SUPERUSER_ID, [vals.get('user_id')], {'karma': 2}, context=context) post_id = super(Post, self).create(cr, uid, vals, context=create_context) + self.create_history(cr, uid, [post_id], context=context) self.message_post(cr, uid, [post_id], body=_(body), subtype=subtype, context=context) return post_id def write(self, cr, uid, ids, vals, context=None): - self.create_history(cr, uid, ids, vals, context=context) + super(Post, self).write(cr, uid, ids, vals, context=context) #NOTE: to avoid message post on write of last comment time if not vals.get('message_last_post'): + self.create_history(cr, uid, ids, context=context) for post in self.browse(cr, uid, ids, context=context): body, subtype = "Edited question", "website_forum.mt_question_edit" if post.parent_id: @@ -243,7 +250,7 @@ class Post(osv.Model): elif vals.get('correct') == False: value = -15 self.pool['res.users'].write(cr, SUPERUSER_ID, [post.user_id.id], {'karma': value}, context=context) - return super(Post, self).write(cr, uid, ids, vals, context=context) + return True class PostStatistics(osv.Model): _name = "website.forum.post.statistics" @@ -310,11 +317,11 @@ class PostHistory(osv.Model): _description = 'Post History' _inherit = ['website.seo.metadata'] _columns = { - 'name': fields.char('Post Title'), + 'name': fields.char('History Title'), 'post_id': fields.many2one('website.forum.post', 'Post', ondelete='cascade'), - 'date': fields.datetime('Created on', select=True, readonly=True), + 'post_name': fields.char('Post Name'), + 'content': fields.text('Content'), 'user_id': fields.many2one('res.users', 'Created by', select=True, readonly=True), - 'content': fields.html('Contents', help='Automatically sanitized HTML contents'), 'tags': fields.many2many('website.forum.tag', 'post_tag_rel', 'post_id', 'post_tag_id', 'Tag'), } diff --git a/addons/website_forum/static/src/js/website_forum.js b/addons/website_forum/static/src/js/website_forum.js index 4dd3b66d255..b7064d236ed 100644 --- a/addons/website_forum/static/src/js/website_forum.js +++ b/addons/website_forum/static/src/js/website_forum.js @@ -95,25 +95,43 @@ $(document).ready(function () { }); if($('input.load_tags').length){ - var previous_tags = $("input.load_tags").val(); - $("input.load_tags").val(""); - $("input.load_tags").textext({ - plugins: 'tags focus autocomplete ajax', - tagsItems: previous_tags.split(","), - //Note: The following list of keyboard keys is added. All entries are default except {32 : 'whitespace!'}. - keys: {8: 'backspace', 9: 'tab', 13: 'enter!', 27: 'escape!', 37: 'left', 38: 'up!', 39: 'right', - 40: 'down!', 46: 'delete', 108: 'numpadEnter', 32: 'whitespace!'}, - ajax: { - url: '/forum/get_tags', - dataType: 'json', - cacheResults: true - } - }); - // Note: Adding event handler of whitespaceKeyDown event. - $("input.load_tags").bind("whitespaceKeyDown",function () { - $(this).textext()[0].tags().addTags([ $(this).val() ]); - $(this).val(""); - }); + var tags = $("input.load_tags").val(); + $("input.load_tags").val(""); + set_tags(tags); }; + function set_tags(tags) { + $("input.load_tags").textext({ + plugins: 'tags focus autocomplete ajax', + tagsItems: tags.split(","), + //Note: The following list of keyboard keys is added. All entries are default except {32 : 'whitespace!'}. + keys: {8: 'backspace', 9: 'tab', 13: 'enter!', 27: 'escape!', 37: 'left', 38: 'up!', 39: 'right', + 40: 'down!', 46: 'delete', 108: 'numpadEnter', 32: 'whitespace!'}, + ajax: { + url: '/forum/get_tags', + dataType: 'json', + cacheResults: true + } + }); + // Note: Adding event handler of whitespaceKeyDown event. + $("input.load_tags").bind("whitespaceKeyDown",function () { + $(this).textext()[0].tags().addTags([ $(this).val() ]); + $(this).val(""); + }); + } + + $('.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; + }); + }); \ 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 34cf953617f..67bb5428003 100644 --- a/addons/website_forum/views/website_forum.xml +++ b/addons/website_forum/views/website_forum.xml @@ -295,10 +295,15 @@

Edit question

- + + + + +
Please enter a descriptive question (should finish by a '?')
-
@@ -308,7 +313,7 @@
@@ -360,7 +365,12 @@

Edit answer

- @@ -368,7 +378,7 @@