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.

130 lines
5.4 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_variable(self, content, variable):
  39. regex = r'{0}\:?\s(.*?);'.format(variable)
  40. value = re.search(regex, content)
  41. return value and value.group(1)
  42. def _get_variables(self, content, variables):
  43. return {var: self._get_variable(content, var) for var in variables}
  44. def _replace_variables(self, content, variables):
  45. for variable in variables:
  46. variable_content = '{0}: {1};'.format(
  47. variable['name'],
  48. variable['value']
  49. )
  50. regex = r'{0}\:?\s(.*?);'.format(variable['name'])
  51. content = re.sub(regex, variable_content, content)
  52. return content
  53. #----------------------------------------------------------
  54. # Read
  55. #----------------------------------------------------------
  56. def get_content(self, url, xmlid):
  57. custom_url = self._get_custom_url(url, xmlid)
  58. custom_attachment = self._get_custom_attachment(custom_url)
  59. if custom_attachment.exists():
  60. return base64.b64decode(custom_attachment.datas).decode('utf-8')
  61. else:
  62. match = re.compile("^/(\w+)/(.+?)(\.custom\.(.+))?\.(\w+)$").match(url)
  63. module_path = module.get_module_path(match.group(1))
  64. resource_path = "%s.%s" % (match.group(2), match.group(5))
  65. module_resource_path = module.get_resource_path(module_path, resource_path)
  66. with open(module_resource_path, "rb") as file:
  67. return file.read().decode('utf-8')
  68. def get_values(self, url, xmlid, variables):
  69. return self._get_variables(self.get_content(url, xmlid), variables)
  70. #----------------------------------------------------------
  71. # Write
  72. #----------------------------------------------------------
  73. def replace_content(self, url, xmlid, content):
  74. custom_url = self._get_custom_url(url, xmlid)
  75. custom_attachment = self._get_custom_attachment(custom_url)
  76. datas = base64.b64encode((content or "\n").encode("utf-8"))
  77. if custom_attachment.exists():
  78. custom_attachment.write({"datas": datas})
  79. else:
  80. self.env["ir.attachment"].create({
  81. 'name': custom_url,
  82. 'type': "binary",
  83. 'mimetype': "text/scss",
  84. 'datas': datas,
  85. 'datas_fname': url.split("/")[-1],
  86. 'url': custom_url,
  87. })
  88. view_to_xpath = self.env["ir.ui.view"].get_related_views(
  89. xmlid, bundles=True
  90. ).filtered(lambda v: v.arch.find(url) >= 0)
  91. self.env["ir.ui.view"].create({
  92. 'name': custom_url,
  93. 'key': 'web_editor.scss_%s' % str(uuid.uuid4())[:6],
  94. 'mode': "extension",
  95. 'inherit_id': view_to_xpath.id,
  96. 'arch': """
  97. <data inherit_id="%(inherit_xml_id)s" name="%(name)s">
  98. <xpath expr="//link[@href='%(url_to_replace)s']" position="attributes">
  99. <attribute name="href">%(new_url)s</attribute>
  100. </xpath>
  101. </data>
  102. """ % {
  103. 'inherit_xml_id': view_to_xpath.xml_id,
  104. 'name': custom_url,
  105. 'url_to_replace': url,
  106. 'new_url': custom_url,
  107. }
  108. })
  109. self.env["ir.qweb"].clear_caches()
  110. def replace_values(self, url, xmlid, variables):
  111. content = self._replace_variables(
  112. self.get_content(url, xmlid), variables
  113. )
  114. self.replace_content(url, xmlid, content)