[IMP] improved code of tags to add new tags, load tags by ajax.

bzr revid: tpa@tinyerp.com-20140326130950-m5bzcjfhigeareij
This commit is contained in:
Turkesh Patel (Open ERP)
2014-03-26 18:39:50 +05:30
parent 719aa0d734
commit f072c63ea3
2 changed files with 34 additions and 12 deletions
+18 -5
View File
@@ -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/<model("website.forum"):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)
@@ -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("");
});
};
});