You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
4.1 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-today MuK IT GmbH.
  4. #
  5. # This file is part of MuK Backend Theme
  6. # (see https://mukit.at).
  7. #
  8. # MuK Proprietary License v1.0
  9. #
  10. # This software and associated files (the "Software") may only be used
  11. # (executed, modified, executed after modifications) if you have
  12. # purchased a valid license from MuK IT GmbH.
  13. #
  14. # The above permissions are granted for a single database per purchased
  15. # license. Furthermore, with a valid license it is permitted to use the
  16. # software on other databases as long as the usage is limited to a testing
  17. # or development environment.
  18. #
  19. # You may develop modules based on the Software or that use the Software
  20. # as a library (typically by depending on it, importing it and using its
  21. # resources), but without copying any source code or material from the
  22. # Software. You may distribute those modules under the license of your
  23. # choice, provided that this license is compatible with the terms of the
  24. # MuK Proprietary License (For example: LGPL, MIT, or proprietary licenses
  25. # similar to this one).
  26. #
  27. # It is forbidden to publish, distribute, sublicense, or sell copies of
  28. # the Software or modified copies of the Software.
  29. #
  30. # The above copyright notice and this permission notice must be included
  31. # in all copies or substantial portions of the Software.
  32. #
  33. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  34. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  35. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  36. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  37. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  38. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  39. # DEALINGS IN THE SOFTWARE.
  40. #
  41. ###################################################################################
  42. import re
  43. import uuid
  44. import base64
  45. from odoo import models, fields, api
  46. from odoo.modules import module
  47. class ScssEditor(models.AbstractModel):
  48. _inherit = 'web_editor.assets'
  49. # ----------------------------------------------------------
  50. # Helper
  51. # ----------------------------------------------------------
  52. def _get_theme_variable(self, content, variable):
  53. regex = r'{0}\:?\s(.*?);'.format(variable)
  54. value = re.search(regex, content)
  55. return value and value.group(1)
  56. def _get_theme_variables(self, content, variables):
  57. return {var: self._get_theme_variable(content, var) for var in variables}
  58. def _replace_theme_variables(self, content, variables):
  59. for variable in variables:
  60. variable_content = '{0}: {1};'.format(
  61. variable['name'],
  62. variable['value']
  63. )
  64. regex = r'{0}\:?\s(.*?);'.format(variable['name'])
  65. content = re.sub(regex, variable_content, content)
  66. return content
  67. @api.model
  68. def _save_asset_hook(self):
  69. res = super()._save_asset_hook()
  70. if (
  71. res.get('website_id', False) and
  72. self.env.context.get('theme_variables', False)
  73. ):
  74. res['website_id'] = False
  75. return res
  76. # ----------------------------------------------------------
  77. # Functions
  78. # ----------------------------------------------------------
  79. def get_theme_variables_values(self, url, bundle, variables):
  80. custom_url = self._make_custom_asset_url(url, bundle)
  81. content = self._get_content_from_url(custom_url)
  82. if not content:
  83. content = self._get_content_from_url(url)
  84. return self._get_theme_variables(content.decode('utf-8'), variables)
  85. def replace_theme_variables_values(self, url, bundle, variables):
  86. original = self._get_content_from_url(url).decode('utf-8')
  87. content = self._replace_theme_variables(original, variables)
  88. self.with_context(theme_variables=True).save_asset(
  89. url, bundle, content, 'scss'
  90. )