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.

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