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.

81 lines
3.6 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Authors: Cédric Pigeon
  5. # Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu)
  6. # All Rights Reserved
  7. #
  8. # WARNING: This program as such is intended to be used by professional
  9. # programmers who take the whole responsibility of assessing all potential
  10. # consequences resulting from its eventual inadequacies and bugs.
  11. # End users who are looking for a ready-to-use solution with commercial
  12. # guarantees and support are strongly advised to contact a Free Software
  13. # Service Company.
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as
  17. # published by the Free Software Foundation, either version 3 of the
  18. # License, or (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. #
  28. ##############################################################################
  29. from openerp import models, api
  30. from lxml import etree as ET
  31. class ir_model_data(models.Model):
  32. _inherit = 'ir.model.data'
  33. @api.model
  34. def _update(self, model, module, values, xml_id=False, store=True,
  35. noupdate=False, mode='init', res_id=False):
  36. if model == 'ir.ui.view':
  37. parameter_model = self.env['ir.config_parameter']
  38. page_prefix = parameter_model.get_param('help_online_page_prefix',
  39. False)
  40. if page_prefix and xml_id.startswith('website.%s' % page_prefix):
  41. xml_str = self.manageImageReferences(values['arch'], module)
  42. values['arch'] = xml_str
  43. return super(ir_model_data, self)._update(model,
  44. module,
  45. values,
  46. xml_id=xml_id,
  47. store=store,
  48. noupdate=noupdate,
  49. mode=mode,
  50. res_id=res_id)
  51. def manageImageReferences(self, xml_str, module):
  52. parser = ET.XMLParser(remove_blank_text=True)
  53. root = ET.XML(xml_str, parser=parser)
  54. img_model = 'ir.attachment'
  55. for img_elem in root.iter('img'):
  56. if img_model in img_elem.get('src'):
  57. img_src = img_elem.get('src')
  58. try:
  59. if '/ir.attachment/' in img_src:
  60. fragments = img_src.split('/ir.attachment/')
  61. xml_id = fragments[1].split('|')[0]
  62. img_src = img_src.replace("|", "_")
  63. else:
  64. id_pos = img_src.index('id=') + 3
  65. xml_id = img_elem.get('src')[id_pos:]
  66. img_id = self.get_object_reference(module,
  67. xml_id)
  68. img_elem.attrib['src'] = img_src.replace(xml_id,
  69. str(img_id[1]))
  70. except:
  71. continue
  72. return ET.tostring(root, encoding='utf-8', xml_declaration=False)