diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 17:14:58 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 17:14:58 +0700 |
| commit | 1ca3b3df3421961caec3b747a364071c80f5c7da (patch) | |
| tree | 6778a1f0f3f9b4c6e26d6d87ccde16e24da6c9d6 /muk_web_theme/models | |
| parent | b57188be371d36d96caac4b8d65a40745c0e972c (diff) | |
initial commit
Diffstat (limited to 'muk_web_theme/models')
| -rw-r--r-- | muk_web_theme/models/__init__.py | 27 | ||||
| -rw-r--r-- | muk_web_theme/models/ir_http.py | 36 | ||||
| -rw-r--r-- | muk_web_theme/models/res_company.py | 79 | ||||
| -rw-r--r-- | muk_web_theme/models/res_config_settings.py | 154 | ||||
| -rw-r--r-- | muk_web_theme/models/res_users.py | 80 | ||||
| -rw-r--r-- | muk_web_theme/models/scss_editor.py | 146 |
6 files changed, 522 insertions, 0 deletions
diff --git a/muk_web_theme/models/__init__.py b/muk_web_theme/models/__init__.py new file mode 100644 index 0000000..6d18a30 --- /dev/null +++ b/muk_web_theme/models/__init__.py @@ -0,0 +1,27 @@ +################################################################################### +# +# Copyright (c) 2017-today MuK IT GmbH. +# +# This file is part of MuK Grid Snippets +# (see https://mukit.at). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +################################################################################### + +from . import res_company +from . import res_users +from . import ir_http +from . import scss_editor +from . import res_config_settings
\ No newline at end of file diff --git a/muk_web_theme/models/ir_http.py b/muk_web_theme/models/ir_http.py new file mode 100644 index 0000000..4671619 --- /dev/null +++ b/muk_web_theme/models/ir_http.py @@ -0,0 +1,36 @@ +################################################################################### +# +# Copyright (c) 2017-today MuK IT GmbH. +# +# This file is part of MuK Grid Snippets +# (see https://mukit.at). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +################################################################################### + +from odoo import models +from odoo.http import request + + +class IrHttp(models.AbstractModel): + + _inherit = 'ir.http' + + def session_info(self): + result = super(IrHttp, self).session_info() + params = request.env['ir.config_parameter'].sudo() + blend_mode = params.get_param('muk_web_theme.background_blend_mode') + result.update(muk_web_theme_background_blend_mode=blend_mode or 'normal') + return result diff --git a/muk_web_theme/models/res_company.py b/muk_web_theme/models/res_company.py new file mode 100644 index 0000000..3b0ecf5 --- /dev/null +++ b/muk_web_theme/models/res_company.py @@ -0,0 +1,79 @@ +################################################################################### +# +# Copyright (c) 2017-today MuK IT GmbH. +# +# This file is part of MuK Grid Snippets +# (see https://mukit.at). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +################################################################################### + +from odoo import models, fields + + +class ResCompany(models.Model): + + _inherit = 'res.company' + + #---------------------------------------------------------- + # Database + #---------------------------------------------------------- + + background_image = fields.Binary( + string="Apps Menu Background Image", + attachment=True + ) + + background_blend_mode = fields.Selection( + selection=[ + ('normal', 'Normal'), + ('multiply', 'Multiply'), + ('screen', 'Screen'), + ('overlay', 'Overlay'), + ('hard-light', 'Hard-light'), + ('darken', 'Darken'), + ('lighten', 'Lighten'), + ('color-dodge', 'Color-dodge'), + ('color-burn', 'Color-burn'), + ('hard-light', 'Hard-light'), + ('difference', 'Difference'), + ('exclusion', 'Exclusion'), + ('hue', 'Hue'), + ('saturation', 'Saturation'), + ('color', 'Color'), + ('luminosity', 'Luminosity'), + ], + string="Apps Menu Background Blend Mode", + default='normal' + ) + + default_sidebar_preference = fields.Selection( + selection=[ + ('invisible', 'Invisible'), + ('small', 'Small'), + ('large', 'Large') + ], + string="Sidebar Type", + default='small' + ) + + default_chatter_preference = fields.Selection( + selection=[ + ('normal', 'Normal'), + ('sided', 'Sided'), + ], + string="Chatter Position", + default='sided' + ) diff --git a/muk_web_theme/models/res_config_settings.py b/muk_web_theme/models/res_config_settings.py new file mode 100644 index 0000000..9d77e24 --- /dev/null +++ b/muk_web_theme/models/res_config_settings.py @@ -0,0 +1,154 @@ +###################################################################################
+#
+# Copyright (c) 2017-today MuK IT GmbH.
+#
+# This file is part of MuK Grid Snippets
+# (see https://mukit.at).
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+###################################################################################
+
+import re
+import uuid
+import base64
+
+from odoo import api, fields, models
+
+XML_ID = "muk_web_theme._assets_primary_variables"
+SCSS_URL = "/muk_web_theme/static/src/scss/colors.scss"
+
+
+class ResConfigSettings(models.TransientModel):
+
+ _inherit = 'res.config.settings'
+
+ #----------------------------------------------------------
+ # Database
+ #----------------------------------------------------------
+
+ theme_favicon = fields.Binary(
+ related="company_id.favicon",
+ readonly=False
+ )
+
+ theme_background_image = fields.Binary(
+ related="company_id.background_image",
+ readonly=False
+ )
+
+ theme_background_blend_mode = fields.Selection(
+ related="company_id.background_blend_mode",
+ readonly=False
+ )
+
+ theme_default_sidebar_preference = fields.Selection(
+ related="company_id.default_sidebar_preference",
+ readonly=False
+ )
+
+ theme_default_chatter_preference = fields.Selection(
+ related="company_id.default_chatter_preference",
+ readonly=False
+ )
+
+ theme_color_brand = fields.Char(
+ string="Theme Brand Color"
+ )
+
+ theme_color_primary = fields.Char(
+ string="Theme Primary Color"
+ )
+
+ theme_color_required = fields.Char(
+ string="Theme Required Color"
+ )
+
+ theme_color_menu = fields.Char(
+ string="Theme Menu Color"
+ )
+
+ theme_color_appbar_color = fields.Char(
+ string="Theme AppBar Color"
+ )
+
+ theme_color_appbar_background = fields.Char(
+ string="Theme AppBar Background"
+ )
+
+ #----------------------------------------------------------
+ # Functions
+ #----------------------------------------------------------
+
+ def set_values(self):
+ res = super(ResConfigSettings, self).set_values()
+ param = self.env['ir.config_parameter'].sudo()
+ variables = [
+ 'o-brand-odoo',
+ 'o-brand-primary',
+ 'mk-required-color',
+ 'mk-apps-color',
+ 'mk-appbar-color',
+ 'mk-appbar-background',
+ ]
+ colors = self.env['muk_web_theme.scss_editor'].get_values(
+ SCSS_URL, XML_ID, variables
+ )
+ colors_changed = []
+ colors_changed.append(self.theme_color_brand != colors['o-brand-odoo'])
+ colors_changed.append(self.theme_color_primary != colors['o-brand-primary'])
+ colors_changed.append(self.theme_color_required != colors['mk-required-color'])
+ colors_changed.append(self.theme_color_menu != colors['mk-apps-color'])
+ colors_changed.append(self.theme_color_appbar_color != colors['mk-appbar-color'])
+ colors_changed.append(self.theme_color_appbar_background != colors['mk-appbar-background'])
+ if(any(colors_changed)):
+ variables = [
+ {'name': 'o-brand-odoo', 'value': self.theme_color_brand or "#243742"},
+ {'name': 'o-brand-primary', 'value': self.theme_color_primary or "#5D8DA8"},
+ {'name': 'mk-required-color', 'value': self.theme_color_required or "#d1dfe6"},
+ {'name': 'mk-apps-color', 'value': self.theme_color_menu or "#f8f9fa"},
+ {'name': 'mk-appbar-color', 'value': self.theme_color_appbar_color or "#dee2e6"},
+ {'name': 'mk-appbar-background', 'value': self.theme_color_appbar_background or "#000000"},
+ ]
+ self.env['muk_web_theme.scss_editor'].replace_values(
+ SCSS_URL, XML_ID, variables
+ )
+ param.set_param('muk_web_theme.background_blend_mode', self.theme_background_blend_mode)
+ return res
+
+ @api.model
+ def get_values(self):
+ res = super(ResConfigSettings, self).get_values()
+ params = self.env['ir.config_parameter'].sudo()
+ variables = [
+ 'o-brand-odoo',
+ 'o-brand-primary',
+ 'mk-required-color',
+ 'mk-apps-color',
+ 'mk-appbar-color',
+ 'mk-appbar-background',
+ ]
+ colors = self.env['muk_web_theme.scss_editor'].get_values(
+ SCSS_URL, XML_ID, variables
+ )
+ res.update({
+ 'theme_color_brand': colors['o-brand-odoo'],
+ 'theme_color_primary': colors['o-brand-primary'],
+ 'theme_color_required': colors['mk-required-color'],
+ 'theme_color_menu': colors['mk-apps-color'],
+ 'theme_color_appbar_color': colors['mk-appbar-color'],
+ 'theme_color_appbar_background': colors['mk-appbar-background'],
+ 'theme_background_blend_mode': params.get_param('muk_web_theme.background_blend_mode', 'normal'),
+ })
+ return res
\ No newline at end of file diff --git a/muk_web_theme/models/res_users.py b/muk_web_theme/models/res_users.py new file mode 100644 index 0000000..eb8c2f6 --- /dev/null +++ b/muk_web_theme/models/res_users.py @@ -0,0 +1,80 @@ +################################################################################### +# +# Copyright (c) 2017-today MuK IT GmbH. +# +# This file is part of MuK Grid Snippets +# (see https://mukit.at). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +################################################################################### + +from odoo import models, fields, api + + +class ResUsers(models.Model): + + _inherit = 'res.users' + + #---------------------------------------------------------- + # Defaults + #---------------------------------------------------------- + + @api.model + def _default_sidebar_type(self): + return self.env.user.company_id.default_sidebar_preference or 'small' + + @api.model + def _default_chatter_position(self): + return self.env.user.company_id.default_chatter_preference or 'sided' + + #---------------------------------------------------------- + # Database + #---------------------------------------------------------- + + sidebar_type = fields.Selection( + selection=[ + ('invisible', 'Invisible'), + ('small', 'Small'), + ('large', 'Large') + ], + required=True, + string="Sidebar Type", + default=lambda self: self._default_sidebar_type() + ) + + chatter_position = fields.Selection( + selection=[ + ('normal', 'Normal'), + ('sided', 'Sided'), + ], + required=True, + string="Chatter Position", + default=lambda self: self._default_chatter_position() + ) + + #---------------------------------------------------------- + # Setup + #---------------------------------------------------------- + + def __init__(self, pool, cr): + init_res = super(ResUsers, self).__init__(pool, cr) + theme_fields = ['sidebar_type', 'chatter_position'] + readable_fields = list(self.SELF_READABLE_FIELDS) + writeable_fields = list(self.SELF_WRITEABLE_FIELDS) + readable_fields.extend(theme_fields) + writeable_fields.extend(theme_fields) + type(self).SELF_READABLE_FIELDS = readable_fields + type(self).SELF_WRITEABLE_FIELDS = writeable_fields + return init_res diff --git a/muk_web_theme/models/scss_editor.py b/muk_web_theme/models/scss_editor.py new file mode 100644 index 0000000..0665856 --- /dev/null +++ b/muk_web_theme/models/scss_editor.py @@ -0,0 +1,146 @@ +###################################################################################
+#
+# Copyright (c) 2017-today MuK IT GmbH.
+#
+# This file is part of MuK Grid Snippets
+# (see https://mukit.at).
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+###################################################################################
+
+import re
+import uuid
+import base64
+
+from odoo import models, fields, api
+from odoo.modules import module
+
+
+class ScssEditor(models.AbstractModel):
+ _name = 'muk_web_theme.scss_editor'
+ _description = 'Scss Editor'
+
+ # ----------------------------------------------------------
+ # Helper
+ # ----------------------------------------------------------
+
+ def _build_custom_url(self, url_parts, xmlid):
+ return "%s.custom.%s.%s" % (url_parts[0], xmlid, url_parts[1])
+
+ def _get_custom_url(self, url, xmlid):
+ return self._build_custom_url(url.rsplit(".", 1), xmlid)
+
+ def _get_custom_attachment(self, url):
+ return self.env["ir.attachment"].with_context(
+ bin_size=False, bin_size_datas=False
+ ).search([("url", '=', url)], limit=1)
+
+ def _get_custom_view(self, url):
+ return self.env["ir.ui.view"].search([("name", '=', url)])
+
+ def _get_variable(self, content, variable):
+ regex = r'{0}\:?\s(.*?);'.format(variable)
+ value = re.search(regex, content)
+ return value and value.group(1)
+
+ def _get_variables(self, content, variables):
+ return {var: self._get_variable(content, var) for var in variables}
+
+ def _replace_variables(self, content, variables):
+ for variable in variables:
+ variable_content = '{0}: {1};'.format(
+ variable['name'],
+ variable['value']
+ )
+ regex = r'{0}\:?\s(.*?);'.format(variable['name'])
+ content = re.sub(regex, variable_content, content)
+ return content
+
+ # ----------------------------------------------------------
+ # Read
+ # ----------------------------------------------------------
+
+ def get_content(self, url, xmlid):
+ custom_url = self._get_custom_url(url, xmlid)
+ custom_attachment = self._get_custom_attachment(custom_url)
+ if custom_attachment.exists():
+ return base64.b64decode(custom_attachment.datas).decode('utf-8')
+ else:
+ match = re.compile("^/(\w+)/(.+?)(\.custom\.(.+))?\.(\w+)$").match(url)
+ module_path = module.get_module_path(match.group(1))
+ resource_path = "%s.%s" % (match.group(2), match.group(5))
+ module_resource_path = module.get_resource_path(module_path, resource_path)
+ with open(module_resource_path, "rb") as file:
+ return file.read().decode('utf-8')
+
+ def get_values(self, url, xmlid, variables):
+ return self._get_variables(self.get_content(url, xmlid), variables)
+
+ # ----------------------------------------------------------
+ # Write
+ # ----------------------------------------------------------
+
+ def replace_content(self, url, xmlid, content):
+ custom_url = self._get_custom_url(url, xmlid)
+ custom_view = self._get_custom_view(custom_url)
+ custom_attachment = self._get_custom_attachment(custom_url)
+ datas = base64.b64encode((content or "\n").encode("utf-8"))
+ if custom_attachment.exists():
+ custom_attachment.write({"datas": datas})
+ else:
+ self.env["ir.attachment"].create({
+ 'name': custom_url,
+ 'type': "binary",
+ 'mimetype': "text/scss",
+ 'datas': datas,
+ # TODO: old field datas_fname got removed, check if store_fname is correct and write migration
+ 'store_fname': url.split("/")[-1],
+ 'url': custom_url,
+ })
+ if not custom_view.exists():
+ view_to_xpath = self.env["ir.ui.view"].get_related_views(
+ xmlid, bundles=True
+ ).filtered(lambda v: v.arch.find(url) >= 0)
+ self.env["ir.ui.view"].create({
+ 'name': custom_url,
+ 'key': 'web_editor.scss_%s' % str(uuid.uuid4())[:6],
+ 'mode': "extension",
+ 'priority': view_to_xpath.priority,
+ 'inherit_id': view_to_xpath.id,
+ 'arch': """
+ <data inherit_id="%(inherit_xml_id)s" name="%(name)s">
+ <xpath expr="//link[@href='%(url_to_replace)s']" position="attributes">
+ <attribute name="href">%(new_url)s</attribute>
+ </xpath>
+ </data>
+ """ % {
+ 'inherit_xml_id': view_to_xpath.xml_id,
+ 'name': custom_url,
+ 'url_to_replace': url,
+ 'new_url': custom_url,
+ }
+ })
+ self.env["ir.qweb"].clear_caches()
+
+ def replace_values(self, url, xmlid, variables):
+ content = self._replace_variables(
+ self.get_content(url, xmlid), variables
+ )
+ self.replace_content(url, xmlid, content)
+
+ def reset_values(self, url, xmlid):
+ custom_url = self._get_custom_url(url, xmlid)
+ self._get_custom_attachment(custom_url).unlink()
+ self._get_custom_view(custom_url).unlink()
|
