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.

197 lines
8.0 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Partner External Maps module for Odoo
  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. import logging
  25. logger = logging.getLogger(__name__)
  26. class MapWebsite(models.Model):
  27. _name = 'map.website'
  28. _description = 'Map Website'
  29. name = fields.Char(string='Map Website Name', required=True)
  30. address_url = fields.Char(
  31. string='URL that uses the address',
  32. help="In this URL, {ADDRESS} will be replaced by the address.")
  33. lat_lon_url = fields.Char(
  34. string='URL that uses latitude and longitude',
  35. help="In this URL, {LATITUDE} and {LONGITUDE} will be replaced by "
  36. "the latitude and longitude (requires the module 'base_geolocalize')")
  37. route_address_url = fields.Char(
  38. string='Route URL that uses the addresses',
  39. help="In this URL, {START_ADDRESS} and {DEST_ADDRESS} will be "
  40. "replaced by the start and destination addresses.")
  41. route_lat_lon_url = fields.Char(
  42. string='Route URL that uses latitude and longitude',
  43. help="In this URL, {START_LATITUDE}, {START_LONGITUDE}, "
  44. "{DEST_LATITUDE} and {DEST_LONGITUDE} will be replaced by the "
  45. "latitude and longitude of the start and destination adresses "
  46. "(requires the module 'base_geolocalize').")
  47. class ResUsers(models.Model):
  48. _inherit = 'res.users'
  49. # begin with context_ to allow user to change it by himself
  50. context_map_website_id = fields.Many2one(
  51. 'map.website', string='Map Website')
  52. # We want to give the possibility to the user to have one map provider for
  53. # regular maps and another one for routing
  54. context_route_map_website_id = fields.Many2one(
  55. 'map.website', string='Route Map Website',
  56. domain=[
  57. '|', ('address_url', '!=', False), ('lat_lon_url', '!=', False)],
  58. help="Map provided used when you click on the car icon on the partner "
  59. "form to display an itinerary.")
  60. context_route_start_partner_id = fields.Many2one(
  61. 'res.partner', string='Start Address for Route Map',
  62. domain=[
  63. '|',
  64. ('route_address_url', '!=', False),
  65. ('route_lat_lon_url', '!=', False)])
  66. @api.model
  67. def _default_map_settings(self):
  68. '''Method called from post-install script
  69. I can't use a default method on the field, because it would be executed
  70. before loading map_website_data.xml, so it would not be able to set a
  71. value'''
  72. users = self.env['res.users'].search([])
  73. map_website = self.env['map.website'].search([
  74. '|', ('address_url', '!=', False), ('lat_lon_url', '!=', False)],
  75. limit=1)
  76. map_route_website = self.env['map.website'].search([
  77. '|',
  78. ('route_address_url', '!=', False),
  79. ('route_lat_lon_url', '!=', False)], limit=1)
  80. logger.info('Updating user settings for maps...')
  81. for user in users:
  82. user.write({
  83. 'context_map_website_id': map_website.id or False,
  84. 'context_route_map_website_id': map_route_website.id or False,
  85. 'context_route_start_partner_id': user.partner_id.id or False,
  86. })
  87. class ResPartner(models.Model):
  88. _inherit = 'res.partner'
  89. @api.model
  90. def _address_as_string(self):
  91. addr = u''
  92. if self.street:
  93. addr += self.street
  94. if self.street2:
  95. addr += u' ' + self.street2
  96. if self.city:
  97. addr += u' ' + self.city
  98. if self.state_id:
  99. addr += u' ' + self.state_id.name
  100. if self.country_id:
  101. addr += u' ' + self.country_id.name
  102. if not addr:
  103. raise Warning(
  104. _("Address missing on partner '%s'.") % self.name)
  105. return addr
  106. @api.model
  107. def _prepare_url(self, url, replace):
  108. assert url, 'Missing URL'
  109. for key, value in replace.iteritems():
  110. if not isinstance(value, (str, unicode)):
  111. # for latitude and longitude which are floats
  112. value = unicode(value)
  113. url = url.replace(key, value)
  114. logger.debug('Final URL: %s', url)
  115. return url
  116. @api.multi
  117. def open_map(self):
  118. if not self.env.user.context_map_website_id:
  119. raise Warning(
  120. _('Missing map provider: '
  121. 'you should set it in your preferences.'))
  122. map_website = self.env.user.context_map_website_id
  123. if (
  124. map_website.lat_lon_url and
  125. hasattr(self, 'partner_latitude') and
  126. self.partner_latitude and self.partner_longitude):
  127. url = self._prepare_url(
  128. map_website.lat_lon_url, {
  129. '{LATITUDE}': self.partner_latitude,
  130. '{LONGITUDE}': self.partner_longitude})
  131. else:
  132. if not map_website.address_url:
  133. raise Warning(
  134. _("Missing parameter 'URL that uses the address' "
  135. "for map website '%s'.") % map_website.name)
  136. url = self._prepare_url(
  137. map_website.address_url,
  138. {'{ADDRESS}': self._address_as_string()})
  139. return {
  140. 'type': 'ir.actions.act_url',
  141. 'url': url,
  142. 'target': 'new',
  143. }
  144. @api.multi
  145. def open_route_map(self):
  146. if not self.env.user.context_route_map_website_id:
  147. raise Warning(
  148. _('Missing route map website: '
  149. 'you should set it in your preferences.'))
  150. map_website = self.env.user.context_route_map_website_id
  151. if not self.env.user.context_route_start_partner_id:
  152. raise Warning(
  153. _('Missing start address for route map: '
  154. 'you should set it in your preferences.'))
  155. start_partner = self.env.user.context_route_start_partner_id
  156. if (
  157. map_website.route_lat_lon_url and
  158. hasattr(self, 'partner_latitude') and
  159. self.partner_latitude and
  160. self.partner_longitude and
  161. start_partner.partner_latitude and
  162. start_partner.partner_longitude):
  163. url = self._prepare_url(
  164. map_website.route_lat_lon_url, {
  165. '{START_LATITUDE}': start_partner.partner_latitude,
  166. '{START_LONGITUDE}': start_partner.partner_longitude,
  167. '{DEST_LATITUDE}': self.partner_latitude,
  168. '{DEST_LONGITUDE}': self.partner_longitude})
  169. else:
  170. if not map_website.route_address_url:
  171. raise Warning(
  172. _("Missing route URL that uses the addresses "
  173. "for the map website '%s'") % map_website.name)
  174. url = self._prepare_url(
  175. map_website.route_address_url, {
  176. '{START_ADDRESS}': start_partner._address_as_string(),
  177. '{DEST_ADDRESS}': self._address_as_string()})
  178. return {
  179. 'type': 'ir.actions.act_url',
  180. 'url': url,
  181. 'target': 'new',
  182. }