Test coverage for vnccollab.content.form.raptus_autocomplete
1: import simplejson
1: from Acquisition import aq_inner
1: from Products.Five.browser import BrowserView
1: from Products.Archetypes.Registry import registerPropertyType
1: from Products.Archetypes.Registry import registerWidget
1: from Products.CMFPlone.utils import safe_unicode
1: from raptus.autocompletewidget import widget as base
2: class KeywordsAutocompleteSearch(BrowserView):
1: def __call__(self):
2: return simplejson.dumps(self._search())
1: def _search(self, param='term'):
4: context = aq_inner(self.context)
4: field = self.request.get('f', None)
4: query = self.request.get(param, '')
4: limit = self.request.get('limit', None)
4: if not query or not field:
>>>>>> return []
4: field = context.Schema().getField(field)
4: if not field:
>>>>>> return []
4: query = query.lower()
4: values = context.collectKeywords(field.getName(), field.accessor)
16: return [v for v in values if query in v.lower()]
2: class KeywordsAutocompletePopulate(KeywordsAutocompleteSearch):
1: def __call__(self):
2: query = (self.request.get('q', '') or '').strip()
2: if not query:
>>>>>> return ''
2: results = self._search('q')
5: for r in results:
4: if query == r:
1: return r
2: class KeywordsAutocompleteBaseWidget(base.AutocompleteBaseWidget):
"""Override raptus.autocompletewidget in order to set our own
ajax search autocomplete url which knows how to get Archetypes
Keywords Widget vocabulary items.
1: """
# JavaScript template
js_template = """\
(function($) {
$().ready(function() {
$('#archetypes-fieldname-%(id)s #%(id)s').each(function() {
$('#archetypes-fieldname-%(id)s').append('<input name="%(id)s-input" type="text" id="%(id)s-input" />');
%(js_populate)s
$(this).remove();
// append Add link to autocomplete input, it'll add new tags,
// that do not exist yet
var input = $('#%(id)s-input');
var add_link = input.after('<a href="#" style="margin-left: 10px" id="autocomplete-add-%(id)s">Add as a New Tag</a>');
$('#autocomplete-add-%(id)s').click(function(event){
// skip empty value
if (!input.val().strip()) {
input.val('');
return false;
}
// populate checkboxes from entered value
value = input.val().split("\\n");
for (var i=0; i<value.length; i++) {
var v = value[i].strip();
if (v) {
$('#archetypes-fieldname-%(id)s #%(id)s-input').before("<" + "label class='plain'><" + "input type='checkbox' name='%(id)s:list' checked='checked' value='" + v + "' /> " + v + "</label><br />");
}
}
// reset input
input.val('');
return false;
});
$('#archetypes-fieldname-%(id)s #%(id)s-input').autocomplete({
source: '%(url)s/@@keywordsautocompletewidget-search?f=%(id)s',
minLength: %(minChars)d,
select: %(js_callback)s
});
})
});
})(jQuery);
1: """
2: class KeywordsAutocompleteSelectionWidget(KeywordsAutocompleteBaseWidget,
1: base.AutocompleteSelectionWidget):
1: """See doc string in above defined class"""
1: _properties = base.AutocompleteSelectionWidget._properties.copy()
# JavaScript template
# the funny <" + "input bit is to prevent breakage in testbrowser tests
# when it parses the js as a real input, but with a bogus value
js_callback_template = """\
function(event, ui) {
var data = ui.item ? ui.item.value : '';
var field = $('#archetypes-fieldname-%(id)s input[type="radio"][value="' + data + '"]');
if(field.length == 0)
$('#archetypes-fieldname-%(id)s #%(id)s-input').before("<" + "label class='plain'><" + "input type='radio' name='%(id)s' checked='checked' value='" + data + "' /> " + data + "</label><br />");
else
field.each(function() { this.checked = true });
if(data) {
$('#archetypes-fieldname-%(id)s #%(id)s-input').val('');
return false;
}
}
1: """
js_populate_template = """\
var value = $(this).val();
if(value)
$.get('%(url)s/@@keywordsautocompletewidget-populate', {'f': '%(id)s', 'q': value}, function(data) {
if(data) {
$('#archetypes-fieldname-%(id)s #%(id)s-input').before("<" + "label class='plain'><" + "input type='radio' name='%(id)s' checked='checked' value='" + data + "' /> " + data + "</label><br />");
}
}, 'json');
1: """
2: class KeywordsAutocompleteMultiSelectionWidget(KeywordsAutocompleteBaseWidget,
1: base.AutocompleteMultiSelectionWidget):
1: """See doc string in above defined class"""
1: _properties = base.AutocompleteMultiSelectionWidget._properties.copy()
# JavaScript template
# the funny <" + "input bit is to prevent breakage in testbrowser tests
# when it parses the js as a real input, but with a bogus value
js_callback_template = """\
function(event, ui) {
var data = ui.item ? ui.item.value : '';
var field = $('#archetypes-fieldname-%(id)s input[type="checkbox"][value="' + data + '"]');
if(field.length == 0)
$('#archetypes-fieldname-%(id)s #%(id)s-input').before("<" + "label class='plain'><" + "input type='checkbox' name='%(id)s:list' checked='checked' value='" + data + "' /> " + data + "</label><br />");
else
field.each(function() { this.checked = true });
if(data) {
$('#archetypes-fieldname-%(id)s #%(id)s-input').val('');
return false;
}
}
1: """
js_populate_template = """\
value = $(this).text().split("\\n");
if(value)
for(var i=0; i<value.length; i++)
$.get('%(url)s/@@keywordsautocompletewidget-populate', {'f': '%(id)s', 'q': value[i]}, function(data) {
if(data) {
$('#archetypes-fieldname-%(id)s #%(id)s-input').before("<" + "label class='plain'><" + "input type='checkbox' name='%(id)s:list' checked='checked' value='" + data + "' /> " + data + "</label><br />");
}
}, 'html');
1: """
1: registerWidget(KeywordsAutocompleteSelectionWidget,
1: title='Keywords Autocomplete selection',
1: description=(''),
1: used_for=('Products.Archetypes.Field.StringField',)
)
1: registerWidget(KeywordsAutocompleteMultiSelectionWidget,
1: title='Keywords Autocomplete multiselection',
1: description=(''),
1: used_for=('Products.Archetypes.Field.LinesField',)
)
1: registerPropertyType('autoFill', 'boolean', KeywordsAutocompleteSelectionWidget)
1: registerPropertyType('minChars', 'integer', KeywordsAutocompleteSelectionWidget)
1: registerPropertyType('maxResults', 'integer',
1: KeywordsAutocompleteSelectionWidget)
1: registerPropertyType('mustMatch', 'boolean',
1: KeywordsAutocompleteSelectionWidget)
1: registerPropertyType('matchContains', 'boolean',
1: KeywordsAutocompleteSelectionWidget)
1: registerPropertyType('formatItem', 'string',
1: KeywordsAutocompleteSelectionWidget)
1: registerPropertyType('formatResult', 'string',
1: KeywordsAutocompleteSelectionWidget)