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.

138 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_attachment = self._get_custom_attachment(custom_url)
  78. datas = base64.b64encode((content or "\n").encode("utf-8"))
  79. if custom_attachment.exists():
  80. custom_attachment.write({"datas": datas})
  81. else:
  82. self.env["ir.attachment"].create({
  83. 'name': custom_url,
  84. 'type': "binary",
  85. 'mimetype': "text/scss",
  86. 'datas': datas,
  87. 'datas_fname': url.split("/")[-1],
  88. 'url': custom_url,
  89. })
  90. view_to_xpath = self.env["ir.ui.view"].get_related_views(
  91. xmlid, bundles=True
  92. ).filtered(lambda v: v.arch.find(url) >= 0)
  93. self.env["ir.ui.view"].create({
  94. 'name': custom_url,
  95. 'key': 'web_editor.scss_%s' % str(uuid.uuid4())[:6],
  96. 'mode': "extension",
  97. 'inherit_id': view_to_xpath.id,
  98. 'arch': """
  99. <data inherit_id="%(inherit_xml_id)s" name="%(name)s">
  100. <xpath expr="//link[@href='%(url_to_replace)s']" position="attributes">
  101. <attribute name="href">%(new_url)s</attribute>
  102. </xpath>
  103. </data>
  104. """ % {
  105. 'inherit_xml_id': view_to_xpath.xml_id,
  106. 'name': custom_url,
  107. 'url_to_replace': url,
  108. 'new_url': custom_url,
  109. }
  110. })
  111. self.env["ir.qweb"].clear_caches()
  112. def replace_values(self, url, xmlid, variables):
  113. content = self._replace_variables(
  114. self.get_content(url, xmlid), variables
  115. )
  116. self.replace_content(url, xmlid, content)
  117. def reset_values(self, url, xmlid):
  118. custom_url = self._get_custom_url(url, xmlid)
  119. self._get_custom_attachment(custom_url).unlink()
  120. self._get_custom_view(custom_url).unlink()