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.

144 lines
5.8 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Utils
  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_utils.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)])
  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. 'datas_fname': url.split("/")[-1],
  92. 'url': custom_url,
  93. })
  94. if not custom_view.exists():
  95. view_to_xpath = self.env["ir.ui.view"].get_related_views(
  96. xmlid, bundles=True
  97. ).filtered(lambda v: v.arch.find(url) >= 0)
  98. self.env["ir.ui.view"].create({
  99. 'name': custom_url,
  100. 'key': 'web_editor.scss_%s' % str(uuid.uuid4())[:6],
  101. 'mode': "extension",
  102. 'priority': view_to_xpath.priority,
  103. 'inherit_id': view_to_xpath.id,
  104. 'arch': """
  105. <data inherit_id="%(inherit_xml_id)s" name="%(name)s">
  106. <xpath expr="//link[@href='%(url_to_replace)s']" position="attributes">
  107. <attribute name="href">%(new_url)s</attribute>
  108. </xpath>
  109. </data>
  110. """ % {
  111. 'inherit_xml_id': view_to_xpath.xml_id,
  112. 'name': custom_url,
  113. 'url_to_replace': url,
  114. 'new_url': custom_url,
  115. }
  116. })
  117. self.env["ir.qweb"].clear_caches()
  118. def replace_values(self, url, xmlid, variables):
  119. content = self._replace_variables(
  120. self.get_content(url, xmlid), variables
  121. )
  122. self.replace_content(url, xmlid, content)
  123. def reset_values(self, url, xmlid):
  124. custom_url = self._get_custom_url(url, xmlid)
  125. self._get_custom_attachment(custom_url).unlink()
  126. self._get_custom_view(custom_url).unlink()