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.

73 lines
3.2 KiB

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