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.

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