Files
odoo_source/addons/website/models/ir_asset.py
T
8cc066173d [IMP] *: Improve assets management
This commit changes the way assets are declared in Odoo modules.

Before: assets were declared in template files. Template bundles were
generated from primary templates, so technically any qweb template could
have been called as an asset bundle, with the 't-call-assets' directive.

Being standard qweb templates, they had access to standard HTML tags
(script, link, with or without raw scripts or style definition), qweb
directives (t-call, t-raw, etc.) and could be inherited by other
templates.

Now: assets are defined in the module's manifest and generated by the
't-call-assets' directive.

More information on the new system can be found on the updated user
documentation (see the "JavaScript Reference" section).

Task: 2352566

Co-authored-by: Bruno Boi <boi@odoo.com>
Co-authored-by: Julien Mougenot <jum@odoo.com>
Co-authored-by: Lucas Perais <lpe@odoo.com>
Co-authored-by: Mathieu Duckerts-Antoine <dam@odoo.com>
Co-authored-by: Raphael Collet <rco@odoo.com>
Co-authored-by: Simon Genin <ges@odoo.com>
2021-03-31 13:57:17 +02:00

61 lines
2.6 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
class IrAsset(models.Model):
_inherit = 'ir.asset'
key = fields.Char(copy=False, help='Technical field used to resolve multiple assets in a multi-website environment.')
website_id = fields.Many2one('website')
def _get_related_assets(self, domain):
website = self.env['website'].get_current_website(fallback=False)
if website:
domain += website.website_domain()
assets = super()._get_related_assets(domain)
return assets.filter_duplicate()
def _get_active_addons_list(self):
"""Overridden to discard inactive themes."""
addons_list = super()._get_active_addons_list()
website = self.env['website'].get_current_website(fallback=False)
if not website:
return addons_list
IrModule = self.env['ir.module.module'].sudo()
# discard all theme modules except website.theme_id
themes = IrModule.search(IrModule.get_themes_domain()) - website.theme_id
to_remove = set(themes.mapped('name'))
return [name for name in addons_list if name not in to_remove]
def filter_duplicate(self):
""" Filter current recordset only keeping the most suitable asset per distinct name.
Every non-accessible asset will be removed from the set:
* In non website context, every asset with a website will be removed
* In a website context, every asset from another website
"""
current_website = self.env['website'].get_current_website(fallback=False)
if not current_website:
return self.filtered(lambda asset: not asset.website_id)
most_specific_assets = self.env['ir.asset']
for asset in self:
if asset.website_id == current_website:
# specific asset: add it if it's for the current website and ignore
# it if it's for another website
most_specific_assets += asset
elif not asset.website_id:
# no key: added either way
if not asset.key:
most_specific_assets += asset
# generic asset: add it iff for the current website, there is no
# specific asset for this asset (based on the same `key` attribute)
elif not any(asset.key == asset2.key and asset2.website_id == current_website for asset2 in self):
most_specific_assets += asset
return most_specific_assets