[testlib] simplify code by using a class attribute
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""Specific views for entities implementing IGeocodable"""__docformat__="restructuredtext en"fromcubicweb.interfacesimportIGeocodablefromcubicweb.viewimportEntityView,EntityAdapter,implements_adapter_compatfromcubicweb.selectorsimportimplements,adaptablefromcubicweb.utilsimportjson_dumpsclassIGeocodableAdapter(EntityAdapter):"""interface required by geocoding views such as gmap-view"""__needs_bw_compat__=True__regid__='IGeocodable'__select__=implements(IGeocodable,warn=False)# XXX for bw compat, should be abstract@property@implements_adapter_compat('IGeocodable')deflatitude(self):"""returns the latitude of the entity"""raiseNotImplementedError@property@implements_adapter_compat('IGeocodable')deflongitude(self):"""returns the longitude of the entity"""raiseNotImplementedError@implements_adapter_compat('IGeocodable')defmarker_icon(self):"""returns the icon that should be used as the marker. an icon is defined by a 4-uple: (icon._url, icon.size, icon.iconAnchor, icon.shadow) """return(self._cw.uiprops['GMARKER_ICON'],(20,34),(4,34),None)classGeocodingJsonView(EntityView):__regid__='geocoding-json'__select__=adaptable('IGeocodable')binary=Truetemplatable=Falsecontent_type='application/json'defcall(self):zoomlevel=self._cw.form.pop('zoomlevel',8)extraparams=self._cw.form.copy()extraparams.pop('vid',None)extraparams.pop('rql',None)markers=[]forentityinself.cw_rset.entities():igeocodable=entity.cw_adapt_to('IGeocodable')# remove entities that don't define latitude and longitudeifnot(igeocodable.latitudeandigeocodable.longitude):continuemarkers.append(self.build_marker_data(entity,igeocodable,extraparams))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(json_dumps(geodata))defbuild_marker_data(self,entity,igeocodable,extraparams):return{'latitude':igeocodable.latitude,'longitude':igeocodable.longitude,'icon':igeocodable.marker_icon(),'title':entity.dc_long_title(),'bubbleUrl':entity.absolute_url(vid='gmap-bubble',__notemplate=1,**extraparams),}classGoogleMapBubbleView(EntityView):__regid__='gmap-bubble'__select__=adaptable('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__=adaptable('IGeocodable')paginable=Falsedefcall(self,gmap_key,width=400,height=400,uselabel=True,urlparams=None):self._cw.demote_to_html()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>')