author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 21 Jun 2010 15:29:10 +0200 | |
branch | stable |
changeset 5812 | d970049d7cfd |
parent 5424 | 8ecbcbff9777 |
child 5444 | f7fdb5dd82f6 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
553
diff
changeset
|
18 |
"""Specific views for entities implementing IGeocodable |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
553
diff
changeset
|
19 |
|
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
553
diff
changeset
|
20 |
""" |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
553
diff
changeset
|
21 |
__docformat__ = "restructuredtext en" |
0 | 22 |
|
23 |
from cubicweb.interfaces import IGeocodable |
|
984 | 24 |
from cubicweb.view import EntityView |
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
553
diff
changeset
|
25 |
from cubicweb.selectors import implements |
5389
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
26 |
from cubicweb.web import json |
0 | 27 |
|
28 |
class GeocodingJsonView(EntityView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
29 |
__regid__ = 'geocoding-json' |
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
30 |
__select__ = implements(IGeocodable) |
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
31 |
|
0 | 32 |
binary = True |
33 |
templatable = False |
|
34 |
content_type = 'application/json' |
|
35 |
||
36 |
def call(self): |
|
2868
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
37 |
# remove entities that don't define latitude and longitude |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
38 |
self.cw_rset = self.cw_rset.filtered_rset(lambda e: e.latitude and e.longitude) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
39 |
zoomlevel = self._cw.form.pop('zoomlevel', 8) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
40 |
extraparams = self._cw.form.copy() |
0 | 41 |
extraparams.pop('vid', None) |
42 |
extraparams.pop('rql', None) |
|
43 |
markers = [self.build_marker_data(rowidx, extraparams) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
44 |
for rowidx in xrange(len(self.cw_rset))] |
0 | 45 |
center = { |
46 |
'latitude': sum(marker['latitude'] for marker in markers) / len(markers), |
|
47 |
'longitude': sum(marker['longitude'] for marker in markers) / len(markers), |
|
48 |
} |
|
49 |
geodata = { |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
50 |
'zoomlevel': int(zoomlevel), |
0 | 51 |
'center': center, |
52 |
'markers': markers, |
|
53 |
} |
|
5377
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4451
diff
changeset
|
54 |
self.w(json.dumps(geodata)) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
55 |
|
0 | 56 |
def build_marker_data(self, row, extraparams): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
57 |
entity = self.cw_rset.get_entity(row, 0) |
2868
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
58 |
icon = None |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
59 |
if hasattr(entity, 'marker_icon'): |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
60 |
icon = entity.marker_icon() |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
61 |
else: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
62 |
icon = (self._cw.external_resource('GMARKER_ICON'), (20, 34), (4, 34), None) |
0 | 63 |
return {'latitude': entity.latitude, 'longitude': entity.longitude, |
64 |
'title': entity.dc_long_title(), |
|
65 |
#icon defines : (icon._url, icon.size, icon.iconAncho', icon.shadow) |
|
2868
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
66 |
'icon': icon, |
0 | 67 |
'bubbleUrl': entity.absolute_url(vid='gmap-bubble', __notemplate=1, **extraparams), |
68 |
} |
|
69 |
||
70 |
||
71 |
class GoogleMapBubbleView(EntityView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
72 |
__regid__ = 'gmap-bubble' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
73 |
__select__ = implements(IGeocodable) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
74 |
|
0 | 75 |
def cell_call(self, row, col): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
76 |
entity = self.cw_rset.get_entity(row, col) |
0 | 77 |
self.w(u'<div>%s</div>' % entity.view('oneline')) |
78 |
# FIXME: we should call something like address-view if available |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
79 |
|
0 | 80 |
|
81 |
class GoogleMapsView(EntityView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
82 |
__regid__ = 'gmap-view' |
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
83 |
__select__ = implements(IGeocodable) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
84 |
|
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
85 |
paginable = False |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
86 |
|
0 | 87 |
def call(self, gmap_key, width=400, height=400, uselabel=True, urlparams=None): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
88 |
self._cw.demote_to_html() |
2868
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
89 |
# remove entities that don't define latitude and longitude |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
90 |
self.cw_rset = self.cw_rset.filtered_rset(lambda e: e.latitude and e.longitude) |
3998
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
91 |
self._cw.add_js('http://maps.google.com/maps?sensor=false&file=api&v=2&key=%s' % gmap_key, |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
984
diff
changeset
|
92 |
localfile=False) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
93 |
self._cw.add_js( ('cubicweb.widgets.js', 'cubicweb.gmap.js', 'gmap.utility.labeledmarker.js') ) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
94 |
rql = self.cw_rset.printable_rql() |
0 | 95 |
if urlparams is None: |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
96 |
loadurl = self._cw.build_url(rql=rql, vid='geocoding-json') |
0 | 97 |
else: |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
98 |
loadurl = self._cw.build_url(rql=rql, vid='geocoding-json', **urlparams) |
0 | 99 |
self.w(u'<div style="width: %spx; height: %spx;" class="widget gmap" ' |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
100 |
u'cubicweb:wdgtype="GMapWidget" cubicweb:loadtype="auto" ' |
0 | 101 |
u'cubicweb:loadurl="%s" cubicweb:uselabel="%s"> </div>' % (width, height, loadurl, uselabel)) |
102 |
||
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
103 |
|
0 | 104 |
class GoogeMapsLegend(EntityView): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
105 |
__regid__ = 'gmap-legend' |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
106 |
|
0 | 107 |
def call(self): |
108 |
self.w(u'<ol>') |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
109 |
for rowidx in xrange(len(self.cw_rset)): |
0 | 110 |
self.w(u'<li>') |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
111 |
self.wview('listitem', self.cw_rset, row=rowidx, col=0) |
0 | 112 |
self.w(u'</li>') |
113 |
self.w(u'</ol>') |