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.

68 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 = "priority"
  28. priority = fields.Integer('Priority', default=100, deprecated=True)
  29. name = fields.Char('ZIP')
  30. code = fields.Char('City Code', size=64,
  31. help="The official code for the city")
  32. city = fields.Char('City', required=True)
  33. state_id = fields.Many2one('res.country.state', 'State')
  34. country_id = fields.Many2one('res.country', 'Country')
  35. @api.multi
  36. def name_get(self):
  37. res = []
  38. for bzip in self:
  39. if bzip.name:
  40. name = [bzip.name, bzip.city]
  41. else:
  42. name = [bzip.city]
  43. if bzip.state_id:
  44. name.append(bzip.state_id.name)
  45. if bzip.country_id:
  46. name.append(bzip.country_id.name)
  47. res.append((bzip.id, ", ".join(name)))
  48. return res
  49. @api.onchange('state_id')
  50. def onchange_state_id(self):
  51. if self.state_id:
  52. self.country_id = self.state_id.country_id
  53. @api.model
  54. def name_search(self, name, args=None, operator='ilike', limit=100):
  55. args = args or []
  56. recs = self.browse()
  57. if name:
  58. recs = self.search([('name', 'ilike', name)] + args, limit=limit)
  59. if not recs:
  60. recs = self.search([('city', operator, name)] + args, limit=limit)
  61. return recs.name_get()