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.

141 lines
5.7 KiB

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