Browse Source

IMP base_location module

- Migrate to v8 api
- Rename name field to zip
- Create new computed stored field 'name' that use zip, city, state and country in order to allow search using '%' from the m2o widget
pull/63/head
Juan Jose Scarafia 10 years ago
parent
commit
44f7c96be7
  1. 85
      base_location/better_zip.py
  2. 14
      base_location/better_zip_view.xml
  3. 34
      base_location/company.py
  4. 1
      base_location/company_view.xml
  5. 27
      base_location/partner.py
  6. 2
      base_location/partner_view.xml
  7. 8
      base_location/state.py
  8. 2
      base_location/state_view.xml
  9. 4
      base_location_geonames_import/test/import.yml
  10. 4
      base_location_geonames_import/wizard/geonames_import.py

85
base_location/better_zip.py

@ -19,64 +19,43 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
from openerp.osv import orm, fields
from openerp import models, fields, api
class BetterZip(orm.Model):
class BetterZip(models.Model):
" City/locations completion object"
_name = "res.better.zip"
_description = __doc__
_order = "priority"
_columns = {'priority': fields.integer('Priority', deprecated=True),
'name': fields.char('ZIP'),
'city': fields.char('City', required=True),
'state_id': fields.many2one('res.country.state', 'State'),
'country_id': fields.many2one('res.country', 'Country'),
'code': fields.char('City Code', size=64,
help="The official code for the city"),
}
_defaults = {'priority': 100}
def name_get(self, cursor, uid, ids, context=None):
res = []
for bzip in self.browse(cursor, uid, ids, context=context):
if bzip.name:
name = [bzip.name, bzip.city]
else:
name = [bzip.city]
if bzip.state_id:
name.append(bzip.state_id.name)
if bzip.country_id:
name.append(bzip.country_id.name)
res.append((bzip.id, ", ".join(name)))
return res
def onchange_state_id(self, cr, uid, ids, state_id=False, context=None):
result = {}
if state_id:
state = self.pool['res.country.state'].browse(
cr, uid, state_id, context=context)
if state:
result['value'] = {'country_id': state.country_id.id}
return result
def name_search(
self, cr, uid, name, args=None, operator='ilike', context=None,
limit=100
):
if args is None:
args = []
if context is None:
context = {}
ids = []
if name:
ids = self.search(
cr, uid, [('name', 'ilike', name)] + args, limit=limit)
if not ids:
ids = self.search(
cr, uid, [('city', operator, name)] + args, limit=limit)
return self.name_get(cr, uid, ids, context=context)
priority = fields.Integer('Priority', deprecated=True)
name = fields.Char('Name', compute='_get_name', store=True)
zip = fields.Char('ZIP')
city = fields.Char('City', required=True)
state_id = fields.Many2one('res.country.state', 'State')
country_id = fields.Many2one('res.country', 'Country')
code = fields.Char(
'City Code', size=64, help="The official code for the city")
@api.one
@api.depends(
'zip',
'city',
'state_id',
'state_id.name',
'country_id',
'country_id.name',
)
def _get_name(self):
self.name = ('(%s) %s, %s, %s') % (
self.zip or '',
self.city or '',
self.state_id and self.state_id.name or '',
self.country_id and self.country_id.name or '',
)
@api.onchange('state_id')
def onchange_state_id(self):
if self.state_id:
self.country_id = self.state_id.country_id.id

14
base_location/better_zip_view.xml

@ -8,11 +8,10 @@
<field name="arch" type="xml">
<form string="ZIP" version="7.0">
<group col="4">
<field name="name"/>
<field name="zip"/>
<field name="code"/>
<field name="city"/>
<field name="priority"/>
<field name="state_id" on_change="onchange_state_id(state_id)"/>
<field name="state_id"/>
<field name="country_id"/>
</group>
</form>
@ -24,11 +23,11 @@
<field name="model">res.better.zip</field>
<field name="arch" type="xml">
<tree string="ZIP">
<field name="name"/>
<field name="zip"/>
<field name="code"/>
<field name="city"/>
<field name="state_id"/>
<field name="country_id"/>
<field name="priority"/>
</tree>
</field>
</record>
@ -38,8 +37,11 @@
<field name="model">res.better.zip</field>
<field name="arch" type="xml">
<search string="Search city">
<field name="name"/>
<field name="code"/>
<field name="zip"/>
<field name="city"/>
<field name="state_id"/>
<field name="country_id"/>
</search>
</field>
</record>

34
base_location/company.py

@ -19,31 +19,21 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
from openerp.osv import orm, fields
from openerp import models, fields, api
class ResCompany(orm.Model):
class ResCompany(models.Model):
_inherit = 'res.company'
def on_change_city(self, cr, uid, ids, zip_id, context=None):
result = {}
if context is None:
context = {}
if zip_id:
bzip = self.pool['res.better.zip'].browse(
cr, uid, zip_id, context=context)
result = {'value': {
'zip': bzip.name,
'country_id': bzip.country_id.id if bzip.country_id else False,
'city': bzip.city,
'state_id': bzip.state_id.id if bzip.state_id else False
}
}
return result
@api.onchange('better_zip_id')
def on_change_city(self):
if self.better_zip_id:
self.zip = self.better_zip_id.zip
self.city = self.better_zip_id.city
self.state_id = self.better_zip_id.state_id
self.country_id = self.better_zip_id.country_id
_columns = {
'better_zip_id': fields.many2one(
'res.better.zip', 'Location', select=1,
help=('Use the city name or the zip code to search the location')),
}
better_zip_id = fields.Many2one(
'res.better.zip', 'Location', select=1,
help=('Use the city name or the zip code to search the location'))

1
base_location/company_view.xml

@ -11,7 +11,6 @@
<field name="better_zip_id"
options="{'create_name_field': 'city'}"
colspan="4"
on_change="on_change_city(better_zip_id)"
placeholder="City completion" />
</field>
</field>

27
base_location/partner.py

@ -19,24 +19,17 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
from openerp.osv import orm, fields
from openerp import models, fields, api
class ResPartner(orm.Model):
class ResPartner(models.Model):
_inherit = "res.partner"
_columns = {'zip_id': fields.many2one('res.better.zip', 'City/Location')}
zip_id = fields.Many2one('res.better.zip', 'City/Location')
def onchange_zip_id(self, cursor, uid, ids, zip_id, context=None):
if not zip_id:
return {}
if isinstance(zip_id, list):
zip_id = zip_id[0]
bzip = self.pool['res.better.zip'].browse(
cursor, uid, zip_id, context=context)
return {'value': {
'zip': bzip.name,
'city': bzip.city,
'country_id': bzip.country_id.id if bzip.country_id else False,
'state_id': bzip.state_id.id if bzip.state_id else False,
}
}
@api.onchange('zip_id')
def onchange_zip_id(self):
if self.zip_id:
self.zip = self.zip_id.zip
self.city = self.zip_id.city
self.country_id = self.zip_id.country_id
self.state_id = self.zip_id.state_id

2
base_location/partner_view.xml

@ -9,7 +9,6 @@
<field name="street2" position="after">
<field name="zip_id"
options="{'create_name_field': 'city'}"
on_change="onchange_zip_id(zip_id)"
placeholder="City completion"
attrs="{'invisible': [('use_parent_address','=',True)]}"
class="oe_edit_only"
@ -18,7 +17,6 @@
<xpath expr="//field[@name='child_ids']/form//field[@name='street2']" position="after">
<field name="zip_id"
options="{'create_name_field': 'city'}"
on_change="onchange_zip_id(zip_id)"
placeholder="City completion"
attrs="{'invisible': [('use_parent_address','=',True)]}"
class="oe_edit_only"

8
base_location/state.py

@ -19,12 +19,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
from openerp.osv import orm, fields
from openerp import models, fields
class ResCountryState(orm.Model):
class ResCountryState(models.Model):
_inherit = 'res.country.state'
_columns = {'better_zip_ids': fields.one2many(
'res.better.zip', 'state_id', 'Cities')}
better_zip_ids = fields.One2many(
'res.better.zip', 'state_id', 'Cities')

2
base_location/state_view.xml

@ -13,7 +13,7 @@
colspan="4"
nolabel="1">
<tree editable="top">
<field name="name"/>
<field name="zip"/>
<field name="code"/>
<field name="city"/>
<field name="country_id"/>

4
base_location_geonames_import/test/import.yml

@ -19,7 +19,7 @@
], context=context)
assert len(state_ids) == 1, "There must be 1 LG"
zip_ids = self.search(cr, uid, [
('name', '=', '16017'),
('zip', '=', '16017'),
('city', '=', 'Isola Del Cantone'),
('state_id', '=', state_ids[0]),
('country_id', '=', ref('base.it'))
@ -46,7 +46,7 @@
], context=context)
assert len(state_ids) == 1, "There must be 1 LG"
zip_ids = self.search(cr, uid, [
('name', '=', '16017'),
('zip', '=', '16017'),
('city', '=', 'Isola Del Cantone'),
('state_id', '=', state_ids[0]),
('country_id', '=', ref('base.it'))

4
base_location_geonames_import/wizard/geonames_import.py

@ -46,7 +46,7 @@ class better_zip_geonames_import(models.TransientModel):
def _prepare_better_zip(self, row, country_id):
state = self.select_or_create_state(row, country_id)
vals = {
'name': row[1],
'zip': row[1],
'city': row[2],
'state_id': state.id,
'country_id': country_id,
@ -86,7 +86,7 @@ class better_zip_geonames_import(models.TransientModel):
return states[0]
else:
return self.env['res.country.state'].create({
'name': row[name_row_index],
'zip': row[name_row_index],
'code': row[code_row_index],
'country_id': country_id
})

Loading…
Cancel
Save