From 9ce6136be700ae426b8910efd2357d16e2f49d7d Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Fri, 31 Jan 2020 13:33:02 +0000 Subject: [PATCH] [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) --- addons/web/data/report_layout.xml | 7 + addons/web/models/models.py | 39 ++++ .../static/src/scss/layout_background.scss | 16 +- addons/web/static/src/scss/layout_boxed.scss | 16 +- addons/web/static/src/scss/layout_clean.scss | 20 +- .../web/static/src/scss/layout_standard.scss | 8 +- addons/web/static/src/scss/report.scss | 8 +- addons/web/views/report_templates.xml | 172 ++++++++++++++++-- odoo/addons/base/data/res_company_data.xml | 12 +- odoo/addons/base/models/res_company.py | 24 --- 10 files changed, 236 insertions(+), 86 deletions(-) diff --git a/addons/web/data/report_layout.xml b/addons/web/data/report_layout.xml index fbfbfbc21ea..d2abe98c214 100644 --- a/addons/web/data/report_layout.xml +++ b/addons/web/data/report_layout.xml @@ -25,5 +25,12 @@ /web/static/img/preview_clean.png /web/static/pdf/preview_clean.pdf + + + text/scss + res.company.scss + binary + /web/static/src/scss/asset_styles_company_report.scss + 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}) diff --git a/addons/web/static/src/scss/layout_background.scss b/addons/web/static/src/scss/layout_background.scss index 12dc0f109dc..0fea6d0a764 100644 --- a/addons/web/static/src/scss/layout_background.scss +++ b/addons/web/static/src/scss/layout_background.scss @@ -11,7 +11,7 @@ margin-right: 16px; } h3 { - color: $o-company-primary-color; + color: $o-default-report-primary-color; font-weight: 700; font-size: 1.25rem; max-width: 300px; @@ -22,12 +22,12 @@ white-space: nowrap; } ul { - border-top: 1px solid $o-company-secondary-color; - border-bottom: 1px solid $o-company-secondary-color; + border-top: 1px solid $o-default-report-secondary-color; + border-bottom: 1px solid $o-default-report-secondary-color; padding: 4px 0; margin: 0 0 4px 0; li { - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; } } } @@ -39,7 +39,7 @@ min-height: 620px; strong { - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; } .table { border-top: 1px solid gray('300'); @@ -49,10 +49,10 @@ border-top: none; } h2 { - color: $o-company-primary-color; + color: $o-default-report-primary-color; } thead tr th { - color: $o-company-secondary-color + color: $o-default-report-secondary-color } tbody { color: gray('700'); @@ -84,7 +84,7 @@ border-top: 1px solid gray('400') !important; } strong { - color: $o-company-primary-color; + color: $o-default-report-primary-color; } } } diff --git a/addons/web/static/src/scss/layout_boxed.scss b/addons/web/static/src/scss/layout_boxed.scss index a32c09379cd..d9be91da7a5 100644 --- a/addons/web/static/src/scss/layout_boxed.scss +++ b/addons/web/static/src/scss/layout_boxed.scss @@ -16,23 +16,23 @@ .o_boxed_footer { margin-top: 200px; white-space: nowrap; - border-top: 3px solid $o-company-secondary-color; + border-top: 3px solid $o-default-report-secondary-color; ul { margin: 4px 0; } } .o_report_layout_boxed { #total strong { - color: $o-company-primary-color; + color: $o-default-report-primary-color; } #informations strong { - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; } > h2 { text-transform: uppercase; } h2 span { - color: $o-company-primary-color; + color: $o-default-report-primary-color; } table { border: 1px solid gray('700'); @@ -41,7 +41,7 @@ tr th { text-transform: uppercase; border: 1px solid gray('700'); - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; } } tbody { @@ -59,12 +59,12 @@ border-bottom: 1px solid gray('700'); } &.o_line_section td { - background-color: rgba($o-company-primary-color, 0.7); + background-color: rgba($o-default-report-primary-color, 0.7); color: #fff; } &.is-subtotal, td.o_price_total { - background-color: rgba($o-company-secondary-color, 0.1); + background-color: rgba($o-default-report-secondary-color, 0.1); } } } @@ -92,7 +92,7 @@ } &:last-child td, &.o_total td { - background-color: rgba($o-company-primary-color, 0.9); + background-color: rgba($o-default-report-primary-color, 0.9); color: #fff; &:first-child { diff --git a/addons/web/static/src/scss/layout_clean.scss b/addons/web/static/src/scss/layout_clean.scss index 0a62584ea88..5d559726281 100644 --- a/addons/web/static/src/scss/layout_clean.scss +++ b/addons/web/static/src/scss/layout_clean.scss @@ -8,14 +8,14 @@ .o_clean_footer { margin: 0 3px; margin-top: 200px; - border-top: 3px solid $o-company-secondary-color; + border-top: 3px solid $o-default-report-secondary-color; h4 { - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; font-weight: bolder; } .pagenumber { - border: 3px solid $o-company-primary-color; - background-color: $o-company-secondary-color; + border: 3px solid $o-default-report-primary-color; + background-color: $o-default-report-secondary-color; color: white; padding: 4px 8px; text-align: center; @@ -23,11 +23,11 @@ } .o_report_layout_clean { h1, h2, h3 { - color: $o-company-primary-color; + color: $o-default-report-primary-color; font-weight: bolder; } strong { - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; } table { &.o_main_table { @@ -35,9 +35,9 @@ } thead { - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; tr th { - border-top: 3px solid $o-company-secondary-color !important; + border-top: 3px solid $o-default-report-secondary-color !important; text-transform: uppercase; } tr th:first-child { @@ -50,7 +50,7 @@ border-top: none; } tr:last-child td { - border-bottom: 3px solid $o-company-secondary-color; + border-bottom: 3px solid $o-default-report-secondary-color; } tr { td { @@ -63,7 +63,7 @@ } #total { strong { - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; } } /*Total table*/ diff --git a/addons/web/static/src/scss/layout_standard.scss b/addons/web/static/src/scss/layout_standard.scss index 5cedf578c48..acd52b32bd4 100644 --- a/addons/web/static/src/scss/layout_standard.scss +++ b/addons/web/static/src/scss/layout_standard.scss @@ -7,19 +7,19 @@ .o_report_layout_standard { h2 { - color: $o-company-primary-color; + color: $o-default-report-primary-color; } #informations strong { - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; } #total strong{ - color: $o-company-primary-color; + color: $o-default-report-primary-color; } table { thead { - color: $o-company-secondary-color; + color: $o-default-report-secondary-color; } } } diff --git a/addons/web/static/src/scss/report.scss b/addons/web/static/src/scss/report.scss index f6fcdeb9407..86c0f986006 100644 --- a/addons/web/static/src/scss/report.scss +++ b/addons/web/static/src/scss/report.scss @@ -1,6 +1,6 @@ -$o-company-font: 'Lato' !default; -$o-company-primary-color: rgb(0, 0, 0) !default; -$o-company-secondary-color: rgb(0, 0, 0) !default; +$o-default-report-font: 'Lato' !default; +$o-default-report-primary-color: rgb(0, 0, 0) !default; +$o-default-report-secondary-color: rgb(0, 0, 0) !default; html, body { height: 100%; @@ -10,7 +10,7 @@ html, body { body { color: #000 !important; word-wrap: break-word; - font-family: $o-company-font; + font-family: $o-default-report-font; } .border-black td, .border-black th { border-top: 1px solid black !important; diff --git a/addons/web/views/report_templates.xml b/addons/web/views/report_templates.xml index 631330238ae..4f6f6086c7a 100644 --- a/addons/web/views/report_templates.xml +++ b/addons/web/views/report_templates.xml @@ -19,13 +19,13 @@ - +