[MOV] web_editor: move web_editor.assets main methods at the top

Part-of: odoo/odoo#85392
This commit is contained in:
qsm-odoo
2022-02-28 12:56:04 +00:00
parent 2a13a4088b
commit aec6925b17
+79 -79
View File
@@ -19,6 +19,85 @@ class Assets(models.AbstractModel):
_name = 'web_editor.assets'
_description = 'Assets Utils'
def reset_asset(self, url, bundle):
"""
Delete the potential customizations made to a given (original) asset.
Params:
url (str): the URL of the original asset (scss / js) file
bundle (str):
the name of the bundle in which the customizations to delete
were made
"""
custom_url = self.make_custom_asset_file_url(url, bundle)
# Simply delete the attachement which contains the modified scss/js file
# and the xpath view which links it
self._get_custom_attachment(custom_url).unlink()
self._get_custom_asset(custom_url).unlink()
def save_asset(self, url, bundle, content, file_type):
"""
Customize the content of a given asset (scss / js).
Params:
url (src):
the URL of the original asset to customize (whether or not the
asset was already customized)
bundle (src):
the name of the bundle in which the customizations will take
effect
content (src): the new content of the asset (scss / js)
file_type (src):
either 'scss' or 'js' according to the file being customized
"""
custom_url = self.make_custom_asset_file_url(url, bundle)
datas = base64.b64encode((content or "\n").encode("utf-8"))
# Check if the file to save had already been modified
custom_attachment = self._get_custom_attachment(custom_url)
if custom_attachment:
# If it was already modified, simply override the corresponding
# attachment content
custom_attachment.write({"datas": datas})
else:
# If not, create a new attachment to copy the original scss/js file
# content, with its modifications
new_attach = {
'name': url.split("/")[-1],
'type': "binary",
'mimetype': (file_type == 'js' and 'text/javascript' or 'text/scss'),
'datas': datas,
'url': custom_url,
}
new_attach.update(self._save_asset_hook())
self.env["ir.attachment"].create(new_attach)
# Create an asset with the new attachment
IrAsset = self.env['ir.asset']
new_asset = {
'path': custom_url,
'target': url,
'directive': 'replace',
**self._save_asset_hook(),
}
target_asset = self._get_custom_asset(url)
if target_asset:
new_asset['name'] = target_asset.name + ' override'
new_asset['bundle'] = target_asset.bundle
new_asset['sequence'] = target_asset.sequence
else:
path_parts = '/'.join(os.path.split(custom_url)).split('/')
new_asset['name'] = '%s: replace %s' % (bundle, path_parts[-1])
new_asset['bundle'] = IrAsset._get_related_bundle(url, bundle)
IrAsset.create(new_asset)
self.env["ir.qweb"].clear_caches()
def get_all_custom_attachments(self, urls):
"""
Fetch all the ir.attachment records related to given URLs.
@@ -118,85 +197,6 @@ class Assets(models.AbstractModel):
parts = url.rsplit(".", 1)
return "%s.custom.%s.%s" % (parts[0], bundle_xmlid, parts[1])
def reset_asset(self, url, bundle):
"""
Delete the potential customizations made to a given (original) asset.
Params:
url (str): the URL of the original asset (scss / js) file
bundle (str):
the name of the bundle in which the customizations to delete
were made
"""
custom_url = self.make_custom_asset_file_url(url, bundle)
# Simply delete the attachement which contains the modified scss/js file
# and the xpath view which links it
self._get_custom_attachment(custom_url).unlink()
self._get_custom_asset(custom_url).unlink()
def save_asset(self, url, bundle, content, file_type):
"""
Customize the content of a given asset (scss / js).
Params:
url (src):
the URL of the original asset to customize (whether or not the
asset was already customized)
bundle (src):
the name of the bundle in which the customizations will take
effect
content (src): the new content of the asset (scss / js)
file_type (src):
either 'scss' or 'js' according to the file being customized
"""
custom_url = self.make_custom_asset_file_url(url, bundle)
datas = base64.b64encode((content or "\n").encode("utf-8"))
# Check if the file to save had already been modified
custom_attachment = self._get_custom_attachment(custom_url)
if custom_attachment:
# If it was already modified, simply override the corresponding
# attachment content
custom_attachment.write({"datas": datas})
else:
# If not, create a new attachment to copy the original scss/js file
# content, with its modifications
new_attach = {
'name': url.split("/")[-1],
'type': "binary",
'mimetype': (file_type == 'js' and 'text/javascript' or 'text/scss'),
'datas': datas,
'url': custom_url,
}
new_attach.update(self._save_asset_hook())
self.env["ir.attachment"].create(new_attach)
# Create an asset with the new attachment
IrAsset = self.env['ir.asset']
new_asset = {
'path': custom_url,
'target': url,
'directive': 'replace',
**self._save_asset_hook(),
}
target_asset = self._get_custom_asset(url)
if target_asset:
new_asset['name'] = target_asset.name + ' override'
new_asset['bundle'] = target_asset.bundle
new_asset['sequence'] = target_asset.sequence
else:
path_parts = '/'.join(os.path.split(custom_url)).split('/')
new_asset['name'] = '%s: replace %s' % (bundle, path_parts[-1])
new_asset['bundle'] = IrAsset._get_related_bundle(url, bundle)
IrAsset.create(new_asset)
self.env["ir.qweb"].clear_caches()
def _get_custom_attachment(self, custom_url, op='='):
"""
Fetch the ir.attachment record related to the given customized asset.