author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Thu, 03 Dec 2009 09:50:51 +0100 | |
branch | stable |
changeset 3991 | a0aa9789c6bc |
parent 3918 | 0b7390acca7f |
child 3998 | 94cc7cad3d2d |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
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
|
1 |
"""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
|
2 |
|
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
553
diff
changeset
|
3 |
:organization: Logilab |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1641
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
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
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1641
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - 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
|
7 |
""" |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
553
diff
changeset
|
8 |
__docformat__ = "restructuredtext en" |
0 | 9 |
|
10 |
import simplejson |
|
11 |
||
12 |
from cubicweb.interfaces import IGeocodable |
|
984 | 13 |
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
|
14 |
from cubicweb.selectors import implements |
0 | 15 |
|
16 |
class GeocodingJsonView(EntityView): |
|
17 |
id = 'geocoding-json' |
|
18 |
binary = True |
|
19 |
templatable = False |
|
20 |
content_type = 'application/json' |
|
21 |
||
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
22 |
__select__ = implements(IGeocodable) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
23 |
|
0 | 24 |
def call(self): |
2868
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
25 |
# remove entities that don't define latitude and longitude |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
26 |
self.rset = self.rset.filtered_rset(lambda e: e.latitude and e.longitude) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
27 |
zoomlevel = self.req.form.pop('zoomlevel', 8) |
0 | 28 |
extraparams = self.req.form.copy() |
29 |
extraparams.pop('vid', None) |
|
30 |
extraparams.pop('rql', None) |
|
31 |
markers = [self.build_marker_data(rowidx, extraparams) |
|
32 |
for rowidx in xrange(len(self.rset))] |
|
33 |
center = { |
|
34 |
'latitude': sum(marker['latitude'] for marker in markers) / len(markers), |
|
35 |
'longitude': sum(marker['longitude'] for marker in markers) / len(markers), |
|
36 |
} |
|
37 |
geodata = { |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
38 |
'zoomlevel': int(zoomlevel), |
0 | 39 |
'center': center, |
40 |
'markers': markers, |
|
41 |
} |
|
42 |
self.w(simplejson.dumps(geodata)) |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
43 |
|
0 | 44 |
def build_marker_data(self, row, extraparams): |
45 |
entity = self.entity(row, 0) |
|
2868
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
46 |
icon = None |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
47 |
if hasattr(entity, 'marker_icon'): |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
48 |
icon = entity.marker_icon() |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
49 |
else: |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
50 |
icon = (self.req.external_resource('GMARKER_ICON'), (20, 34), (4, 34), None) |
0 | 51 |
return {'latitude': entity.latitude, 'longitude': entity.longitude, |
52 |
'title': entity.dc_long_title(), |
|
53 |
#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
|
54 |
'icon': icon, |
0 | 55 |
'bubbleUrl': entity.absolute_url(vid='gmap-bubble', __notemplate=1, **extraparams), |
56 |
} |
|
57 |
||
58 |
||
59 |
class GoogleMapBubbleView(EntityView): |
|
60 |
id = 'gmap-bubble' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
61 |
|
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
62 |
__select__ = implements(IGeocodable) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
63 |
|
0 | 64 |
def cell_call(self, row, col): |
65 |
entity = self.entity(row, col) |
|
66 |
self.w(u'<div>%s</div>' % entity.view('oneline')) |
|
67 |
# 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
|
68 |
|
0 | 69 |
|
70 |
class GoogleMapsView(EntityView): |
|
71 |
id = 'gmap-view' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
72 |
|
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) |
0 | 74 |
need_navigation = False |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
75 |
|
0 | 76 |
def call(self, gmap_key, width=400, height=400, uselabel=True, urlparams=None): |
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2868
diff
changeset
|
77 |
self.req.demote_to_html() |
2868
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
78 |
# remove entities that don't define latitude and longitude |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
79 |
self.rset = self.rset.filtered_rset(lambda e: e.latitude and e.longitude) |
3918
0b7390acca7f
[views] fix gmap url / avoid double escaping
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3094
diff
changeset
|
80 |
self.req.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
|
81 |
localfile=False) |
0 | 82 |
self.req.add_js( ('cubicweb.widgets.js', 'cubicweb.gmap.js', 'gmap.utility.labeledmarker.js') ) |
83 |
rql = self.rset.printable_rql() |
|
84 |
if urlparams is None: |
|
85 |
loadurl = self.build_url(rql=rql, vid='geocoding-json') |
|
86 |
else: |
|
87 |
loadurl = self.build_url(rql=rql, vid='geocoding-json', **urlparams) |
|
88 |
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
|
89 |
u'cubicweb:wdgtype="GMapWidget" cubicweb:loadtype="auto" ' |
0 | 90 |
u'cubicweb:loadurl="%s" cubicweb:uselabel="%s"> </div>' % (width, height, loadurl, uselabel)) |
91 |
||
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
92 |
|
0 | 93 |
class GoogeMapsLegend(EntityView): |
94 |
id = 'gmap-legend' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
95 |
|
0 | 96 |
def call(self): |
97 |
self.w(u'<ol>') |
|
98 |
for rowidx in xrange(len(self.rset)): |
|
99 |
self.w(u'<li>') |
|
100 |
self.wview('listitem', self.rset, row=rowidx, col=0) |
|
101 |
self.w(u'</li>') |
|
102 |
self.w(u'</ol>') |