author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 14 May 2009 10:24:56 +0200 | |
branch | tls-sprint |
changeset 1801 | 672acc730ce5 |
parent 1641 | 2c80b09d8d86 |
child 1977 | 606923dff11b |
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 |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
553
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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 |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
553
diff
changeset
|
6 |
""" |
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 |
__docformat__ = "restructuredtext en" |
0 | 8 |
|
9 |
import simplejson |
|
10 |
||
11 |
from cubicweb.interfaces import IGeocodable |
|
984 | 12 |
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
|
13 |
from cubicweb.selectors import implements |
0 | 14 |
|
15 |
class GeocodingJsonView(EntityView): |
|
16 |
id = 'geocoding-json' |
|
17 |
binary = True |
|
18 |
templatable = False |
|
19 |
content_type = 'application/json' |
|
20 |
||
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
21 |
__select__ = implements(IGeocodable) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
22 |
|
0 | 23 |
def call(self): |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
24 |
zoomlevel = self.req.form.pop('zoomlevel', 8) |
0 | 25 |
extraparams = self.req.form.copy() |
26 |
extraparams.pop('vid', None) |
|
27 |
extraparams.pop('rql', None) |
|
28 |
markers = [self.build_marker_data(rowidx, extraparams) |
|
29 |
for rowidx in xrange(len(self.rset))] |
|
30 |
center = { |
|
31 |
'latitude': sum(marker['latitude'] for marker in markers) / len(markers), |
|
32 |
'longitude': sum(marker['longitude'] for marker in markers) / len(markers), |
|
33 |
} |
|
34 |
geodata = { |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
35 |
'zoomlevel': int(zoomlevel), |
0 | 36 |
'center': center, |
37 |
'markers': markers, |
|
38 |
} |
|
39 |
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
|
40 |
|
0 | 41 |
def build_marker_data(self, row, extraparams): |
42 |
entity = self.entity(row, 0) |
|
43 |
return {'latitude': entity.latitude, 'longitude': entity.longitude, |
|
44 |
'title': entity.dc_long_title(), |
|
45 |
#icon defines : (icon._url, icon.size, icon.iconAncho', icon.shadow) |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
46 |
'icon': entity.marker_icon() or (self.req.external_resource('GMARKER_ICON'), (20, 34), (4, 34), None), |
0 | 47 |
'bubbleUrl': entity.absolute_url(vid='gmap-bubble', __notemplate=1, **extraparams), |
48 |
} |
|
49 |
||
50 |
||
51 |
class GoogleMapBubbleView(EntityView): |
|
52 |
id = 'gmap-bubble' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
53 |
|
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
54 |
__select__ = implements(IGeocodable) |
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 cell_call(self, row, col): |
57 |
entity = self.entity(row, col) |
|
58 |
self.w(u'<div>%s</div>' % entity.view('oneline')) |
|
59 |
# 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
|
60 |
|
0 | 61 |
|
62 |
class GoogleMapsView(EntityView): |
|
63 |
id = 'gmap-view' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
64 |
|
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
65 |
__select__ = implements(IGeocodable) |
0 | 66 |
need_navigation = False |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
67 |
|
0 | 68 |
def call(self, gmap_key, width=400, height=400, uselabel=True, urlparams=None): |
69 |
self.req.add_js('http://maps.google.com/maps?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
|
70 |
localfile=False) |
0 | 71 |
self.req.add_js( ('cubicweb.widgets.js', 'cubicweb.gmap.js', 'gmap.utility.labeledmarker.js') ) |
72 |
rql = self.rset.printable_rql() |
|
73 |
if urlparams is None: |
|
74 |
loadurl = self.build_url(rql=rql, vid='geocoding-json') |
|
75 |
else: |
|
76 |
loadurl = self.build_url(rql=rql, vid='geocoding-json', **urlparams) |
|
77 |
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
|
78 |
u'cubicweb:wdgtype="GMapWidget" cubicweb:loadtype="auto" ' |
0 | 79 |
u'cubicweb:loadurl="%s" cubicweb:uselabel="%s"> </div>' % (width, height, loadurl, uselabel)) |
80 |
||
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
81 |
|
0 | 82 |
class GoogeMapsLegend(EntityView): |
83 |
id = 'gmap-legend' |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
84 |
|
0 | 85 |
def call(self): |
86 |
self.w(u'<ol>') |
|
87 |
for rowidx in xrange(len(self.rset)): |
|
88 |
self.w(u'<li>') |
|
89 |
self.wview('listitem', self.rset, row=rowidx, col=0) |
|
90 |
self.w(u'</li>') |
|
91 |
self.w(u'</ol>') |