Skip to content
Snippets Groups Projects
Commit a14d6bb9 authored by Quentin De Paoli's avatar Quentin De Paoli
Browse files

[ADD] base_address_city: add a module to let the users choose through a list...

[ADD] base_address_city: add a module to let the users choose through a list the city instead of a varchar field, when inputting addresses
parent 0dc4e13f
Branches
Tags
No related merge requests found
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import models
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name': 'City Addresses',
'summary': 'Add a many2one field city on addresses',
'sequence': '19',
'category': 'Base',
'complexity': 'easy',
'description': """
City Management in Addresses
============================
This module allows to enforce users to choose the city of a partner inside a given list instead of a free text field.
""",
'data': [
'security/ir.model.access.csv',
'views/res_city_view.xml',
'views/res_country_view.xml',
],
'depends': ['base'],
}
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import res_city
import res_country
import res_partner
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
class City(models.Model):
_name = 'res.city'
_description = 'City'
_order = 'name'
name = fields.Char("Name", required=True, translate=True)
zipcode = fields.Char("Zip")
country_id = fields.Many2one('res.country', string='Country', required=True)
state_id = fields.Many2one(
'res.country.state', 'State', domain="[('country_id', '=', country_id)]")
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
class Country(models.Model):
_inherit = 'res.country'
enforce_cities = fields.Boolean(
string='Enforce Cities',
help="Check this box to ensure every address created in that country has a 'City' chosen "
"in the list of the country's cities.")
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from lxml import etree
from odoo import api, models, fields
class FormatAddressMixin(models.AbstractModel):
_inherit = "format.address.mixin"
@api.model
def fields_view_get_address(self, arch):
arch = super(FormatAddressMixin, self).fields_view_get_address(arch)
#render the partner address accordingly to address_view_id
doc = etree.fromstring(arch)
for city_node in doc.xpath("//field[@name='city']"):
replacement_xml = """
<div>
<field name="country_enforce_cities" invisible="1"/>
<div attrs="{'invisible': [('country_enforce_cities', '=', False)]}">
<field name='city' attrs="{'invisible': ['|', ('city_id', '!=', False), ('city', '=', False)]}"/>
<field name='city_id'/>
</div>
</div>
"""
city_id_node = etree.fromstring(replacement_xml)
city_node.getparent().replace(city_node, city_id_node)
arch = etree.tostring(doc)
return arch
class Partner(models.Model):
_inherit = 'res.partner'
country_enforce_cities = fields.Boolean(related='country_id.enforce_cities')
city_id = fields.Many2one('res.city', string='Company')
@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
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
"access_res_city_group_user","res_city group_user","model_res_city","base.group_partner_manager",1,1,1,1
"access_res_city_group_all","res_city group_user_all","model_res_city",,1,0,0,0
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="view_city_tree" model="ir.ui.view">
<field name="model">res.city</field>
<field name="arch" type="xml">
<tree string="City" editable="top">
<field name="name"/>
<field name="zipcode"/>
<field name="country_id"/>
<field name="state_id"/>
</tree>
</field>
</record>
<record id="view_city_filter" model="ir.ui.view">
<field name="model">res.city</field>
<field name="arch" type="xml">
<search string="Search City">
<field name="name" filter_domain="['|', ('name','ilike',self), ('zipcode','ilike',self)]"
string="City"/>
<separator/>
<field name="country_id"/>
</search>
</field>
</record>
<record id="action_res_city_tree" model="ir.actions.act_window">
<field name="name">Cities</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.city</field>
<field name="view_type">form</field>
<field name="view_mode">tree</field>
<field name="help">
Display and manage the list of all cities that can be assigned to
your partner records. Note that an option can be set on each country separately
to enforce any address of it to have a city in this list.
</field>
</record>
</data>
</odoo>
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="view_res_country_city_extended_form" model="ir.ui.view">
<field name="model">res.country</field>
<field name="inherit_id" ref="base.view_country_form"/>
<field name="arch" type="xml">
<xpath expr="//div[@class='oe_button_box']" position="inside">
<button name="%(action_res_city_tree)d"
class="oe_stat_button"
icon="fa-globe"
type="action"
context="{'default_country_id': active_id, 'search_default_country_id': active_id}"
string="Cities">
</button>
</xpath>
<xpath expr="//field[@name='phone_code']" position="after">
<field name="enforce_cities"/>
</xpath>
</field>
</record>
</data>
</odoo>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment