diff --git a/website_field_autocomplete/README.rst b/website_field_autocomplete/README.rst index e13d27e275..60bf3ae261 100755 --- a/website_field_autocomplete/README.rst +++ b/website_field_autocomplete/README.rst @@ -53,7 +53,8 @@ Following is a breakdown of the available attributes & their defaults: Known Issues / Road Map ======================= -* Use of Model/jsonRPC requires a user session. +* Replace jQuery UI Autocomplete w/ HTML5 Datalist when `ready for production +`_. Bug Tracker diff --git a/website_field_autocomplete/__init__.py b/website_field_autocomplete/__init__.py index e50cfb2d65..eab4a98a75 100755 --- a/website_field_autocomplete/__init__.py +++ b/website_field_autocomplete/__init__.py @@ -1,3 +1,5 @@ # -*- coding: utf-8 -*- # Copyright 2016 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import controllers diff --git a/website_field_autocomplete/controllers/__init__.py b/website_field_autocomplete/controllers/__init__.py new file mode 100755 index 0000000000..9c63968e9a --- /dev/null +++ b/website_field_autocomplete/controllers/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 LasLabs Inc. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import main diff --git a/website_field_autocomplete/controllers/main.py b/website_field_autocomplete/controllers/main.py new file mode 100755 index 0000000000..ab69cdd55b --- /dev/null +++ b/website_field_autocomplete/controllers/main.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 LasLabs Inc. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +import json +from openerp import http +from openerp.http import request +from openerp.addons.website.controllers.main import Website + + +class Website(Website): + + @http.route( + '/website/field_autocomplete/', + type='http', + auth='public', + methods=['GET'], + website=True, + ) + def _get_autocomplete_data(self, model, **kwargs): + res = [] + domain = json.loads(kwargs.get('domain', "[]")) + fields = json.loads(kwargs.get('fields', "[]")) + limit = kwargs.get('limit', None) + if limit: + limit = int(limit) + for rec_id in request.env[model].search(domain, limit=limit): + res.append({ + k: getattr(rec_id, k, None) for k in fields + }) + return json.dumps(res) diff --git a/website_field_autocomplete/static/src/js/field_autocomplete.js b/website_field_autocomplete/static/src/js/field_autocomplete.js index 6722913430..2044995698 100644 --- a/website_field_autocomplete/static/src/js/field_autocomplete.js +++ b/website_field_autocomplete/static/src/js/field_autocomplete.js @@ -6,7 +6,7 @@ odoo.define('website_field_autocomplete.field_autocomplete', function(require){ "use strict"; var snippet_animation = require('web_editor.snippets.animation'); - var Model = require('web.Model'); + var $ = require('$'); snippet_animation.registry.field_autocomplete = snippet_animation.Class.extend({ @@ -22,10 +22,16 @@ odoo.define('website_field_autocomplete.field_autocomplete', function(require){ if (this.add_domain) { domain = domain.concat(this.add_domain); } - var args = [domain, [this.displayField]]; - return this.QueryModel.call('search_read', args, {limit: this.limit}) - .then(function(records) { - var data = records.reduce(function(a, b) { + return $.ajax({ + url: '/website/field_autocomplete/' + self.model, + method: 'GET', + data: { + domain: JSON.stringify(domain), + fields: JSON.stringify(self.fields), + limit: self.limit, + }, + }).then(function(records) { + var data = JSON.parse(records).reduce(function(a, b) { a.push(b[self.displayField]); return a; }, []); @@ -35,11 +41,12 @@ odoo.define('website_field_autocomplete.field_autocomplete', function(require){ start: function() { var self = this; - this.QueryModel = new Model(this.$target.data('model')); + this.model = this.$target.data('model'); this.queryField = this.$target.data('query-field') || 'name'; this.displayField = this.$target.data('display-field') || this.queryField; this.limit = this.$target.data('limit') || 10; this.add_domain = this.$target.data('domain'); + this.fields = [this.displayField]; this.$target.autocomplete({ source: function(request, response) { self.autocomplete(request, response);