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.

71 lines
2.8 KiB

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