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.

108 lines
3.5 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.model
  48. def name_search(self, name='', args=None, operator='ilike', limit=100):
  49. args = list(args or [])
  50. args += ['|', ('city', operator, name),
  51. '|', ('name', operator, name), ('code', operator, name)]
  52. recs = self.search(args, limit=limit)
  53. return recs.name_get()
  54. @api.onchange('country_id')
  55. def _onchange_country_id(self):
  56. if self.state_id.country_id != self.country_id:
  57. self.state_id = False
  58. if self.city_id.country_id != self.country_id:
  59. self.city_id = False
  60. if self.country_id:
  61. domain = [('country_id', '=', self.country_id.id)]
  62. else:
  63. domain = []
  64. return {
  65. 'domain': {
  66. 'state_id': domain,
  67. 'city_id': domain,
  68. }
  69. }
  70. @api.onchange('city_id')
  71. def _onchange_city_id(self):
  72. if self.city_id:
  73. self.city = self.city_id.name
  74. self.country_id = self.city_id.country_id
  75. self.state_id = self.city_id.state_id
  76. @api.onchange('state_id')
  77. def _onchange_state_id(self):
  78. if self.state_id:
  79. self.country_id = self.state_id.country_id
  80. @api.constrains('state_id', 'country_id', 'city_id')
  81. def constrains_country(self):
  82. for rec in self:
  83. if rec.state_id and rec.state_id.country_id != \
  84. rec.country_id:
  85. raise ValidationError(_(
  86. "The country of the state differs from the country in "
  87. "location %s") % rec.name)
  88. if rec.city_id and rec.city_id.country_id \
  89. != rec.country_id:
  90. raise ValidationError(_(
  91. "The country of the city differs from the country in "
  92. "location %s") % rec.name)
  93. if rec.city_id and rec.city_id.state_id \
  94. != rec.state_id:
  95. raise ValidationError(_(
  96. "The state of the city differs from the state in "
  97. "location %s") % rec.name)