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.

100 lines
3.2 KiB

  1. # Copyright 2016 Nicolas Bessi, Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models, _
  4. from odoo.exceptions import ValidationError
  5. class BetterZip(models.Model):
  6. '''City/locations completion object'''
  7. _name = "res.better.zip"
  8. _description = __doc__
  9. _order = "name asc"
  10. name = fields.Char('ZIP')
  11. code = fields.Char(
  12. 'City Code',
  13. size=64,
  14. help="The official code for the city"
  15. )
  16. city = fields.Char('City', required=True)
  17. city_id = fields.Many2one(
  18. 'res.city',
  19. 'City',
  20. )
  21. state_id = fields.Many2one(
  22. 'res.country.state',
  23. 'State',
  24. )
  25. country_id = fields.Many2one('res.country', 'Country')
  26. enforce_cities = fields.Boolean(
  27. related='country_id.enforce_cities',
  28. readonly=True,
  29. )
  30. latitude = fields.Float()
  31. longitude = fields.Float()
  32. @api.multi
  33. @api.depends('name', 'city', 'state_id', 'country_id')
  34. def name_get(self):
  35. result = []
  36. for rec in self:
  37. name = []
  38. if rec.name:
  39. name.append('%(name)s' % {'name': rec.name})
  40. name.append('%(name)s' % {'name': rec.city})
  41. if rec.state_id:
  42. name.append('%(name)s' % {'name': rec.state_id.name})
  43. if rec.country_id:
  44. name.append('%(name)s' % {'name': rec.country_id.name})
  45. result.append((rec.id, ", ".join(name)))
  46. return result
  47. @api.onchange('country_id')
  48. def _onchange_country_id(self):
  49. if self.state_id.country_id != self.country_id:
  50. self.state_id = False
  51. if self.city_id.country_id != self.country_id:
  52. self.city_id = False
  53. if self.country_id:
  54. domain = [('country_id', '=', self.country_id.id)]
  55. else:
  56. domain = []
  57. return {
  58. 'domain': {
  59. 'state_id': domain,
  60. 'city_id': domain,
  61. }
  62. }
  63. @api.onchange('city_id')
  64. def _onchange_city_id(self):
  65. if self.city_id:
  66. self.city = self.city_id.name
  67. self.country_id = self.city_id.country_id
  68. self.state_id = self.city_id.state_id
  69. @api.onchange('state_id')
  70. def _onchange_state_id(self):
  71. if self.state_id:
  72. self.country_id = self.state_id.country_id
  73. @api.constrains('state_id', 'country_id', 'city_id')
  74. def constrains_country(self):
  75. for rec in self:
  76. if rec.state_id and rec.state_id.country_id != \
  77. rec.country_id:
  78. raise ValidationError(_(
  79. "The country of the state differs from the country in "
  80. "location %s") % rec.name)
  81. if rec.city_id and rec.city_id.country_id \
  82. != rec.country_id:
  83. raise ValidationError(_(
  84. "The country of the city differs from the country in "
  85. "location %s") % rec.name)
  86. if rec.city_id and rec.city_id.state_id \
  87. != rec.state_id:
  88. raise ValidationError(_(
  89. "The state of the city differs from the state in "
  90. "location %s") % rec.name)