From 4c0b473590b1198c3c42fe21b6a275c8eb2bc839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Pigeon?= Date: Tue, 4 Nov 2014 13:06:12 +0100 Subject: [PATCH] [CHG] use same xml_id for duplicate images in help pages --- help_online/models/export_help_wizard.py | 48 +++++++++++++++----- help_online/tests/test_export_help_wizard.py | 10 ++-- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/help_online/models/export_help_wizard.py b/help_online/models/export_help_wizard.py index 017057f1..d8290e74 100644 --- a/help_online/models/export_help_wizard.py +++ b/help_online/models/export_help_wizard.py @@ -43,36 +43,60 @@ class ExportHelpWizard(models.TransientModel): data = fields.Binary('XML', readonly=True) export_filename = fields.Char('Export XML Filename', size=128) - def _manage_images_on_page(self, page_node, data_node): + def _manage_images_on_page(self, page_node, data_node, images_reference): """ - Extract images from page and generate a xml node - Replace db id in url with xml id """ - def substitute_id_by_xml_id(img_elem): - new_src = False + def get_attach_id(images_reference, + img_model, img_src, generated_xml_id=False): attach_id = False - img_src = img_elem.get('src') if 'id=' in img_src: id_pos = img_src.index('id=') + 3 - attach_id = img_elem.get('src')[id_pos:] + attach_id = img_src[id_pos:] + else: + fragments = img_src.split('ir.attachment/') + attach_id, _ = fragments[1].split('_', 1) + + if attach_id in images_reference: + xml_id = images_reference[attach_id] + else: + ir_data = self.env['ir.model.data'].search( + [('model', '=', img_model), + ('res_id', '=', attach_id)]) + xml_id = generated_xml_id + if ir_data: + xml_id = ir_data[0].name + images_reference[attach_id] = xml_id + + return attach_id, xml_id + + def substitute_id_by_xml_id(img_src, attach_id, xml_id): + new_src = False + if 'id=' in img_src: new_src = img_src.replace(attach_id, xml_id) else: fragments = img_src.split('ir.attachment/') - attach_id, trail = fragments[1].split('_', 1) + _, trail = fragments[1].split('_', 1) new_src = "/website/image/ir.attachment/%s|%s" % \ (xml_id, trail) - return new_src, attach_id + return new_src i_img = 0 img_model = 'ir.attachment' for img_elem in page_node.iter('img'): - if img_model in img_elem.get('src'): + img_src = img_elem.get('src') + if img_model in img_src: i_img += 1 - xml_id = "%s_img_%s" % \ + generated_xml_id = "%s_img_%s" % \ (page_node.attrib['name'], str(i_img).rjust(2, '0')) + attach_id, xml_id = get_attach_id(images_reference, + img_model, + img_src, + generated_xml_id) - new_src, attach_id = substitute_id_by_xml_id(img_elem) + new_src = substitute_id_by_xml_id(img_src, attach_id, xml_id) if not attach_id: continue @@ -197,7 +221,7 @@ class ExportHelpWizard(models.TransientModel): order='name') xml_to_export = ET.Element('openerp') data_node = ET.SubElement(xml_to_export, 'data') - + images_reference = {} for view_data in view_data_list: parser = ET.XMLParser(remove_blank_text=True) root = ET.XML(view_data['arch'], parser=parser) @@ -208,7 +232,7 @@ class ExportHelpWizard(models.TransientModel): root.attrib['id'] = template_id root.attrib['page'] = 'True' - self._manage_images_on_page(root, data_node) + self._manage_images_on_page(root, data_node, images_reference) self._clean_href_urls(root, page_prefix, template_prefix) data_node.append(root) diff --git a/help_online/tests/test_export_help_wizard.py b/help_online/tests/test_export_help_wizard.py index 2ce0015e..097e03b2 100644 --- a/help_online/tests/test_export_help_wizard.py +++ b/help_online/tests/test_export_help_wizard.py @@ -121,17 +121,17 @@ class test_export_help_wizard(object): self.assertNotIn("website.", templateNodeList[0].attrib['name']) if self.imgXmlId: - xPath = ".//record[@id='%s_img_01']" % self.pageName + xPath = ".//record[@id='%s']" % self.imgXmlId imgNodeList = rootXml.findall(xPath) - self.assertEqual(len(imgNodeList), 1) + self.assertEqual(len(imgNodeList), 2) for imgElem in templateNodeList[0].iter('img'): imgSrc = imgElem.get('src') if '/ir.attachment/' in imgSrc: - self.assertIn("/ir.attachment/%s_img_02|" - % self.pageName, imgSrc) + self.assertIn("/ir.attachment/%s|" + % self.imgXmlId, imgSrc) else: - self.assertIn("id=%s_img_01" % self.pageName, imgSrc) + self.assertIn("id=%s" % self.imgXmlId, imgSrc) if self.pageTemplate: xPath = ".//template[@id='website.%s_snippet']" % self.pageName