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