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.

72 lines
2.9 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. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp.osv import orm, fields
  23. class BetterZip(orm.Model):
  24. " City/locations completion object"
  25. _name = "res.better.zip"
  26. _description = __doc__
  27. _order = "priority"
  28. _columns = {'priority': fields.integer('Priority', deprecated=True),
  29. 'name': fields.char('ZIP', required=True),
  30. 'city': fields.char('City', required=True),
  31. 'state_id': fields.many2one('res.country.state', 'State'),
  32. 'country_id': fields.many2one('res.country', 'Country'),
  33. 'code': fields.char('City Code', size=64,
  34. help="The official code for the city"),
  35. }
  36. _defaults = {'priority': 100}
  37. def name_get(self, cursor, uid, ids, context=None):
  38. res = []
  39. for bzip in self.browse(cursor, uid, ids):
  40. name = [bzip.name, bzip.city]
  41. if bzip.state_id:
  42. name.append(bzip.state_id.name)
  43. if bzip.country_id:
  44. name.append(bzip.country_id.name)
  45. res.append((bzip.id, ", ".join(name)))
  46. return res
  47. def onchange_state_id(self, cr, uid, ids, state_id=False, context={}):
  48. result = {}
  49. if state_id:
  50. state = self.pool['res.country.state'].browse(cr, uid, state_id, context=context)
  51. if state:
  52. result['value'] = {'country_id': state.country_id.id}
  53. return result
  54. def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
  55. if args is None:
  56. args = []
  57. if context is None:
  58. context = {}
  59. ids = []
  60. if name:
  61. ids = self.search(cr, uid, [('name', 'ilike', name)] + args, limit=limit)
  62. if not ids:
  63. ids = self.search(cr, uid, [('city', operator, name)] + args, limit=limit)
  64. return self.name_get(cr, uid, ids, context=context)