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.

82 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'),
  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, context=context):
  40. if bzip.name:
  41. name = [bzip.name, bzip.city]
  42. else:
  43. name = [bzip.city]
  44. if bzip.state_id:
  45. name.append(bzip.state_id.name)
  46. if bzip.country_id:
  47. name.append(bzip.country_id.name)
  48. res.append((bzip.id, ", ".join(name)))
  49. return res
  50. def onchange_state_id(self, cr, uid, ids, state_id=False, context=None):
  51. result = {}
  52. if state_id:
  53. state = self.pool['res.country.state'].browse(
  54. cr, uid, state_id, context=context)
  55. if state:
  56. result['value'] = {'country_id': state.country_id.id}
  57. return result
  58. def name_search(
  59. self, cr, uid, name, args=None, operator='ilike', context=None,
  60. limit=100
  61. ):
  62. if args is None:
  63. args = []
  64. if context is None:
  65. context = {}
  66. ids = []
  67. if name:
  68. ids = self.search(
  69. cr, uid, [('name', 'ilike', name)] + args, limit=limit)
  70. if not ids:
  71. ids = self.search(
  72. cr, uid, [('city', operator, name)] + args, limit=limit)
  73. return self.name_get(cr, uid, ids, context=context)