Skip to content

Commit

Permalink
[IMP] website_field_autocomplete: Switch from JS model to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
lasley committed Jun 24, 2016
1 parent 4a77ac8 commit b868682
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
3 changes: 2 additions & 1 deletion website_field_autocomplete/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
<http://caniuse.com/#feat=datalist>`_.


Bug Tracker
Expand Down
2 changes: 2 additions & 0 deletions website_field_autocomplete/__init__.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions website_field_autocomplete/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions website_field_autocomplete/controllers/main.py
Original file line number Diff line number Diff line change
@@ -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/<string:model>',
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)
19 changes: 13 additions & 6 deletions website_field_autocomplete/static/src/js/field_autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({

Expand All @@ -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;
}, []);
Expand All @@ -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);
Expand Down

0 comments on commit b868682

Please sign in to comment.