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.

67 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. #
  4. # Author: Nicolas Bessi. Copyright Camptocamp SA
  5. # Contributor: Pedro Manuel Baeza <pedro.baeza@serviciosbaeza.com>
  6. # Ignacio Ibeas <ignacio@acysos.com>
  7. # Alejandro Santana <alejandrosantana@anubia.es>
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License as
  11. # published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. from openerp import models, fields, api
  23. class BetterZip(models.Model):
  24. '''City/locations completion object'''
  25. _name = "res.better.zip"
  26. _description = __doc__
  27. _order = "name asc"
  28. name = fields.Char('ZIP')
  29. code = fields.Char('City Code', size=64,
  30. help="The official code for the city")
  31. city = fields.Char('City', required=True)
  32. state_id = fields.Many2one('res.country.state', 'State')
  33. country_id = fields.Many2one('res.country', 'Country')
  34. @api.multi
  35. def name_get(self):
  36. res = []
  37. for bzip in self:
  38. if bzip.name:
  39. name = [bzip.name, bzip.city]
  40. else:
  41. name = [bzip.city]
  42. if bzip.state_id:
  43. name.append(bzip.state_id.name)
  44. if bzip.country_id:
  45. name.append(bzip.country_id.name)
  46. res.append((bzip.id, ", ".join(name)))
  47. return res
  48. @api.onchange('state_id')
  49. def onchange_state_id(self):
  50. if self.state_id:
  51. self.country_id = self.state_id.country_id
  52. @api.model
  53. def name_search(self, name, args=None, operator='ilike', limit=100):
  54. args = args or []
  55. recs = self.browse()
  56. if name:
  57. recs = self.search([('name', 'ilike', name)] + args, limit=limit)
  58. if not recs:
  59. recs = self.search([('city', operator, name)] + args, limit=limit)
  60. return recs.name_get()