Browse files

[FIX] base_address_city: correct view_get with address fields

Have a partner with children exceeding the limit of the kanban.
Add a contact > save and quit
Save the partner

Before this commit, there was a traceback, because a check on parent_id was done to compute
the modifier readonly of the city fields

However, onchange gets rid of parent_id because child_ids and parent_id are two faces of the same coin
And if that check was not done, we would end up in an infinite recursion

After this commit, there is no traceback, as we retrieve the parent with another means
Also, in subviews, checking the parent might be overkill, because we literally know there is one
But you know, safety first

OPW 1886320
  • Loading branch information...
kebeclibre committed Sep 19, 2018
1 parent 6ce265b commit 7e9185c73f7338823cf901926566fd111b27c6d1
Showing with 47 additions and 11 deletions.
  1. +47 −11 addons/base_address_city/models/
@@ -25,17 +25,53 @@ def _fields_view_get_address(self, arch):
# render the partner address accordingly to address_view_id
doc = etree.fromstring(arch)
if doc.xpath("//field[@name='city_id']"):
return arch
for city_node in doc.xpath("//field[@name='city']"):
replacement_xml = """
<field name="country_enforce_cities" invisible="1"/>
<field name='city' placeholder="%s" attrs="{'invisible': [('country_enforce_cities', '=', True), ('city_id', '!=', False)], 'readonly': [('type', '=', 'contact'), ('parent_id', '!=', False)]}"/>
<field name='city_id' placeholder="%s" attrs="{'invisible': [('country_enforce_cities', '=', False)], 'readonly': [('type', '=', 'contact'), ('parent_id', '!=', False)]}" context="{'default_country_id': country_id}" domain="[('country_id', '=', country_id)]"/>
""" % (_('City'), _('City'))
city_id_node = etree.fromstring(replacement_xml)
city_node.getparent().replace(city_node, city_id_node)
return arch
list_replacement = [
'''<field name="country_enforce_cities" invisible="1"/>''',
'''<field name='city' placeholder="%(placeholder)s"
'invisible': [('country_enforce_cities', '=', True), ('city_id', '!=', False)],
'readonly': [('type', '=', 'contact'), %(parent_condition)s]
'''<field name='city_id' placeholder="%(placeholder)s"
'invisible': [('country_enforce_cities', '=', False)],
'readonly': [('type', '=', 'contact'), %(parent_condition)s]
context="{'default_country_id': country_id}" domain="[('country_id', '=', country_id)]"
def _do_replace(node, data_format):
index_insert = 0
prev = node.getprevious()
parent = node.getparent()
if prev is not None:
index_insert = parent.index(prev) + 1
for line in list_replacement:
line = line % data_format
element = etree.fromstring(line)
parent.insert(index_insert, element)
index_insert += 1
replacement_data = {
'placeholder': _('City'),
'parent_condition': "('parent_id', '!=', False)",
# First, the actual Main Form
main_form_city_node = doc.xpath("//field[@name='city'][not(ancestor::field)]")[0]
_do_replace(main_form_city_node, replacement_data)
# Second, the subviews
for child in doc.xpath("//field[@name='child_ids'][not(ancestor::field)]//field[@name='city']"):
replacement_data['parent_condition'] = "('', '!=', False)"
_do_replace(child, replacement_data)
arch = etree.tostring(doc, encoding='unicode')
return arch

0 comments on commit 7e9185c

Please sign in to comment.