2013-08-27 10:23:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-06-03 14:34:30 +00:00
|
|
|
import json
|
|
|
|
from openerp import SUPERUSER_ID
|
2013-08-27 10:23:53 +00:00
|
|
|
from openerp.addons.web import http
|
|
|
|
from openerp.addons.web.http import request
|
2015-07-01 15:55:24 +00:00
|
|
|
from openerp.tools import html_escape as escape
|
2014-06-03 14:34:30 +00:00
|
|
|
|
2013-08-27 10:23:53 +00:00
|
|
|
|
2013-08-30 13:40:26 +00:00
|
|
|
class google_map(http.Controller):
|
2014-06-03 14:34:30 +00:00
|
|
|
'''
|
|
|
|
This class generates on-the-fly partner maps that can be reused in every
|
|
|
|
website page. To do so, just use an ``<iframe ...>`` whose ``src``
|
|
|
|
attribute points to ``/google_map`` (this controller generates a complete
|
|
|
|
HTML5 page).
|
|
|
|
|
|
|
|
URL query parameters:
|
|
|
|
- ``partner_ids``: a comma-separated list of ids (partners to be shown)
|
|
|
|
- ``partner_url``: the base-url to display the partner
|
|
|
|
(eg: if ``partner_url`` is ``/partners/``, when the user will click on
|
|
|
|
a partner on the map, it will be redirected to <myodoo>.com/partners/<id>)
|
|
|
|
|
|
|
|
In order to resize the map, simply resize the ``iframe`` with CSS
|
|
|
|
directives ``width`` and ``height``.
|
|
|
|
'''
|
2013-08-27 10:23:53 +00:00
|
|
|
|
2014-03-14 16:23:46 +00:00
|
|
|
@http.route(['/google_map'], type='http', auth="public", website=True)
|
2013-08-30 13:40:26 +00:00
|
|
|
def google_map(self, *arg, **post):
|
2014-06-03 14:34:30 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-08-27 10:23:53 +00:00
|
|
|
partner_obj = request.registry['res.partner']
|
|
|
|
|
2014-06-03 14:34:30 +00:00
|
|
|
# filter real ints from query parameters and build a domain
|
|
|
|
clean_ids = []
|
|
|
|
for s in post.get('partner_ids', "").split(","):
|
|
|
|
try:
|
|
|
|
i = int(s)
|
|
|
|
clean_ids.append(i)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2013-08-27 10:23:53 +00:00
|
|
|
|
2014-06-03 14:34:30 +00:00
|
|
|
# search for partners that can be displayed on a map
|
|
|
|
domain = [("id", "in", clean_ids), ('website_published', '=', True), ('is_company', '=', True)]
|
|
|
|
partners_ids = partner_obj.search(cr, SUPERUSER_ID, domain, context=context)
|
2013-08-27 10:23:53 +00:00
|
|
|
|
2014-06-03 14:34:30 +00:00
|
|
|
# browse and format data
|
|
|
|
partner_data = {
|
|
|
|
"counter": len(partners_ids),
|
|
|
|
"partners": []
|
|
|
|
}
|
|
|
|
request.context.update({'show_address': True})
|
|
|
|
for partner in partner_obj.browse(cr, SUPERUSER_ID, partners_ids, context=context):
|
|
|
|
partner_data["partners"].append({
|
|
|
|
'id': partner.id,
|
2015-07-01 15:55:24 +00:00
|
|
|
'name': escape(partner.name),
|
|
|
|
'address': escape('\n'.join(partner.name_get()[0][1].split('\n')[1:])),
|
|
|
|
'latitude': escape(str(partner.partner_latitude)),
|
|
|
|
'longitude': escape(str(partner.partner_longitude)),
|
2014-06-03 14:34:30 +00:00
|
|
|
})
|
2013-08-27 10:23:53 +00:00
|
|
|
|
2014-06-03 14:34:30 +00:00
|
|
|
# generate the map
|
2013-11-30 15:46:59 +00:00
|
|
|
values = {
|
2014-06-03 14:34:30 +00:00
|
|
|
'partner_url': post.get('partner_url'),
|
|
|
|
'partner_data': json.dumps(partner_data)
|
2013-11-30 15:46:59 +00:00
|
|
|
}
|
2014-06-03 14:34:30 +00:00
|
|
|
return request.website.render("website_google_map.google_map", values)
|