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.

57 lines
2.4 KiB

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