[FIX] website_forum - replace texttext by select2 for the tags fields

This commit is contained in:
Jeremy Kersten 2014-11-14 19:14:12 +01:00
parent c92e70b929
commit 90b88e33cb
3 changed files with 142 additions and 25 deletions

View File

@ -170,9 +170,18 @@ class WebsiteForum(http.Controller):
return request.website.render("website_forum.faq", values)
@http.route('/forum/get_tags', type='http', auth="public", 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]
def tag_read(self, q='', l=25, t='texttext', **post):
data = request.registry['forum.tag'].search_read(
request.cr,
request.uid,
domain=[('name', '=ilike', (q or '') + "%")],
fields=['id', 'name'],
limit=int(l),
context=request.context
)
if t == 'texttext':
# old tag with texttext - Retro for V8 - #TODO Remove in master
data = [tag['name'] for tag in data]
return simplejson.dumps(data)
@http.route(['/forum/<model("forum.forum"):forum>/tag'], type='http', auth="public", website=True)
@ -195,7 +204,7 @@ class WebsiteForum(http.Controller):
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})
values = self._prepare_forum_values(forum=forum, searches={}, header={'ask_hide': True})
return request.website.render("website_forum.ask_question", values)
@http.route('/forum/<model("forum.forum"):forum>/question/new', type='http', auth="user", methods=['POST'], website=True)
@ -203,14 +212,23 @@ class WebsiteForum(http.Controller):
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(",")
tag_version = post.get('tag_type', 'texttext')
if tag_version == "texttext": # TODO Remove in master
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}))
elif tag_version == "select2":
tags = filter(None, post.get('question_tags', '').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]))
if tag.startswith('_'): # it's a new tag
question_tag_ids.append((0, 0, {'name': tag[1:], 'forum_id': forum.id}))
else:
question_tag_ids.append((0, 0, {'name': tag, 'forum_id': forum.id}))
question_tag_ids.append((4, int(tag)))
new_question_id = request.registry['forum.post'].create(
request.cr, request.uid, {
@ -357,10 +375,16 @@ class WebsiteForum(http.Controller):
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/edit', type='http', auth="user", website=True)
def post_edit(self, forum, post, **kwargs):
tags = ""
for tag_name in post.tag_ids:
tags += tag_name.name + ","
tag_version = kwargs.get('tag_type', 'texttext')
if tag_version == "texttext": # old version - retro v8 - #TODO Remove in master
tags = ""
for tag_name in post.tag_ids:
tags += tag_name.name + ","
elif tag_version == "select2": # new version
tags = [dict(id=tag.id, name=tag.name) for tag in post.tag_ids]
tags = simplejson.dumps(tags)
values = self._prepare_forum_values(forum=forum)
values.update({
'tags': tags,
'post': post,
@ -369,20 +393,46 @@ class WebsiteForum(http.Controller):
})
return request.website.render("website_forum.edit_post", values)
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/edition', type='http', auth="user", website=True)
def post_edit_retro(self, forum, post, **kwargs):
# This function is only there for retrocompatibility between old template using texttext and template using select2
# It should be removed into master #TODO JKE: remove in master all condition with tag_type
kwargs.update(tag_type="select2")
return self.post_edit(forum, post, **kwargs)
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/save', type='http', auth="user", methods=['POST'], website=True)
def post_save(self, forum, post, **kwargs):
cr, uid, context = request.cr, request.uid, request.context
question_tags = []
if kwargs.get('question_tag') and kwargs.get('question_tag').strip('[]'):
Tag = request.registry['forum.tag']
tags = kwargs.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
User = request.registry['res.users']
Tag = request.registry['forum.tag']
tag_version = kwargs.get('tag_type', 'texttext')
if tag_version == "texttext": # old version - retro v8 - #TODO Remove in master
if kwargs.get('question_tag') and kwargs.get('question_tag').strip('[]'):
tags = kwargs.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)
elif tag_version == "select2": # new version
for tag in filter(None, kwargs.get('question_tag', '').split(',')):
if tag.startswith('_'): # it's a new tag
# check if user have Karma needed to create need tag
user = User.browse(cr, SUPERUSER_ID, uid, context=context)
if user.exists() and user.karma >= forum.karma_retag:
# check that not arleady created meanwhile and maybe excluded by the limit on the search
tag_ids = Tag.search(cr, uid, [('name', '=', tag[1:])], context=context)
if tag_ids:
new_tag = tag_ids
else:
new_tag = Tag.create(cr, uid, {'name': tag[1:], 'forum_id': forum.id}, context=context)
question_tags.append(new_tag)
else:
new_tag = Tag.create(cr, uid, {'name': tag, 'forum_id': forum.id}, context=context)
question_tags.append(new_tag)
question_tags += [int(tag)]
vals = {
'tag_ids': [(6, 0, question_tags)],
'name': kwargs.get('question_name'),

View File

@ -120,6 +120,67 @@ $(document).ready(function () {
openerp.jsonRpc("/forum/validate_email/close", 'call', {});
});
$('input.js_select2').select2({
tags: true,
tokenSeparators: [",", " ", "_"],
maximumInputLength: 35,
minimumInputLength: 2,
maximumSelectionSize: 5,
lastsearch: [],
createSearchChoice: function (term) {
if ($(lastsearch).filter(function () { return this.text.localeCompare(term) === 0;}).length === 0) {
//check Karma
if (parseInt($("#karma").val()) >= parseInt($("#karma_retag").val())) {
return {
id: "_" + $.trim(term),
text: $.trim(term) + ' *',
isNew: true,
};
}
}
},
formatResult: function(term) {
if (term.isNew) {
return '<span class="label label-primary">New</span> ' + _.escape(term.text);
}
else {
return _.escape(term.text);
}
},
ajax: {
url: '/forum/get_tags',
dataType: 'json',
data: function(term, page) {
return {
q: term,
t: 'select2',
l: 50
};
},
results: function(data, page) {
var ret = [];
_.each(data, function(x) {
ret.push({ id: x.id, text: x.name, isNew: false });
});
lastsearch = ret;
return { results: ret };
}
},
// Take default tags from the input value
initSelection: function (element, callback) {
var data = [];
_.each(JSON.parse(element.val()), function(x) {
data.push({ id: x.id, text: x.name, isNew: false });
});
element.val('');
callback(data);
},
});
//TODO Remove in master
if($('input.load_tags').length){
var tags = $("input.load_tags").val();
$("input.load_tags").val("");
@ -150,6 +211,7 @@ $(document).ready(function () {
}
});
}
//END-TODO Remove in master
if ($('textarea.load_editor').length) {
var editor = CKEDITOR.instances['content'];
@ -159,6 +221,7 @@ $(document).ready(function () {
});
function IsKarmaValid(eventNumber,minKarma){
"use strict";
if(parseInt($("#karma").val()) >= minKarma){

View File

@ -337,11 +337,13 @@
class="form-control" placeholder="Enter your Question"/>
<h5 class="mt20">Please enter a descriptive question (should finish with a '?')</h5>
<input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
<input type="hidden" name="karma_retag" t-attf-value="#{forum.karma_retag}" id="karma_retag"/>
<textarea name="content" required="True" class="form-control load_editor">
<t t-esc="question_content"/>
</textarea>
<br/>
<input type="text" name="question_tags" placeholder="Tags" class="form-control load_tags"/>
<input type="hidden" name="tag_type" value="select2"/>
<input type="hidden" name="question_tags" placeholder="Tags" class="form-control js_select2"/>
<br/>
<button t-attf-class="btn btn-primary #{(user.karma &lt; forum.karma_ask) and 'karma_required' or ''}"
id="btn_ask_your_question" t-att-data-karma="forum.karma_ask">Post Your Question</button>
@ -364,12 +366,14 @@
<h5 class="mt20">Please enter a descriptive question (should finish by a '?')</h5>
</div>
<input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
<input type="hidden" name="karma_retag" t-attf-value="#{forum.karma_retag}" id="karma_retag"/>
<textarea name="content" required="True" class="form-control load_editor">
<t t-esc="post.content"/>
</textarea>
<div t-if="not is_answer">
<br/>
<input type="text" name="question_tag" class="form-control col-md-9 load_tags" placeholder="Tags" t-attf-value="#{tags}"/>
<input type="hidden" name="tag_type" value="select2"/>
<input type="hidden" name="question_tag" class="form-control col-md-9 js_select2" placeholder="Tags" t-attf-value="#{tags}"/>
<br/>
</div>
<button class="btn btn-primary btn-lg">Save</button>
@ -533,7 +537,7 @@
</li>
<li>
<t t-call="website_forum.link_button">
<t t-set="url" t-value="'/forum/' + slug(forum) +'/post/' + slug(question) + '/edit'"/>
<t t-set="url" t-value="'/forum/' + slug(forum) +'/post/' + slug(question) + '/edition'"/>
<t t-set="label" t-value="'Edit'"/>
<t t-set="classes" t-value="'fa-edit'"/>
<t t-set="karma" t-value="not question.can_edit and question.karma_edit or 0"/>