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.

193 lines
7.6 KiB

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