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.

56 lines
2.4 KiB

  1. # Copyright 2014 ACSONE SA/NV (<http://acsone.eu>)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import models, api
  4. from lxml import etree as ET
  5. class IrModelData(models.Model):
  6. _inherit = 'ir.model.data'
  7. @api.model
  8. def _update(self, model, module, values, xml_id=False, store=True,
  9. noupdate=False, mode='init', res_id=False):
  10. if model == 'ir.ui.view':
  11. parameter_model = self.env['ir.config_parameter']
  12. page_prefix = parameter_model.get_param('help_online_page_prefix',
  13. False)
  14. if page_prefix and xml_id.startswith('website.%s' % page_prefix):
  15. xml_str = self.manageImageReferences(values['arch'], module)
  16. values['arch'] = xml_str
  17. return super(IrModelData, self)._update(model,
  18. module,
  19. values,
  20. xml_id=xml_id,
  21. store=store,
  22. noupdate=noupdate,
  23. mode=mode,
  24. res_id=res_id)
  25. def manageImageReferences(self, xml_str, module):
  26. parser = ET.XMLParser(remove_blank_text=True)
  27. root = ET.XML(xml_str, parser=parser)
  28. img_model = 'ir.attachment'
  29. for img_elem in root.iter('img'):
  30. if img_model in img_elem.get('src'):
  31. img_src = img_elem.get('src')
  32. try:
  33. if '/ir.attachment/' in img_src:
  34. fragments = img_src.split('/ir.attachment/')
  35. xml_id = fragments[1].split('|')[0]
  36. img_src = img_src.replace("|", "_")
  37. else:
  38. id_pos = img_src.index('id=') + 3
  39. xml_id = img_elem.get('src')[id_pos:]
  40. img_id = self.get_object_reference(module,
  41. xml_id)
  42. img_elem.attrib['src'] = img_src.replace(xml_id,
  43. str(img_id[1]))
  44. except:
  45. continue
  46. return ET.tostring(root, encoding='utf-8', xml_declaration=False)