You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.2 KiB

  1. # Copyright 2016 Nicolas Bessi, Camptocamp SA
  2. # Copyright 2018 Aitor Bouzas <aitor.bouzas@adaptivecity.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import api, fields, models
  5. class ResCityZip(models.Model):
  6. """City/locations completion object"""
  7. _name = "res.city.zip"
  8. _description = __doc__
  9. _order = "name asc"
  10. _rec_name = "display_name"
  11. name = fields.Char('ZIP', required=True)
  12. city_id = fields.Many2one(
  13. 'res.city',
  14. 'City',
  15. required=True,
  16. )
  17. display_name = fields.Char(compute='_compute_new_display_name',
  18. store=True, index=True)
  19. _sql_constraints = [
  20. ('name_city_uniq', 'UNIQUE(name, city_id)',
  21. 'You already have a zip with that code in the same city. '
  22. 'The zip code must be unique within it\'s city'),
  23. ]
  24. @api.multi
  25. @api.depends('name', 'city_id')
  26. def _compute_new_display_name(self):
  27. for rec in self:
  28. name = [rec.name, rec.city_id.name]
  29. if rec.city_id.state_id:
  30. name.append(rec.city_id.state_id.name)
  31. if rec.city_id.country_id:
  32. name.append(rec.city_id.country_id.name)
  33. rec.display_name = ", ".join(name)