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.

76 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Partner Address on Map module for OpenERP
  5. # Copyright (C) 2015 Akretion (http://www.akretion.com/)
  6. # @author: Alexis de Lattre <alexis.delattre@akretion.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 import models, fields, api, _
  23. from openerp.exceptions import Warning
  24. class MapUrl(models.Model):
  25. _name = 'map.url'
  26. _description = 'Map System'
  27. name = fields.Char(string='Map Provider', required=True)
  28. url = fields.Char(string='URL', required=True)
  29. class ResUsers(models.Model):
  30. _inherit = 'res.users'
  31. # begin with context_ to allow user to change it by himself
  32. context_map_url_id = fields.Many2one(
  33. 'map.url', string='Map Provider')
  34. # called from post-install script
  35. # I can't use a default method on the field, because it would be executed
  36. # before loading map_url_data.xml, so it would not be able to set a value
  37. @api.model
  38. def _default_map_url(self):
  39. users = self.env['res.users'].search([])
  40. map_url = self.env['map.url'].search([], limit=1)
  41. users.write({'context_map_url_id': map_url.id})
  42. class ResPartner(models.Model):
  43. _inherit = 'res.partner'
  44. @api.multi
  45. def open_map(self):
  46. if not self.env.user.context_map_url_id:
  47. raise Warning(
  48. _('Missing map provider: '
  49. 'you should set it in your preferences.'))
  50. url = self.env.user.context_map_url_id.url
  51. if self.street:
  52. url += self.street
  53. if self.street2:
  54. url += ' ' + self.street2
  55. if self.city:
  56. url += ' ' + self.city
  57. if self.state_id:
  58. url += ' ' + self.state_id.name
  59. if self.country_id:
  60. url += ' ' + self.country_id.name
  61. return {
  62. 'type': 'ir.actions.act_url',
  63. 'url': url,
  64. 'target': 'new',
  65. }