From f072c63ea3771646d0756bbb76deb022d4d2c323 Mon Sep 17 00:00:00 2001 From: "Turkesh Patel (Open ERP)" Date: Wed, 26 Mar 2014 18:39:50 +0530 Subject: [PATCH] [IMP] improved code of tags to add new tags, load tags by ajax. bzr revid: tpa@tinyerp.com-20140326130950-m5bzcjfhigeareij --- addons/website_forum/controllers/main.py | 23 +++++++++++++++---- .../static/src/js/website_forum.js | 23 +++++++++++++------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py index 83436516594..a86bc138f5d 100644 --- a/addons/website_forum/controllers/main.py +++ b/addons/website_forum/controllers/main.py @@ -20,6 +20,7 @@ ############################################################################## import werkzeug.urls +import simplejson from openerp import tools from openerp import SUPERUSER_ID @@ -264,12 +265,18 @@ class website_forum(http.Controller): cr, uid, context = request.cr, request.uid, request.context create_context = dict(context) + Tag = request.registry['website.forum.tag'] tags = question.get('question_tags').strip('[]').replace('"','').split(",") - tag_ids = request.registry['website.forum.tag'].search(cr, uid, [('name', 'in', tags)], context=context) + question_tags = [] + tag_ids = Tag.search(cr, uid, [('name', 'in', tags)], context=context) for tag in tag_ids: question_tags.append((4,tag)) + for tag in tags: + if not Tag.search(cr, uid, [('name', 'like', tag)], count=True, context=context): + 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, @@ -462,10 +469,15 @@ class website_forum(http.Controller): @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 + Tag = request.registry['website.forum.tag'] tags = post.get('question_tag').strip('[]').replace('"','').split(",") - tag_ids = request.registry['website.forum.tag'].search(cr, uid, [('name', 'in', tags)], context=context) + tag_ids = Tag.search(cr, uid, [('name', 'in', tags)], context=context) question_tags = [(6, 0, tag_ids)] + for tag in tags: + if not Tag.search(cr, uid, [('name', 'like', tag)], count=True, context=context): + 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'), 'name': post.get('question_name'), @@ -584,7 +596,8 @@ class website_forum(http.Controller): request.registry['website.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='json', auth="public", multilang=True, methods=['POST'], website=True) + @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) - return [ tag['name'] for tag in tags] + 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) diff --git a/addons/website_forum/static/src/js/website_forum.js b/addons/website_forum/static/src/js/website_forum.js index 13dc84804d5..7848a62fbf7 100644 --- a/addons/website_forum/static/src/js/website_forum.js +++ b/addons/website_forum/static/src/js/website_forum.js @@ -88,16 +88,25 @@ $(document).ready(function () { }); if($('input.load_tags').length){ - openerp.jsonRpc('/forum/get_tags/','call' ,{}).then(function(data){ - var previous_tags = $("input.load_tags").val(); + var previous_tags = $("input.load_tags").val(); $("input.load_tags").val(""); $("input.load_tags").textext({ - plugins : 'tags suggestions autocomplete', - tagsItems : previous_tags.split(","), - suggestions :data, + 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 + } }); - return true; - }) + // Note: Adding event handler of whitespaceKeyDown event. + $("input.load_tags").bind("whitespaceKeyDown",function () { + $(this).textext()[0].tags().addTags([ $(this).val() ]); + $(this).val(""); + }); }; }); \ No newline at end of file