diff options
| author | Denis Ledoux <dle@odoo.com> | 2019-08-22 10:35:40 +0000 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2019-09-11 07:55:17 +0000 |
| commit | 0e028de72ea0f49586790b580774bac203a8ed96 (patch) | |
| tree | 4ce89f74753e54fbe99d003ba9c4e112350f7ebb /odoo/api.py | |
| parent | 0bf6fb05196ac92bb8a7626f1c398ee8683b2ddb (diff) | |
[IMP] api: lazy_property for user, company, companies
These variable are not supposed to change within a same
environment.
The lazy property will compute these variables only
once, then store the result,
while the property were computing these variables
each time they were called.
e.g. for a 1000 iteration loop,
with `env.company`,
the company was computed 1000 times.
With a lazy property, the company will be computed one time only.
Diffstat (limited to 'odoo/api.py')
| -rw-r--r-- | odoo/api.py | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/odoo/api.py b/odoo/api.py index 88cc4c9490c..1bba63b4400 100644 --- a/odoo/api.py +++ b/odoo/api.py @@ -51,7 +51,7 @@ from decorator import decorate, decorator from werkzeug.local import Local, release_local import odoo -from odoo.tools import frozendict, classproperty, StackMap +from odoo.tools import frozendict, classproperty, lazy_property, StackMap from odoo.exceptions import CacheMiss _logger = logging.getLogger(__name__) @@ -518,12 +518,12 @@ class Environment(Mapping): superuser mode. """ return self.su or self.user._is_system() - @property + @lazy_property def user(self): """ return the current user (as an instance) """ return self(su=True)['res.users'].browse(self.uid) - @property + @lazy_property def company(self): """ return the company in which the user is logged in (as an instance) """ company_ids = self.context.get('allowed_company_ids', False) @@ -533,7 +533,7 @@ class Environment(Mapping): return self['res.company'].browse(company_id) return self.user.company_id - @property + @lazy_property def companies(self): """ return a recordset of the enabled companies by the user """ try: # In case the user tries to bidouille the url (eg: cids=1,foo,bar) |
