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.

111 lines
4.2 KiB

9 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Alexis de Lattre <alexis.delattre@akretion.com>
  3. # © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from openerp import models, api, _
  6. from openerp.exceptions import Warning as UserError
  7. import logging
  8. logger = logging.getLogger(__name__)
  9. class ResPartner(models.Model):
  10. _inherit = 'res.partner'
  11. @api.multi
  12. def _address_as_string(self):
  13. self.ensure_one()
  14. addr = []
  15. if self.street:
  16. addr.append(self.street)
  17. if self.street2:
  18. addr.append(self.street2)
  19. if self.city:
  20. addr.append(self.city)
  21. if self.state_id:
  22. addr.append(self.state_id.name)
  23. if self.country_id:
  24. addr.append(self.country_id.name)
  25. if not addr:
  26. raise UserError(_("Address missing on partner '%s'.") % self.name)
  27. return ' '.join(addr)
  28. @api.model
  29. def _prepare_url(self, url, replace):
  30. assert url, 'Missing URL'
  31. for key, value in replace.iteritems():
  32. if not isinstance(value, (str, unicode)):
  33. # for latitude and longitude which are floats
  34. value = unicode(value)
  35. url = url.replace(key, value)
  36. logger.debug('Final URL: %s', url)
  37. return url
  38. @api.multi
  39. def open_map(self):
  40. self.ensure_one()
  41. map_website = self.env.user.context_map_website_id
  42. if not map_website:
  43. raise UserError(
  44. _('Missing map provider: '
  45. 'you should set it in your preferences.'))
  46. if (map_website.lat_lon_url and hasattr(self, 'partner_latitude') and
  47. self.partner_latitude and self.partner_longitude):
  48. url = self._prepare_url(
  49. map_website.lat_lon_url, {
  50. '{LATITUDE}': self.partner_latitude,
  51. '{LONGITUDE}': self.partner_longitude})
  52. else:
  53. if not map_website.address_url:
  54. raise UserError(
  55. _("Missing parameter 'URL that uses the address' "
  56. "for map website '%s'.") % map_website.name)
  57. url = self._prepare_url(
  58. map_website.address_url,
  59. {'{ADDRESS}': self._address_as_string()})
  60. return {
  61. 'type': 'ir.actions.act_url',
  62. 'url': url,
  63. 'target': 'new',
  64. }
  65. @api.multi
  66. def open_route_map(self):
  67. self.ensure_one()
  68. if not self.env.user.context_route_map_website_id:
  69. raise UserError(
  70. _('Missing route map website: '
  71. 'you should set it in your preferences.'))
  72. map_website = self.env.user.context_route_map_website_id
  73. if not self.env.user.context_route_start_partner_id:
  74. raise UserError(
  75. _('Missing start address for route map: '
  76. 'you should set it in your preferences.'))
  77. start_partner = self.env.user.context_route_start_partner_id
  78. if (map_website.route_lat_lon_url and
  79. hasattr(self, 'partner_latitude') and
  80. self.partner_latitude and self.partner_longitude and
  81. start_partner.partner_latitude and
  82. start_partner.partner_longitude):
  83. url = self._prepare_url( # pragma: no cover
  84. map_website.route_lat_lon_url, {
  85. '{START_LATITUDE}': start_partner.partner_latitude,
  86. '{START_LONGITUDE}': start_partner.partner_longitude,
  87. '{DEST_LATITUDE}': self.partner_latitude,
  88. '{DEST_LONGITUDE}': self.partner_longitude})
  89. else:
  90. if not map_website.route_address_url:
  91. raise UserError(
  92. _("Missing route URL that uses the addresses "
  93. "for the map website '%s'") % map_website.name)
  94. url = self._prepare_url(
  95. map_website.route_address_url, {
  96. '{START_ADDRESS}': start_partner._address_as_string(),
  97. '{DEST_ADDRESS}': self._address_as_string()})
  98. return {
  99. 'type': 'ir.actions.act_url',
  100. 'url': url,
  101. 'target': 'new',
  102. }