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.

139 lines
4.8 KiB

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