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.

43 lines
1.4 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. auto_join=True,
  17. ondelete="cascade",
  18. index=True,
  19. )
  20. display_name = fields.Char(compute='_compute_new_display_name',
  21. store=True, index=True)
  22. _sql_constraints = [
  23. ('name_city_uniq', 'UNIQUE(name, city_id)',
  24. 'You already have a zip with that code in the same city. '
  25. 'The zip code must be unique within it\'s city'),
  26. ]
  27. @api.multi
  28. @api.depends('name', 'city_id', 'city_id.state_id', 'city_id.country_id')
  29. def _compute_new_display_name(self):
  30. for rec in self:
  31. name = [rec.name, rec.city_id.name]
  32. if rec.city_id.state_id:
  33. name.append(rec.city_id.state_id.name)
  34. if rec.city_id.country_id:
  35. name.append(rec.city_id.country_id.name)
  36. rec.display_name = ", ".join(name)