Hpar
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 219 additions and 0 deletions
-
72web_cache_name_get/README.rst
-
0web_cache_name_get/__init__.py
-
22web_cache_name_get/__manifest__.py
-
117web_cache_name_get/static/src/js/view_list.js
-
8web_cache_name_get/views/view.xml
@ -0,0 +1,72 @@ |
|||||
|
.. image:: https://img.shields.io/badge/license-LGPL--3-blue.svg |
||||
|
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html |
||||
|
:alt: License: LGPL-3 |
||||
|
|
||||
|
================== |
||||
|
Web Cache Name Get |
||||
|
================== |
||||
|
|
||||
|
This module improves the loading time of some views: mainly sale or purchase orders |
||||
|
with many order lines. |
||||
|
|
||||
|
It reduce the number of useless requests done by the user's browser. |
||||
|
|
||||
|
|
||||
|
This modules cache some requests to name_get() done from list views. |
||||
|
|
||||
|
For instance, if you have 40 sale order lines on a sale. All these lines have the same tax. |
||||
|
|
||||
|
- Without this module : your browser makes about 40 time the same name_get() request. |
||||
|
- With this modules: your browser makes 1 request and reuse the result for all the lines. |
||||
|
|
||||
|
It works on many2many and many2one on list views. |
||||
|
The cache is cleaned when you navigate to other objects. |
||||
|
|
||||
|
|
||||
|
Usage |
||||
|
===== |
||||
|
|
||||
|
|
||||
|
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas |
||||
|
:alt: Try me on Runbot |
||||
|
:target: https://runbot.odoo-community.org/runbot/162/10.0 |
||||
|
|
||||
|
Known issues / Roadmap |
||||
|
====================== |
||||
|
|
||||
|
* The cache is not very aggressive, if put more globally it may catch more useless requests. |
||||
|
* some requests can be done twice in many2one, because of 'this' been erased in |
||||
|
other parts of the core code. |
||||
|
|
||||
|
|
||||
|
Credits |
||||
|
======= |
||||
|
|
||||
|
Contributors |
||||
|
------------ |
||||
|
|
||||
|
* Raphaël Reverdy <raphael.reverdy@akretion.com> |
||||
|
|
||||
|
|
||||
|
Funders |
||||
|
------- |
||||
|
|
||||
|
The development of this module has been financially supported by: |
||||
|
|
||||
|
* Akretion |
||||
|
|
||||
|
|
||||
|
Maintainer |
||||
|
---------- |
||||
|
|
||||
|
.. image:: https://odoo-community.org/logo.png |
||||
|
:alt: Odoo Community Association |
||||
|
:target: https://odoo-community.org |
||||
|
|
||||
|
This module is maintained by the OCA. |
||||
|
|
||||
|
OCA, or the Odoo Community Association, is a nonprofit organization whose |
||||
|
mission is to support the collaborative development of Odoo features and |
||||
|
promote its widespread use. |
||||
|
|
||||
|
To contribute to this module, please visit https://odoo-community.org. |
@ -0,0 +1,22 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# Copyright 2018 Raphaël Reverdy - Akretion |
||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
||||
|
|
||||
|
{ |
||||
|
"name": "Cache name_get", |
||||
|
"version": "10.0.1.0.1", |
||||
|
"website": "https://github.com/OCA/web", |
||||
|
"author": "Akretion, " |
||||
|
"Odoo Community Association (OCA)", |
||||
|
"license": "AGPL-3", |
||||
|
"category": "Usability", |
||||
|
"summary": "Limit useless name_get requests", |
||||
|
"maintainers": ["hparfr"], |
||||
|
"depends": [ |
||||
|
'web', |
||||
|
], |
||||
|
"data": [ |
||||
|
'views/view.xml', |
||||
|
], |
||||
|
'installable': True, |
||||
|
} |
@ -0,0 +1,117 @@ |
|||||
|
odoo.define('web_cache_name_get.view_list', function(require) { |
||||
|
"use strict"; |
||||
|
|
||||
|
var listView = require('web.ListView'); |
||||
|
var Model = require('web.DataModel'); |
||||
|
var data = require('web.data'); |
||||
|
var core = require('web.core'); |
||||
|
var _t = core._t; |
||||
|
|
||||
|
listView.List.include({ |
||||
|
web_cache_name_get: function(key, callback) { |
||||
|
//will return a promise with the result of
|
||||
|
//the name_get
|
||||
|
//it will cache the request in order to save brandwith
|
||||
|
//and useless requests
|
||||
|
var prom = null; |
||||
|
var key = JSON.stringify(key); |
||||
|
//init cache if not already done
|
||||
|
this.name_get_cache = this.name_get_cache || {}; |
||||
|
|
||||
|
if (this.name_get_cache[key]) { |
||||
|
//cache hit
|
||||
|
prom = this.name_get_cache[key]; |
||||
|
} |
||||
|
if (!prom) { |
||||
|
//cache miss
|
||||
|
prom = callback(); |
||||
|
this.name_get_cache[key] = prom |
||||
|
} |
||||
|
return prom; |
||||
|
}, |
||||
|
/* |
||||
|
cache name_get of many2many and many2one in order to reduce |
||||
|
useless requests |
||||
|
some parts are a copy paste of web/static/src/js/views/list_view.js |
||||
|
because we have hook into it and can't do much better than that. |
||||
|
*/ |
||||
|
render_cell: function (record, column) { |
||||
|
var value; |
||||
|
var self = this; |
||||
|
if (column.type === 'many2one') { |
||||
|
value = record.get(column.id); |
||||
|
// m2o values are usually name_get formatted, [Number, String]
|
||||
|
// pairs, but in some cases only the id is provided. In these
|
||||
|
// cases, we need to perform a name_get call to fetch the actual
|
||||
|
// displayable value
|
||||
|
if (typeof value === 'number' || value instanceof Number) { |
||||
|
// fetch the name, set it on the record (in the right field)
|
||||
|
// and let the various registered events handle refreshing the
|
||||
|
// row
|
||||
|
|
||||
|
// our logic starts here
|
||||
|
prom = this.web_cache_name_get( |
||||
|
[this.view.model, this.view.name, column.relation, value], |
||||
|
function () { |
||||
|
return new data.DataSet(self.view, column.relation) |
||||
|
.name_get([value]); |
||||
|
} |
||||
|
); |
||||
|
prom.done(function (names) { |
||||
|
if (!names.length) { return; } |
||||
|
record.set(column.id, names[0]); |
||||
|
}); |
||||
|
} |
||||
|
} else if (column.type === 'many2many') { |
||||
|
value = record.get(column.id); |
||||
|
// non-resolved (string) m2m values are arrays
|
||||
|
if (value instanceof Array && !_.isEmpty(value) |
||||
|
&& !record.get(column.id + '__display')) { |
||||
|
var ids; |
||||
|
// they come in two shapes:
|
||||
|
if (value[0] instanceof Array) { |
||||
|
_.each(value, function(command) { |
||||
|
switch (command[0]) { |
||||
|
case 4: ids.push(command[1]); break; |
||||
|
case 5: ids = []; break; |
||||
|
case 6: ids = command[2]; break; |
||||
|
default: throw new Error(_.str.sprintf( _t("Unknown m2m command %s"), command[0])); |
||||
|
} |
||||
|
}); |
||||
|
} else { |
||||
|
// 2. an array of ids
|
||||
|
ids = value; |
||||
|
} |
||||
|
// our logic starts here
|
||||
|
var prom = this.web_cache_name_get( |
||||
|
[column.relation, ids, this.dataset.get_context()], |
||||
|
function () { |
||||
|
return new Model(column.relation) |
||||
|
.call('name_get', [ids, self.dataset.get_context()]) |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
// put placeholder at first to limit races conditions
|
||||
|
// because of the cached answers
|
||||
|
// promise resolution can happen before placeholder set
|
||||
|
// and lead to "too much recursion"
|
||||
|
|
||||
|
// temporary empty display name
|
||||
|
record.set(column.id + '__display', false); |
||||
|
prom.done(function (names) { |
||||
|
// FIXME: nth horrible hack in this poor listview
|
||||
|
record.set(column.id + '__display', |
||||
|
_(names).pluck(1).join(', ')); |
||||
|
record.set(column.id, ids); |
||||
|
}); |
||||
|
} |
||||
|
} else { |
||||
|
return this._super(record, column); |
||||
|
} |
||||
|
return column.format(record.toForm().data, { |
||||
|
model: this.dataset.model, |
||||
|
id: record.get('id') |
||||
|
}); |
||||
|
}, |
||||
|
}); |
||||
|
}); |
@ -0,0 +1,8 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<odoo> |
||||
|
<template id="assets_backend" name="web_cache_nameget" inherit_id="web.assets_backend"> |
||||
|
<xpath expr="." position="inside"> |
||||
|
<script type="text/javascript" src="/web_cache_name_get/static/src/js/view_list.js"></script> |
||||
|
</xpath> |
||||
|
</template> |
||||
|
</odoo> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue