合并企业版代码(未测试,先提交到测试分支)
This commit is contained in:
9
web_map/models/__init__.py
Normal file
9
web_map/models/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from . import ir_action
|
||||
from . import ir_ui_view
|
||||
from . import res_partner
|
||||
from . import res_config_settings
|
||||
from . import models
|
||||
from . import ir_http
|
||||
12
web_map/models/ir_action.py
Normal file
12
web_map/models/ir_action.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class ActWindowView(models.Model):
|
||||
_inherit = 'ir.actions.act_window.view'
|
||||
|
||||
view_mode = fields.Selection(selection_add=[
|
||||
('map', "Map")
|
||||
], ondelete={'map': 'cascade'})
|
||||
16
web_map/models/ir_http.py
Normal file
16
web_map/models/ir_http.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import models
|
||||
|
||||
|
||||
class IrHttp(models.AbstractModel):
|
||||
_inherit = 'ir.http'
|
||||
|
||||
def session_info(self):
|
||||
result = super(IrHttp, self).session_info()
|
||||
if self.env.user.has_group('base.group_user'):
|
||||
result.update(
|
||||
map_box_token = self.env['ir.config_parameter'].sudo().get_param('web_map.token_map_box',False)
|
||||
)
|
||||
return result
|
||||
10
web_map/models/ir_ui_view.py
Normal file
10
web_map/models/ir_ui_view.py
Normal file
@@ -0,0 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class View(models.Model):
|
||||
_inherit = 'ir.ui.view'
|
||||
|
||||
type = fields.Selection(selection_add=[('map', "Map")])
|
||||
21
web_map/models/models.py
Normal file
21
web_map/models/models.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import _, api, models
|
||||
from lxml.builder import E
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class Base(models.AbstractModel):
|
||||
_inherit = 'base'
|
||||
|
||||
@api.model
|
||||
def _get_default_map_view(self):
|
||||
view = E.map()
|
||||
|
||||
if 'partner_id' in self._fields:
|
||||
view.set('res_partner', 'partner_id')
|
||||
else:
|
||||
raise UserError(_("You need to set a Contact field on this model to use the Map View"))
|
||||
|
||||
return view
|
||||
45
web_map/models/res_config_settings.py
Normal file
45
web_map/models/res_config_settings.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import fields, models,api, _
|
||||
from odoo.exceptions import UserError
|
||||
import requests
|
||||
from odoo.http import request
|
||||
|
||||
|
||||
class ResConfigSettings(models.TransientModel):
|
||||
_inherit = 'res.config.settings'
|
||||
|
||||
map_box_token = fields.Char(config_parameter='web_map.token_map_box',string = 'Token Map Box', help='Necessary for some functionalities in the map view', copy=True, default='', store=True)
|
||||
|
||||
@api.onchange('map_box_token')
|
||||
def _onchange_map_box_token(self):
|
||||
if not self.map_box_token:
|
||||
return
|
||||
map_box_token = self.env['ir.config_parameter'].get_param('web_map.token_map_box')
|
||||
if self.map_box_token == map_box_token:
|
||||
return
|
||||
|
||||
url = 'https://api.mapbox.com/directions/v5/mapbox/driving/-73.989%2C40.733%3B-74%2C40.733'
|
||||
headers = {
|
||||
'referer': request.httprequest.headers.environ.get('HTTP_REFERER'),
|
||||
}
|
||||
params = {
|
||||
'access_token': self.map_box_token,
|
||||
'steps': 'true',
|
||||
'geometries': 'geojson',
|
||||
}
|
||||
try:
|
||||
result = requests.head(url=url, headers=headers, params=params, timeout=5)
|
||||
error_code = result.status_code
|
||||
except requests.exceptions.RequestException:
|
||||
error_code = 500
|
||||
if error_code == 200:
|
||||
return
|
||||
self.map_box_token = ''
|
||||
if error_code == 401:
|
||||
return {'warning': {'message': _('The token input is not valid')}}
|
||||
elif error_code == 403:
|
||||
return {'warning': {'message': _('This referer is not authorized')}}
|
||||
elif error_code == 500:
|
||||
return {'warning': {'message': _('The MapBox server is unreachable')}}
|
||||
51
web_map/models/res_partner.py
Normal file
51
web_map/models/res_partner.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class ResPartner(models.Model):
|
||||
_name = 'res.partner'
|
||||
_inherit = 'res.partner'
|
||||
|
||||
contact_address_complete = fields.Char(compute='_compute_complete_address', store=True)
|
||||
|
||||
@api.model
|
||||
def update_latitude_longitude(self, partners):
|
||||
partners_data = defaultdict(list)
|
||||
|
||||
for partner in partners:
|
||||
if 'id' in partner and 'partner_latitude' in partner and 'partner_longitude' in partner:
|
||||
partners_data[(partner['partner_latitude'], partner['partner_longitude'])].append(partner['id'])
|
||||
|
||||
for values, partner_ids in partners_data.items():
|
||||
# NOTE this should be done in sudo to avoid crashing as soon as the view is used
|
||||
self.browse(partner_ids).sudo().write({
|
||||
'partner_latitude': values[0],
|
||||
'partner_longitude': values[1],
|
||||
})
|
||||
|
||||
return {}
|
||||
|
||||
@api.onchange('street', 'zip', 'city', 'state_id', 'country_id')
|
||||
def _delete_coordinates(self):
|
||||
self.partner_latitude = False
|
||||
self.partner_longitude = False
|
||||
|
||||
@api.depends('street', 'zip', 'city', 'country_id')
|
||||
def _compute_complete_address(self):
|
||||
for record in self:
|
||||
record.contact_address_complete = ''
|
||||
if record.street:
|
||||
record.contact_address_complete += record.street + ', '
|
||||
if record.zip:
|
||||
record.contact_address_complete += record.zip + ' '
|
||||
if record.city:
|
||||
record.contact_address_complete += record.city + ', '
|
||||
if record.state_id:
|
||||
record.contact_address_complete += record.state_id.name + ', '
|
||||
if record.country_id:
|
||||
record.contact_address_complete += record.country_id.name
|
||||
record.contact_address_complete = record.contact_address_complete.strip().strip(',')
|
||||
Reference in New Issue
Block a user