author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Thu, 22 Apr 2010 19:48:04 +0000 | |
branch | stable |
changeset 5377 | 84d14ddfae13 |
parent 4451 | 65f4665233e6 |
child 5389 | 809d3b5b3d31 |
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 |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3918
diff
changeset
|
4 |
:copyright: 2001-2010 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 |
|
5377
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4451
diff
changeset
|
10 |
try: |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4451
diff
changeset
|
11 |
import json |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4451
diff
changeset
|
12 |
except ImportError: |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4451
diff
changeset
|
13 |
import simplejson as json |
0 | 14 |
|
15 |
from cubicweb.interfaces import IGeocodable |
|
984 | 16 |
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
|
17 |
from cubicweb.selectors import implements |
0 | 18 |
|
19 |
class GeocodingJsonView(EntityView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
20 |
__regid__ = 'geocoding-json' |
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
21 |
__select__ = implements(IGeocodable) |
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
22 |
|
0 | 23 |
binary = True |
24 |
templatable = False |
|
25 |
content_type = 'application/json' |
|
26 |
||
27 |
def call(self): |
|
2868
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
28 |
# 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
|
29 |
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
|
30 |
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
|
31 |
extraparams = self._cw.form.copy() |
0 | 32 |
extraparams.pop('vid', None) |
33 |
extraparams.pop('rql', None) |
|
34 |
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
|
35 |
for rowidx in xrange(len(self.cw_rset))] |
0 | 36 |
center = { |
37 |
'latitude': sum(marker['latitude'] for marker in markers) / len(markers), |
|
38 |
'longitude': sum(marker['longitude'] for marker in markers) / len(markers), |
|
39 |
} |
|
40 |
geodata = { |
|
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
41 |
'zoomlevel': int(zoomlevel), |
0 | 42 |
'center': center, |
43 |
'markers': markers, |
|
44 |
} |
|
5377
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4451
diff
changeset
|
45 |
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
|
46 |
|
0 | 47 |
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
|
48 |
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
|
49 |
icon = None |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
50 |
if hasattr(entity, 'marker_icon'): |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
51 |
icon = entity.marker_icon() |
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
52 |
else: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
53 |
icon = (self._cw.external_resource('GMARKER_ICON'), (20, 34), (4, 34), None) |
0 | 54 |
return {'latitude': entity.latitude, 'longitude': entity.longitude, |
55 |
'title': entity.dc_long_title(), |
|
56 |
#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
|
57 |
'icon': icon, |
0 | 58 |
'bubbleUrl': entity.absolute_url(vid='gmap-bubble', __notemplate=1, **extraparams), |
59 |
} |
|
60 |
||
61 |
||
62 |
class GoogleMapBubbleView(EntityView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
63 |
__regid__ = 'gmap-bubble' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
64 |
__select__ = implements(IGeocodable) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
65 |
|
0 | 66 |
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
|
67 |
entity = self.cw_rset.get_entity(row, col) |
0 | 68 |
self.w(u'<div>%s</div>' % entity.view('oneline')) |
69 |
# 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
|
70 |
|
0 | 71 |
|
72 |
class GoogleMapsView(EntityView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
73 |
__regid__ = 'gmap-view' |
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
74 |
__select__ = implements(IGeocodable) |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
75 |
|
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
76 |
paginable = False |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
77 |
|
0 | 78 |
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
|
79 |
self._cw.demote_to_html() |
2868
937890e25765
[gmap] make gmap-views more tolerant
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
80 |
# 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
|
81 |
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
|
82 |
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
|
83 |
localfile=False) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
84 |
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
|
85 |
rql = self.cw_rset.printable_rql() |
0 | 86 |
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
|
87 |
loadurl = self._cw.build_url(rql=rql, vid='geocoding-json') |
0 | 88 |
else: |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
89 |
loadurl = self._cw.build_url(rql=rql, vid='geocoding-json', **urlparams) |
0 | 90 |
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
|
91 |
u'cubicweb:wdgtype="GMapWidget" cubicweb:loadtype="auto" ' |
0 | 92 |
u'cubicweb:loadurl="%s" cubicweb:uselabel="%s"> </div>' % (width, height, loadurl, uselabel)) |
93 |
||
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
94 |
|
0 | 95 |
class GoogeMapsLegend(EntityView): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
96 |
__regid__ = 'gmap-legend' |
553
c69eac8298c0
[gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
97 |
|
0 | 98 |
def call(self): |
99 |
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
|
100 |
for rowidx in xrange(len(self.cw_rset)): |
0 | 101 |
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
|
102 |
self.wview('listitem', self.cw_rset, row=rowidx, col=0) |
0 | 103 |
self.w(u'</li>') |
104 |
self.w(u'</ol>') |