Permalink
Browse files

[FIX] base_address_city: correct view_get with address fields

Have a partner with children exceeding the limit of the kanban.
Edit,
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 in subviews

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

After this commit, there is no traceback, as we stop checking on parent_id in the case of paged subviews
(list and kanban) because we literally know there is one

OPW 1886320
  • Loading branch information...
kebeclibre committed Sep 19, 2018
1 parent 6ce265b commit 59c53d6e414e77ef8d3a75eec84608f61cd7b753
Showing with 49 additions and 10 deletions.
  1. +49 −10 addons/base_address_city/models/res_partner.py
@@ -25,17 +25,56 @@ 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
return arch
list_replacement = [
'''<field name="country_enforce_cities" invisible="1"/>''',
'''<field name='city' placeholder="%(placeholder)s"
attrs="{
'invisible': [('country_enforce_cities', '=', True), ('city_id', '!=', False)],
'readonly': [('type', '=', 'contact')%(parent_condition)s]
}"
/>''',
'''<field name='city_id' placeholder="%(placeholder)s"
attrs="{
'invisible': [('country_enforce_cities', '=', False)],
'readonly': [('type', '=', 'contact')%(parent_condition)s]
}"
context="{'default_country_id': country_id}" domain="[('country_id', '=', country_id)]"
/>''',
]
def _get_viewtype(node):
parent = node.getparent()
while parent and parent.tag not in ['list', 'tree', 'kanban', 'form']:
parent = parent.getparent()
return parent.tag
def _do_replace(node, data_format):
index_insert = 0
prev = node.getprevious()
parent = node.getparent()
parent.remove(node)
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'),
}
for city_node in doc.xpath("//field[@name='city']"):
replacement_xml = """
<div>
<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)]"/>
</div>
""" % (_('City'), _('City'))
city_id_node = etree.fromstring(replacement_xml)
city_node.getparent().replace(city_node, city_id_node)
parent_condition = ", ('parent_id', '!=', False)"
if _get_viewtype(city_node) != 'form':
parent_condition = ""
replacement_data['parent_condition'] = parent_condition
_do_replace(city_node, replacement_data)
arch = etree.tostring(doc, encoding='unicode')
return arch

0 comments on commit 59c53d6

Please sign in to comment.