forked from Techsystech/web
[CHG] use same xml_id for duplicate images in help pages
parent
0f13ce4a0c
commit
0103535603
|
@ -43,36 +43,60 @@ class ExportHelpWizard(models.TransientModel):
|
||||||
data = fields.Binary('XML', readonly=True)
|
data = fields.Binary('XML', readonly=True)
|
||||||
export_filename = fields.Char('Export XML Filename', size=128)
|
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
|
- Extract images from page and generate a xml node
|
||||||
- Replace db id in url with xml id
|
- Replace db id in url with xml id
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def substitute_id_by_xml_id(img_elem):
|
def get_attach_id(images_reference,
|
||||||
new_src = False
|
img_model, img_src, generated_xml_id=False):
|
||||||
attach_id = False
|
attach_id = False
|
||||||
img_src = img_elem.get('src')
|
|
||||||
if 'id=' in img_src:
|
if 'id=' in img_src:
|
||||||
id_pos = img_src.index('id=') + 3
|
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)
|
new_src = img_src.replace(attach_id, xml_id)
|
||||||
else:
|
else:
|
||||||
fragments = img_src.split('ir.attachment/')
|
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" % \
|
new_src = "/website/image/ir.attachment/%s|%s" % \
|
||||||
(xml_id, trail)
|
(xml_id, trail)
|
||||||
return new_src, attach_id
|
return new_src
|
||||||
|
|
||||||
i_img = 0
|
i_img = 0
|
||||||
img_model = 'ir.attachment'
|
img_model = 'ir.attachment'
|
||||||
for img_elem in page_node.iter('img'):
|
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
|
i_img += 1
|
||||||
xml_id = "%s_img_%s" % \
|
generated_xml_id = "%s_img_%s" % \
|
||||||
(page_node.attrib['name'], str(i_img).rjust(2, '0'))
|
(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:
|
if not attach_id:
|
||||||
continue
|
continue
|
||||||
|
@ -197,7 +221,7 @@ class ExportHelpWizard(models.TransientModel):
|
||||||
order='name')
|
order='name')
|
||||||
xml_to_export = ET.Element('openerp')
|
xml_to_export = ET.Element('openerp')
|
||||||
data_node = ET.SubElement(xml_to_export, 'data')
|
data_node = ET.SubElement(xml_to_export, 'data')
|
||||||
|
images_reference = {}
|
||||||
for view_data in view_data_list:
|
for view_data in view_data_list:
|
||||||
parser = ET.XMLParser(remove_blank_text=True)
|
parser = ET.XMLParser(remove_blank_text=True)
|
||||||
root = ET.XML(view_data['arch'], parser=parser)
|
root = ET.XML(view_data['arch'], parser=parser)
|
||||||
|
@ -208,7 +232,7 @@ class ExportHelpWizard(models.TransientModel):
|
||||||
root.attrib['id'] = template_id
|
root.attrib['id'] = template_id
|
||||||
root.attrib['page'] = 'True'
|
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)
|
self._clean_href_urls(root, page_prefix, template_prefix)
|
||||||
data_node.append(root)
|
data_node.append(root)
|
||||||
|
|
||||||
|
|
|
@ -121,17 +121,17 @@ class test_export_help_wizard(object):
|
||||||
self.assertNotIn("website.", templateNodeList[0].attrib['name'])
|
self.assertNotIn("website.", templateNodeList[0].attrib['name'])
|
||||||
|
|
||||||
if self.imgXmlId:
|
if self.imgXmlId:
|
||||||
xPath = ".//record[@id='%s_img_01']" % self.pageName
|
xPath = ".//record[@id='%s']" % self.imgXmlId
|
||||||
imgNodeList = rootXml.findall(xPath)
|
imgNodeList = rootXml.findall(xPath)
|
||||||
self.assertEqual(len(imgNodeList), 1)
|
self.assertEqual(len(imgNodeList), 2)
|
||||||
|
|
||||||
for imgElem in templateNodeList[0].iter('img'):
|
for imgElem in templateNodeList[0].iter('img'):
|
||||||
imgSrc = imgElem.get('src')
|
imgSrc = imgElem.get('src')
|
||||||
if '/ir.attachment/' in imgSrc:
|
if '/ir.attachment/' in imgSrc:
|
||||||
self.assertIn("/ir.attachment/%s_img_02|"
|
self.assertIn("/ir.attachment/%s|"
|
||||||
% self.pageName, imgSrc)
|
% self.imgXmlId, imgSrc)
|
||||||
else:
|
else:
|
||||||
self.assertIn("id=%s_img_01" % self.pageName, imgSrc)
|
self.assertIn("id=%s" % self.imgXmlId, imgSrc)
|
||||||
|
|
||||||
if self.pageTemplate:
|
if self.pageTemplate:
|
||||||
xPath = ".//template[@id='website.%s_snippet']" % self.pageName
|
xPath = ".//template[@id='website.%s_snippet']" % self.pageName
|
||||||
|
|
Loading…
Reference in New Issue