1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4import re
5
6from odoo import models
7
8
9class Assets(models.AbstractModel):
10    _inherit = 'web_editor.assets'
11
12    def make_scss_customization(self, url, values):
13        """
14        Makes a scss customization of the given file. That file must
15        contain a scss map including a line comment containing the word 'hook',
16        to indicate the location where to write the new key,value pairs.
17
18        Params:
19            url (str):
20                the URL of the scss file to customize (supposed to be a variable
21                file which will appear in the assets_common bundle)
22
23            values (dict):
24                key,value mapping to integrate in the file's map (containing the
25                word hook). If a key is already in the file's map, its value is
26                overridden.
27        """
28        if 'color-palettes-number' in values:
29            self.reset_asset('/website/static/src/scss/options/colors/user_color_palette.scss', 'web.assets_common')
30            # Do not reset all theme colors for compatibility (not removing alpha -> epsilon colors)
31            self.make_scss_customization('/website/static/src/scss/options/colors/user_theme_color_palette.scss', {
32                'success': 'null',
33                'info': 'null',
34                'warning': 'null',
35                'danger': 'null',
36            })
37
38        custom_url = self.make_custom_asset_file_url(url, 'web.assets_common')
39        updatedFileContent = self.get_asset_content(custom_url) or self.get_asset_content(url)
40        updatedFileContent = updatedFileContent.decode('utf-8')
41        for name, value in values.items():
42            pattern = "'%s': %%s,\n" % name
43            regex = re.compile(pattern % ".+")
44            replacement = pattern % value
45            if regex.search(updatedFileContent):
46                updatedFileContent = re.sub(regex, replacement, updatedFileContent)
47            else:
48                updatedFileContent = re.sub(r'( *)(.*hook.*)', r'\1%s\1\2' % replacement, updatedFileContent)
49
50        # Bundle is 'assets_common' as this route is only meant to update
51        # variables scss files
52        self.save_asset(url, 'web.assets_common', updatedFileContent, 'scss')
53
54    def _get_custom_attachment(self, custom_url, op='='):
55        """
56        See web_editor.Assets._get_custom_attachment
57        Extend to only return the attachments related to the current website.
58        """
59        website = self.env['website'].get_current_website()
60        res = super(Assets, self)._get_custom_attachment(custom_url, op=op)
61        return res.with_context(website_id=website.id).filtered(lambda x: not x.website_id or x.website_id == website)
62
63    def _get_custom_view(self, custom_url, op='='):
64        """
65        See web_editor.Assets._get_custom_view
66        Extend to only return the views related to the current website.
67        """
68        website = self.env['website'].get_current_website()
69        res = super(Assets, self)._get_custom_view(custom_url, op=op)
70        return res.with_context(website_id=website.id).filter_duplicate()
71
72    def _save_asset_attachment_hook(self):
73        """
74        See web_editor.Assets._save_asset_attachment_hook
75        Extend to add website ID at attachment creation.
76        """
77        res = super(Assets, self)._save_asset_attachment_hook()
78
79        website = self.env['website'].get_current_website()
80        if website:
81            res['website_id'] = website.id
82        return res
83
84    def _save_asset_view_hook(self):
85        """
86        See web_editor.Assets._save_asset_view_hook
87        Extend to add website ID at view creation.
88        """
89        res = super(Assets, self)._save_asset_view_hook()
90
91        website = self.env['website'].get_current_website()
92        if website:
93            res['website_id'] = website.id
94        return res
95