"""Specific views for entities implementing IGeocodable:organization: Logilab:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses"""__docformat__="restructuredtext en"importsimplejsonfromcubicweb.interfacesimportIGeocodablefromcubicweb.viewimportEntityViewfromcubicweb.selectorsimportimplementsclassGeocodingJsonView(EntityView):__regid__='geocoding-json'__select__=implements(IGeocodable)binary=Truetemplatable=Falsecontent_type='application/json'defcall(self):# remove entities that don't define latitude and longitudeself.cw_rset=self.cw_rset.filtered_rset(lambdae:e.latitudeande.longitude)zoomlevel=self._cw.form.pop('zoomlevel',8)extraparams=self._cw.form.copy()extraparams.pop('vid',None)extraparams.pop('rql',None)markers=[self.build_marker_data(rowidx,extraparams)forrowidxinxrange(len(self.cw_rset))]center={'latitude':sum(marker['latitude']formarkerinmarkers)/len(markers),'longitude':sum(marker['longitude']formarkerinmarkers)/len(markers),}geodata={'zoomlevel':int(zoomlevel),'center':center,'markers':markers,}self.w(simplejson.dumps(geodata))defbuild_marker_data(self,row,extraparams):entity=self.cw_rset.get_entity(row,0)icon=Noneifhasattr(entity,'marker_icon'):icon=entity.marker_icon()else:icon=(self._cw.external_resource('GMARKER_ICON'),(20,34),(4,34),None)return{'latitude':entity.latitude,'longitude':entity.longitude,'title':entity.dc_long_title(),#icon defines : (icon._url, icon.size, icon.iconAncho', icon.shadow)'icon':icon,'bubbleUrl':entity.absolute_url(vid='gmap-bubble',__notemplate=1,**extraparams),}classGoogleMapBubbleView(EntityView):__regid__='gmap-bubble'__select__=implements(IGeocodable)defcell_call(self,row,col):entity=self.cw_rset.get_entity(row,col)self.w(u'<div>%s</div>'%entity.view('oneline'))# FIXME: we should call something like address-view if availableclassGoogleMapsView(EntityView):__regid__='gmap-view'__select__=implements(IGeocodable)paginable=Falsedefcall(self,gmap_key,width=400,height=400,uselabel=True,urlparams=None):self._cw.demote_to_html()# remove entities that don't define latitude and longitudeself.cw_rset=self.cw_rset.filtered_rset(lambdae:e.latitudeande.longitude)self._cw.add_js('http://maps.google.com/maps?sensor=false&file=api&v=2&key=%s'%gmap_key,localfile=False)self._cw.add_js(('cubicweb.widgets.js','cubicweb.gmap.js','gmap.utility.labeledmarker.js'))rql=self.cw_rset.printable_rql()ifurlparamsisNone:loadurl=self._cw.build_url(rql=rql,vid='geocoding-json')else:loadurl=self._cw.build_url(rql=rql,vid='geocoding-json',**urlparams)self.w(u'<div style="width: %spx; height: %spx;" class="widget gmap" 'u'cubicweb:wdgtype="GMapWidget" cubicweb:loadtype="auto" 'u'cubicweb:loadurl="%s" cubicweb:uselabel="%s"> </div>'%(width,height,loadurl,uselabel))classGoogeMapsLegend(EntityView):__regid__='gmap-legend'defcall(self):self.w(u'<ol>')forrowidxinxrange(len(self.cw_rset)):self.w(u'<li>')self.wview('listitem',self.cw_rset,row=rowidx,col=0)self.w(u'</li>')self.w(u'</ol>')