Skip to content

Commit

Permalink
[IMP] website_field_autocomplete: Upgrade for modularity
Browse files Browse the repository at this point in the history
* Break controller getters into two methods to allow for easier inheritance manipulation
* Add method in JS to return autocomplete initialization args
* Add self.record_ids to represent searched RecordSet
* Update request type to JSON in AJAX
  • Loading branch information
lasley committed Jul 9, 2016
1 parent fc5498a commit b3c1d67
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion website_field_autocomplete/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Website Field - AutoComplete",
"summary": 'Provides an autocomplete field for Website on any model',
"version": "9.0.1.0.0",
"version": "9.0.1.0.1",
"category": "Website",
"website": "https://laslabs.com/",
"author": "LasLabs, Odoo Community Association (OCA)",
Expand Down
27 changes: 21 additions & 6 deletions website_field_autocomplete/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,30 @@ class Website(Website):
methods=['GET'],
website=True,
)
def _get_autocomplete_data(self, model, **kwargs):
res = []
def _get_field_autocomplete(self, model, **kwargs):
""" Return json autocomplete data """
domain = json.loads(kwargs.get('domain', "[]"))
fields = json.loads(kwargs.get('fields', "[]"))
limit = kwargs.get('limit', None)
res = self._get_autocomplete_data(model, domain, fields, limit)
return json.dumps(res.values())

def _get_autocomplete_data(self, model, domain, fields, limit=None):
""" Gets and returns raw record data
Params:
model: Model name to query on
domain: Search domain
fields: List of fields to get
limit: Limit results to
Returns:
Dict of record dicts, keyed by ID
"""
res = {}
if limit:
limit = int(limit)
for rec_id in request.env[model].search(domain, limit=limit):
res.append({
self.record_ids = request.env[model].search(domain, limit=limit)
for rec_id in self.record_ids:
res[rec_id.id] = {
k: getattr(rec_id, k, None) for k in fields
})
return json.dumps(res)
}
return res
18 changes: 11 additions & 7 deletions website_field_autocomplete/static/src/js/field_autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ odoo.define('website_field_autocomplete.field_autocomplete', function(require){
domain = domain.concat(this.add_domain);
}
return $.ajax({
dataType: 'json',
url: '/website/field_autocomplete/' + self.model,
method: 'GET',
data: {
Expand All @@ -31,7 +32,6 @@ odoo.define('website_field_autocomplete.field_autocomplete', function(require){
limit: self.limit,
},
}).then(function(records) {
records = JSON.parse(records);
var data = records.reduce(function(a, b) {
a.push({label: b[self.displayField], value: b[self.valueField]});
return a;
Expand All @@ -41,8 +41,16 @@ odoo.define('website_field_autocomplete.field_autocomplete', function(require){
});
},

/* Return arguments that are used to initialize autocomplete */
autocompleteArgs: function() {
return {
source: function(request, response) {
this.autocomplete(request, response);
},
};
},

start: function() {
var self = this;
this.model = this.$target.data('model');
this.queryField = this.$target.data('query-field') || 'name';
this.displayField = this.$target.data('display-field') || this.queryField;
Expand All @@ -53,11 +61,7 @@ odoo.define('website_field_autocomplete.field_autocomplete', function(require){
if (this.valueField != this.displayField) {
this.fields.push(this.valueField);
}
this.$target.autocomplete({
source: function(request, response) {
self.autocomplete(request, response);
},
});
this.$target.autocomplete(this.autocompleteArgs());
},

});
Expand Down

0 comments on commit b3c1d67

Please sign in to comment.