diff --git a/addons/website/models/ir_ui_view.py b/addons/website/models/ir_ui_view.py index ad95a6b9ff7..b935d6e3465 100644 --- a/addons/website/models/ir_ui_view.py +++ b/addons/website/models/ir_ui_view.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import copy from urlparse import urlparse from lxml import etree, html @@ -84,19 +85,24 @@ class view(osv.osv): return out def replace_arch_section(self, cr, uid, view_id, section_xpath, replacement, context=None): - # remove branding from replacement section - for att in ir.ir_ui_view.MOVABLE_BRANDING: - replacement.attrib.pop(att, None) + # the root of the arch section shouldn't actually be replaced as it's + # not really editable itself, only the content truly is editable. + [view] = self.browse(cr, uid, [view_id], context=context) + arch = etree.fromstring(view.arch.encode('utf-8')) + # => get the replacement root if not section_xpath: - # replace all of the arch, not just a fragment - arch = replacement + root = arch else: - arch = etree.fromstring(self.browse(cr, uid, view_id, context=context).arch.encode('utf-8')) # ensure there's only one match - [previous_section] = arch.xpath(section_xpath) + [root] = arch.xpath(section_xpath) - previous_section.getparent().replace(previous_section, replacement) + root.text = replacement.text + root.tail = replacement.tail + # replace all children + del root[:] + for child in replacement: + root.append(copy.deepcopy(child)) return arch diff --git a/addons/website/tests/test_views.py b/addons/website/tests/test_views.py index 812981cd706..8041a7e5f1c 100644 --- a/addons/website/tests/test_views.py +++ b/addons/website/tests/test_views.py @@ -92,10 +92,17 @@ class TestViewSaving(common.TransactionCase): self.eq(View.to_field_ref(self.cr, self.uid, embedded, context=None), h.SPAN({'t-field': 'bob', 'class': 'foo bar', 'id': 'whop'})) - def test_replace_arch(self): replacement = h.P("Wheee") + result = self.registry('ir.ui.view').replace_arch_section( + self.cr, self.uid, self.view_id, None, replacement) + + self.eq(result, h.DIV("Wheee")) + + def test_replace_arch_2(self): + replacement = h.DIV(h.P("Wheee")) + result = self.registry('ir.ui.view').replace_arch_section( self.cr, self.uid, self.view_id, None, replacement) @@ -110,7 +117,7 @@ class TestViewSaving(common.TransactionCase): self.eq(result, h.DIV( h.DIV( - h.H1("I am the greatest title alive!"), + h.H3("I am the greatest title alive!"), h.UL( h.LI("Item 1"), h.LI("Item 2"),