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.

48 lines
1.5 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. state_id = fields.Many2one(related="city_id.state_id")
  21. country_id = fields.Many2one(related="city_id.country_id")
  22. display_name = fields.Char(
  23. compute="_compute_new_display_name", store=True, index=True
  24. )
  25. _sql_constraints = [
  26. (
  27. "name_city_uniq",
  28. "UNIQUE(name, city_id)",
  29. "You already have a zip with that code in the same city. "
  30. "The zip code must be unique within it's city",
  31. )
  32. ]
  33. @api.depends("name", "city_id", "city_id.state_id", "city_id.country_id")
  34. def _compute_new_display_name(self):
  35. for rec in self:
  36. name = [rec.name, rec.city_id.name]
  37. if rec.city_id.state_id:
  38. name.append(rec.city_id.state_id.name)
  39. if rec.city_id.country_id:
  40. name.append(rec.city_id.country_id.name)
  41. rec.display_name = ", ".join(name)