|
| 1 | +# © 2024 initOS GmbH |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import re |
| 6 | + |
| 7 | +from odoo import models |
| 8 | +from odoo.http import ALLOWED_DEBUG_MODES |
| 9 | + |
| 10 | +_logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | +# Failsafe because it has the potential to lock everyone out of the UI |
| 13 | +ALLOWED_DEBUG_MODES.append("unfiltered") |
| 14 | + |
| 15 | + |
| 16 | +def filesize(filename): |
| 17 | + if not filename: |
| 18 | + return 0 |
| 19 | + |
| 20 | + if not os.path.isfile(filename): |
| 21 | + return 0 |
| 22 | + |
| 23 | + try: |
| 24 | + with open(filename, "rb") as fp: |
| 25 | + fp.seek(0, 2) |
| 26 | + return fp.tell() |
| 27 | + except Exception: |
| 28 | + return 0 |
| 29 | + |
| 30 | + |
| 31 | +class IrQweb(models.AbstractModel): |
| 32 | + _inherit = "ir.qweb" |
| 33 | + |
| 34 | + def _generate_asset_nodes( |
| 35 | + self, |
| 36 | + bundle, |
| 37 | + css=True, |
| 38 | + js=True, |
| 39 | + debug=False, |
| 40 | + async_load=False, |
| 41 | + defer_load=False, |
| 42 | + lazy_load=False, |
| 43 | + media=None, |
| 44 | + ): |
| 45 | + |
| 46 | + if "|" in bundle: |
| 47 | + bundle = bundle.rsplit("|", 1)[0] |
| 48 | + |
| 49 | + return super()._generate_asset_nodes( |
| 50 | + bundle, |
| 51 | + css=css, |
| 52 | + js=js, |
| 53 | + debug=debug, |
| 54 | + async_load=async_load, |
| 55 | + defer_load=defer_load, |
| 56 | + lazy_load=lazy_load, |
| 57 | + media=media, |
| 58 | + ) |
| 59 | + |
| 60 | + def get_asset_bundle(self, bundle_name, files, env=None, css=True, js=True): |
| 61 | + if "|" in bundle_name: |
| 62 | + bundle_name = bundle_name.rsplit("|", 1)[0] |
| 63 | + |
| 64 | + hashsum = self.env.context.get("bundle_hashsum") |
| 65 | + skip = self.env.context.get("bundle_skip_filtering") |
| 66 | + if not hashsum or skip: |
| 67 | + return super().get_asset_bundle(bundle_name, files, env=env, css=css, js=js) |
| 68 | + |
| 69 | + urls = {a["url"]: a for a in files} |
| 70 | + |
| 71 | + # Synchronize assets files |
| 72 | + assets = self.env["web.assets"].sudo().search([("bundle", "=", bundle_name)]) |
| 73 | + assets.mapped("file_ids").filtered_domain( |
| 74 | + [("name", "not in", list(urls))] |
| 75 | + ).unlink() |
| 76 | + |
| 77 | + asset_files = assets.mapped("file_ids") |
| 78 | + for url, data in urls.items(): |
| 79 | + asset_files.filtered_domain([("name", "=", url)]).write( |
| 80 | + {"size": filesize(data.get("filename", ""))} |
| 81 | + ) |
| 82 | + |
| 83 | + for asset in assets: |
| 84 | + for file in set(urls) - set(asset.mapped("file_ids.name")): |
| 85 | + data = urls[file] |
| 86 | + |
| 87 | + asset.file_ids.create( |
| 88 | + { |
| 89 | + "asset_id": asset.id, |
| 90 | + "name": file, |
| 91 | + "mimetype": data["atype"], |
| 92 | + "include": True, |
| 93 | + "size": filesize(data.get("filename", "")), |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + # Filter the assets files |
| 98 | + domain = [ |
| 99 | + ("bundle", "=", bundle_name), |
| 100 | + ("hashsum", "=", hashsum), |
| 101 | + ("active", "=", True), |
| 102 | + ] |
| 103 | + assets = assets.search(domain, limit=1) |
| 104 | + names = set(assets.mapped("file_ids").filtered("include").mapped("name")) |
| 105 | + return super().get_asset_bundle( |
| 106 | + bundle_name, |
| 107 | + [file for file in files if file["url"] in names], |
| 108 | + env=env, |
| 109 | + css=css, |
| 110 | + js=js, |
| 111 | + ) |
| 112 | + |
| 113 | + def _get_asset_nodes( |
| 114 | + self, |
| 115 | + bundle, |
| 116 | + css=True, |
| 117 | + js=True, |
| 118 | + debug=False, |
| 119 | + async_load=False, |
| 120 | + defer_load=False, |
| 121 | + lazy_load=False, |
| 122 | + media=None, |
| 123 | + ): |
| 124 | + path = None |
| 125 | + assets = self.env["web.assets"].sudo() |
| 126 | + try: |
| 127 | + from odoo.http import request |
| 128 | + |
| 129 | + path = request.httprequest.path |
| 130 | + except RuntimeError: # pylint: disable=except-pass |
| 131 | + pass |
| 132 | + |
| 133 | + if path: |
| 134 | + domain = [ |
| 135 | + ("bundle", "=", bundle), |
| 136 | + ("active", "=", True), |
| 137 | + ("path_regex", "!=", False), |
| 138 | + ] |
| 139 | + for rec in assets.search(domain): |
| 140 | + pattern = r"^" + rec.path_regex.strip(r"^") |
| 141 | + if re.match(pattern, path): |
| 142 | + assets = rec |
| 143 | + break |
| 144 | + |
| 145 | + if assets: |
| 146 | + bundle += f"|{assets.hashsum}" |
| 147 | + |
| 148 | + return super( |
| 149 | + IrQweb, |
| 150 | + self.with_context( |
| 151 | + bundle_hashsum=assets.hashsum, |
| 152 | + bundle_skip_filtering=debug and "unfiltered" in debug, |
| 153 | + ), |
| 154 | + )._get_asset_nodes( |
| 155 | + bundle, |
| 156 | + css=css, |
| 157 | + js=js, |
| 158 | + debug=debug, |
| 159 | + async_load=async_load, |
| 160 | + defer_load=defer_load, |
| 161 | + lazy_load=lazy_load, |
| 162 | + media=media, |
| 163 | + ) |
0 commit comments