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.

146 lines
6.0 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-today MuK IT GmbH.
  4. #
  5. # This file is part of MuK Grid Snippets
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import re
  23. import uuid
  24. import base64
  25. from odoo import models, fields, api
  26. from odoo.modules import module
  27. class ScssEditor(models.AbstractModel):
  28. _name = 'muk_web_theme.scss_editor'
  29. _description = 'Scss Editor'
  30. # ----------------------------------------------------------
  31. # Helper
  32. # ----------------------------------------------------------
  33. def _build_custom_url(self, url_parts, xmlid):
  34. return "%s.custom.%s.%s" % (url_parts[0], xmlid, url_parts[1])
  35. def _get_custom_url(self, url, xmlid):
  36. return self._build_custom_url(url.rsplit(".", 1), xmlid)
  37. def _get_custom_attachment(self, url):
  38. return self.env["ir.attachment"].with_context(
  39. bin_size=False, bin_size_datas=False
  40. ).search([("url", '=', url)], limit=1)
  41. def _get_custom_view(self, url):
  42. return self.env["ir.ui.view"].search([("name", '=', url)])
  43. def _get_variable(self, content, variable):
  44. regex = r'{0}\:?\s(.*?);'.format(variable)
  45. value = re.search(regex, content)
  46. return value and value.group(1)
  47. def _get_variables(self, content, variables):
  48. return {var: self._get_variable(content, var) for var in variables}
  49. def _replace_variables(self, content, variables):
  50. for variable in variables:
  51. variable_content = '{0}: {1};'.format(
  52. variable['name'],
  53. variable['value']
  54. )
  55. regex = r'{0}\:?\s(.*?);'.format(variable['name'])
  56. content = re.sub(regex, variable_content, content)
  57. return content
  58. # ----------------------------------------------------------
  59. # Read
  60. # ----------------------------------------------------------
  61. def get_content(self, url, xmlid):
  62. custom_url = self._get_custom_url(url, xmlid)
  63. custom_attachment = self._get_custom_attachment(custom_url)
  64. if custom_attachment.exists():
  65. return base64.b64decode(custom_attachment.datas).decode('utf-8')
  66. else:
  67. match = re.compile("^/(\w+)/(.+?)(\.custom\.(.+))?\.(\w+)$").match(url)
  68. module_path = module.get_module_path(match.group(1))
  69. resource_path = "%s.%s" % (match.group(2), match.group(5))
  70. module_resource_path = module.get_resource_path(module_path, resource_path)
  71. with open(module_resource_path, "rb") as file:
  72. return file.read().decode('utf-8')
  73. def get_values(self, url, xmlid, variables):
  74. return self._get_variables(self.get_content(url, xmlid), variables)
  75. # ----------------------------------------------------------
  76. # Write
  77. # ----------------------------------------------------------
  78. def replace_content(self, url, xmlid, content):
  79. custom_url = self._get_custom_url(url, xmlid)
  80. custom_view = self._get_custom_view(custom_url)
  81. custom_attachment = self._get_custom_attachment(custom_url)
  82. datas = base64.b64encode((content or "\n").encode("utf-8"))
  83. if custom_attachment.exists():
  84. custom_attachment.write({"datas": datas})
  85. else:
  86. self.env["ir.attachment"].create({
  87. 'name': custom_url,
  88. 'type': "binary",
  89. 'mimetype': "text/scss",
  90. 'datas': datas,
  91. # TODO: old field datas_fname got removed, check if store_fname is correct and write migration
  92. 'store_fname': url.split("/")[-1],
  93. 'url': custom_url,
  94. })
  95. if not custom_view.exists():
  96. view_to_xpath = self.env["ir.ui.view"].get_related_views(
  97. xmlid, bundles=True
  98. ).filtered(lambda v: v.arch.find(url) >= 0)
  99. self.env["ir.ui.view"].create({
  100. 'name': custom_url,
  101. 'key': 'web_editor.scss_%s' % str(uuid.uuid4())[:6],
  102. 'mode': "extension",
  103. 'priority': view_to_xpath.priority,
  104. 'inherit_id': view_to_xpath.id,
  105. 'arch': """
  106. <data inherit_id="%(inherit_xml_id)s" name="%(name)s">
  107. <xpath expr="//link[@href='%(url_to_replace)s']" position="attributes">
  108. <attribute name="href">%(new_url)s</attribute>
  109. </xpath>
  110. </data>
  111. """ % {
  112. 'inherit_xml_id': view_to_xpath.xml_id,
  113. 'name': custom_url,
  114. 'url_to_replace': url,
  115. 'new_url': custom_url,
  116. }
  117. })
  118. self.env["ir.qweb"].clear_caches()
  119. def replace_values(self, url, xmlid, variables):
  120. content = self._replace_variables(
  121. self.get_content(url, xmlid), variables
  122. )
  123. self.replace_content(url, xmlid, content)
  124. def reset_values(self, url, xmlid):
  125. custom_url = self._get_custom_url(url, xmlid)
  126. self._get_custom_attachment(custom_url).unlink()
  127. self._get_custom_view(custom_url).unlink()