2018-01-16 06:58:15 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-01-16 11:34:37 +01:00
|
|
|
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
from lxml import etree
|
|
|
|
|
2018-01-16 11:34:37 +01:00
|
|
|
from flectra import api, models, fields
|
2018-04-05 10:25:40 +02:00
|
|
|
from flectra.tools.translate import _
|
|
|
|
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
class Partner(models.Model):
|
|
|
|
_inherit = 'res.partner'
|
|
|
|
|
2018-07-09 15:30:17 +02:00
|
|
|
country_enforce_cities = fields.Boolean(related='country_id.enforce_cities', readonly=True)
|
2018-01-16 06:58:15 +01:00
|
|
|
city_id = fields.Many2one('res.city', string='City')
|
|
|
|
|
|
|
|
@api.onchange('city_id')
|
|
|
|
def _onchange_city_id(self):
|
|
|
|
self.city = self.city_id.name
|
|
|
|
self.zip = self.city_id.zipcode
|
|
|
|
self.state_id = self.city_id.state_id
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _fields_view_get_address(self, arch):
|
|
|
|
arch = super(Partner, self)._fields_view_get_address(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 = """
|
|
|
|
<div>
|
|
|
|
<field name="country_enforce_cities" invisible="1"/>
|
2018-04-05 10:25:40 +02:00
|
|
|
<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)]"/>
|
2018-01-16 06:58:15 +01:00
|
|
|
</div>
|
2018-04-05 10:25:40 +02:00
|
|
|
""" % (_('City'), _('City'))
|
2018-01-16 06:58:15 +01:00
|
|
|
city_id_node = etree.fromstring(replacement_xml)
|
|
|
|
city_node.getparent().replace(city_node, city_id_node)
|
|
|
|
|
|
|
|
arch = etree.tostring(doc, encoding='unicode')
|
|
|
|
return arch
|