hparfr
7 years ago
5 changed files with 177 additions and 0 deletions
-
70web_cache_name_get/README.rst
-
0web_cache_name_get/__init__.py
-
21web_cache_name_get/__manifest__.py
-
76web_cache_name_get/static/src/js/view_list.js
-
10web_cache_name_get/views/view.xml
@ -0,0 +1,70 @@ |
|||
.. 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 |
|||
|
|||
================ |
|||
Cache name_get() |
|||
================ |
|||
|
|||
This module improves the loading time of some views: mainly sale or purchase orders |
|||
with many order lines. |
|||
|
|||
It reduce the numer 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 theses 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 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. |
|||
|
|||
|
|||
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,21 @@ |
|||
# -*- 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.0", |
|||
"author": "Akretion, " |
|||
"Odoo Community Association (OCA)", |
|||
"license": "AGPL-3", |
|||
"category": "Usability", |
|||
"summary": "Limit useless name_get requests", |
|||
"depends": [ |
|||
'web', |
|||
], |
|||
"data": [ |
|||
'views/view.xml', |
|||
], |
|||
'installable': True, |
|||
"application": False, |
|||
} |
@ -0,0 +1,76 @@ |
|||
odoo.define('web_cache_name_get.view_list', function(require) { |
|||
"use strict"; |
|||
|
|||
var listView = require('web.ListView'); |
|||
var Model = require('web.DataModel'); |
|||
var core = require('web.core'); |
|||
var _t = core._t; |
|||
|
|||
listView.List.include({ |
|||
/* |
|||
cache name_get of many2many in order to reduce |
|||
useless requests |
|||
first part is 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; |
|||
if (column.type !== 'many2many') { |
|||
return this._super(record, column); |
|||
} else { |
|||
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; |
|||
} |
|||
// the logic starts here
|
|||
|
|||
// create a cache if not already done
|
|||
this.name_get_cache = this.name_get_cache || {}; |
|||
var prom = null; |
|||
var key = JSON.stringify([column.relation, ids, this.dataset.get_context()]); |
|||
|
|||
if (this.name_get_cache[key]) { |
|||
prom = this.name_get_cache[key]; |
|||
} |
|||
if (!prom) { |
|||
prom = new Model(column.relation) |
|||
.call('name_get', [ids, this.dataset.get_context()]); |
|||
this.name_get_cache[key] = prom; |
|||
} |
|||
// 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); |
|||
}); |
|||
} |
|||
} |
|||
return column.format(record.toForm().data, { |
|||
model: this.dataset.model, |
|||
id: record.get('id') |
|||
}); |
|||
}, |
|||
}); |
|||
}); |
@ -0,0 +1,10 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<odoo> |
|||
<data> |
|||
<template id="assets_backend" name="raf cache" 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> |
|||
</data> |
|||
</odoo> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue