diff options
| author | Lucas Perais (lpe) <lpe@odoo.com> | 2020-01-31 13:33:02 +0000 |
|---|---|---|
| committer | Lucas Perais (lpe) <lpe@odoo.com> | 2020-03-02 13:22:09 +0000 |
| commit | 9ce6136be700ae426b8910efd2357d16e2f49d7d (patch) | |
| tree | 03a937506785ef775c306e857befcec77c2df108 /addons/web/models | |
| parent | 5c127860fc111577aa68207bdcec27b3bde6f39e (diff) | |
[FIX] web, base: make company-specific report assets static
Before this commit, the company specific colors and font were
implemented by systematically overwriting a "virtual" report SCSS
asset file before rendering each report.
This caused many issues, forced frequent asset bundles recomputations
(performance problem + cache invalidation causing random bugs).
And it could simply not work in a multi-company setup where multiple
styles are involved, as the asset management could be made not
thread-safe.
PR #44225 was a first attempt to mitigate the numerous problems by
making the asset bundle invalidation less frequent. But the problems
were still present and a more complete solution was necessary for
multi-company setups.
Besides, the design of the bundle forbade making company specific assets.
This commit uses a different approach: instead of having a
company-specific asset that needs to be constantly updated, a global
"multi-company" asset is maintained and included in the report assets.
It only needs to be generated when a company style changes, not for
every rendering operation.
Unfortunately this change cannot be fully performed without updating the
template declarations, so it will require an update of the `web` (or
`base`) module to be operational.
As this represents a rather invasive change in a stable branch, extensive
testing was conducted to minimize the effects and ensure proper
degradation of features for production deployments where the new code
would be deployed without forcing an update of the `web` module:
- The report SCSS files were left untouched, to prevent any bundle
invalidation, ensuring that old cached assets would remain valid. This
means that single-company setups should not see any visible difference
after pulling the code (with or without updating `web`).
SCSS cleanup will be done later.
- Existing Python methods were kept but emptied, to make sure that
old templates and code would not crash.
- For multi-companies, the last used colors will be applied for all
reports until the `web` module is updated. The old behavior was not
working correctly anyways, so the degradation is actually limited.
- For all setups, changing the colors after deploying this patch will
have no effect unless the `web` module is updated.
/UPDATED FOR master on top of #44393/:
- removed the `res_company.update_scss()` method entirely
- fixed the report SCSS styles, as the variable names referred to the old
behavior, e.g. "$o-company-primary-color" is nonsense.
Renamed to "$o-default-report-primary-color" etc.
--
Improves #44225
Forward of #44393
opw-2168623
opw-2171040
closes odoo/odoo#46647
X-original-commit: a5b1421aecf27b1de408434d3bb6d7ac81f57dc3
Signed-off-by: Olivier Dony (odo) <odo@openerp.com>
Diffstat (limited to 'addons/web/models')
| -rw-r--r-- | addons/web/models/models.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/addons/web/models/models.py b/addons/web/models/models.py index 3a70ab4f0c8..7f6e83554e3 100644 --- a/addons/web/models/models.py +++ b/addons/web/models/models.py @@ -2,6 +2,7 @@ import babel.dates import pytz from lxml import etree +import base64 from odoo import _, api, fields, models from odoo.osv.expression import AND @@ -360,3 +361,41 @@ class Base(models.AbstractModel): }) return filter_values + + +class ResCompany(models.Model): + _inherit = 'res.company' + + @api.model + def create(self, values): + res = super().create(values) + if 'primary_color' in values or 'secondary_color' in values or 'font' in values: + self._update_asset_style() + return res + + def write(self, values): + res = super().write(values) + if 'primary_color' in values or 'secondary_color' in values or 'font' in values: + self._update_asset_style() + return res + + def _get_asset_style_b64(self): + template_style = self.env.ref('web.styles_company_report', raise_if_not_found=False) + if not template_style: + return '' + # One bundle for everyone, so this method + # necessarily updates the style for every company at once + company_ids = self.sudo().search([]) + company_styles = template_style.render({ + 'company_ids': company_ids, + }) + return base64.b64encode((company_styles)) + + def _update_asset_style(self): + asset_attachment = self.env.ref('web.asset_styles_company_report', raise_if_not_found=False) + if not asset_attachment: + return + asset_attachment = asset_attachment.sudo() + b64_val = self._get_asset_style_b64() + if b64_val != asset_attachment.datas: + asset_attachment.write({'datas': b64_val}) |
