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.

83 lines
3.0 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. # Franco Tampieri <franco.tampieri@abstarct.it>
  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. #
  23. from openerp.osv import orm, fields
  24. class BetterZip(orm.Model):
  25. " City/locations completion object"
  26. _name = "res.better.zip"
  27. _description = __doc__
  28. _order = "priority"
  29. _columns = {'priority': fields.integer('Priority', deprecated=True),
  30. 'name': fields.char('ZIP'),
  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. 'code': fields.char('City Code', size=64,
  35. help="The official code for the city"),
  36. }
  37. _defaults = {'priority': 100}
  38. def name_get(self, cursor, uid, ids, context=None):
  39. res = []
  40. for bzip in self.browse(cursor, uid, ids, context=context):
  41. if bzip.name:
  42. name = [bzip.name, bzip.city]
  43. else:
  44. name = [bzip.city]
  45. if bzip.state_id:
  46. name.append(bzip.state_id.name)
  47. if bzip.country_id:
  48. name.append(bzip.country_id.name)
  49. res.append((bzip.id, ", ".join(name)))
  50. return res
  51. def onchange_state_id(self, cr, uid, ids, state_id=False, context=None):
  52. result = {}
  53. if state_id:
  54. state = self.pool['res.country.state'].browse(
  55. cr, uid, state_id, context=context)
  56. if state:
  57. result['value'] = {'country_id': state.country_id.id}
  58. return result
  59. def name_search(
  60. self, cr, uid, name, args=None, operator='ilike', context=None,
  61. limit=100
  62. ):
  63. if args is None:
  64. args = []
  65. if context is None:
  66. context = {}
  67. ids = []
  68. if name:
  69. ids = self.search(
  70. cr, uid, [('name', 'ilike', name)] + args, limit=limit)
  71. if not ids:
  72. ids = self.search(
  73. cr, uid, [('city', operator, name)] + args, limit=limit)
  74. return self.name_get(cr, uid, ids, context=context)