Permalink
Browse files

[WIP] Other solution

  • Loading branch information...
kebeclibre committed Nov 8, 2018
1 parent b181edf commit 39a67a6c500825d2a409f117a84a68dcebe097a8
Showing with 41 additions and 41 deletions.
  1. +41 −41 addons/base_address_city/models/res_partner.py
@@ -27,54 +27,54 @@ def _fields_view_get_address(self, arch):
if doc.xpath("//field[@name='city_id']"):
return arch
list_replacement = [
'''<field name="country_enforce_cities" invisible="1"/>''',
replacement_xml = """
<div>
<field name="country_enforce_cities" invisible="1"/>
<field name='city' placeholder="%(placeholder)s" />
<field name='city_id' placeholder="%(placeholder)s"
context="{'default_country_id': country_id}"
domain="[('country_id', '=', country_id)]"/>
</div>
""" % {'placeholder': _('City')}
'''<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)]"
/>''',
]
fields_attrs = {
'city': "{'invisible': [('country_enforce_cities', '=', True), ('city_id', '!=', False)], 'readonly': [('type', '=', 'contact')%s]}",
'city_id': "{'invisible': [('country_enforce_cities', '=', False)], 'readonly': [('type', '=', 'contact')%s]}"
}
def _get_viewtype(node):
def _arch_location(node):
in_subview = False
view_type = False
parent = node.getparent()
while parent is not None and parent.tag not in ['list', 'tree', 'kanban', 'form']:
while parent is not None and (not view_type or not in_subview):
if parent.tag == 'field':
in_subview = True
elif parent.tag in ['list', 'tree', 'kanban', 'form']:
view_type = parent.tag
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
return {
'view_type': view_type,
'in_subview': in_subview,
}
replacement_data = {
'placeholder': _('City'),
}
def _fill_attrs(node):
field_name = node.get('name')
new_attrs = fields_attrs.get(field_name)
if new_attrs:
location = _arch_location(node)
parent_condition = ''
if location['view_type'] == 'form' or not location['in_subview']:
parent_condition = ", ('parent_id', '!=', False)"
replace_node.attrib['attrs'] = new_attrs % parent_condition
for city_node in doc.xpath("//field[@name='city']"):
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)
replacement_tree = etree.fromstring(replacement_xml)
for replace_node in replacement_tree.getchildren():
_fill_attrs(replace_node)
city_node.addprevious(replace_node)
parent = city_node.getparent()
parent.remove(city_node)
arch = etree.tostring(doc, encoding='unicode')
return arch

0 comments on commit 39a67a6

Please sign in to comment.