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.

58 lines
2.0 KiB

  1. import re
  2. import uuid
  3. import base64
  4. from odoo import models, fields, api
  5. from odoo.modules import module
  6. class ScssEditor(models.AbstractModel):
  7. _inherit = 'web_editor.assets'
  8. # ----------------------------------------------------------
  9. # Helper
  10. # ----------------------------------------------------------
  11. def _get_theme_variable(self, content, variable):
  12. regex = r'{0}\:?\s(.*?);'.format(variable)
  13. value = re.search(regex, content)
  14. return value and value.group(1)
  15. def _get_theme_variables(self, content, variables):
  16. return {var: self._get_theme_variable(content, var) for var in variables}
  17. def _replace_theme_variables(self, content, variables):
  18. for variable in variables:
  19. variable_content = '{0}: {1};'.format(
  20. variable['name'],
  21. variable['value']
  22. )
  23. regex = r'{0}\:?\s(.*?);'.format(variable['name'])
  24. content = re.sub(regex, variable_content, content)
  25. return content
  26. @api.model
  27. def _save_asset_hook(self):
  28. res = super()._save_asset_hook()
  29. if self.env.context.get('theme_variables', False):
  30. res['website_id'] = False
  31. return res
  32. # ----------------------------------------------------------
  33. # Functions
  34. # ----------------------------------------------------------
  35. def get_theme_variables_values(self, url, bundle, variables):
  36. custom_url = self._make_custom_asset_url(url, bundle)
  37. content = self._get_content_from_url(custom_url)
  38. if not content:
  39. content = self._get_content_from_url(url)
  40. return self._get_theme_variables(content.decode('utf-8'), variables)
  41. def replace_theme_variables_values(self, url, bundle, variables):
  42. original = self._get_content_from_url(url).decode('utf-8')
  43. content = self._replace_theme_variables(original, variables)
  44. self.with_context(theme_variables=True).save_asset(
  45. url, bundle, content, 'scss'
  46. )