[FIX] change view section saving: only save view section *content*

Before, would save view section itself (including root element). But
branding distribution (and thus editability) are set on the last
programmatic root, thus the root element may well be generated
e.g. <span t-att-foo> will define the span itself as editable.

Saving this would remove the programmatic content on the node, and
thus break the view section altogether.

Fix the issue by only saving the root's content in the previous root.

bzr revid: xmo@openerp.com-20131217112034-nbxbg919cffv4w51
This commit is contained in:
Xavier Morel 2013-12-17 12:20:34 +01:00
parent f6eb40fc90
commit 4469b36f77
2 changed files with 23 additions and 10 deletions

View File

@ -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

View File

@ -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"),