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.

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