[doc/book] some fixes for vregistry, selectors & appobjects
"""Specific views for entities implementing IDownloadable: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"_=unicodefromlogilab.mtconverterimportBINARY_ENCODINGS,TransformError,xml_escapefromcubicweb.viewimportEntityViewfromcubicweb.selectorsimport(one_line_rset,score_entity,implements,match_context_prop)fromcubicweb.interfacesimportIDownloadablefromcubicweb.mttransformsimportENGINEfromcubicweb.web.boximportEntityBoxTemplatefromcubicweb.web.viewsimportprimary,baseviewsdefis_image(entity):mt=entity.download_content_type()ifnot(mtandmt.startswith('image/')):return0return1defdownload_box(w,entity,title=None,label=None,footer=u''):req=entity._cww(u'<div class="sideBox">')iftitleisNone:title=req._('download')w(u'<div class="sideBoxTitle downloadBoxTitle"><span>%s</span></div>'%xml_escape(title))w(u'<div class="sideBox downloadBox"><div class="sideBoxBody">')w(u'<a href="%s"><img src="%s" alt="%s"/> %s</a>'%(xml_escape(entity.download_url()),req.external_resource('DOWNLOAD_ICON'),_('download icon'),xml_escape(labelorentity.dc_title())))w(u'%s</div>'%footer)w(u'</div></div>\n')classDownloadBox(EntityBoxTemplate):__regid__='download_box'# no download box for images# XXX primary_view selector ?__select__=(one_line_rset()&implements(IDownloadable)&match_context_prop()&~score_entity(is_image))order=10defcell_call(self,row,col,title=None,label=None,**kwargs):entity=self.cw_rset.get_entity(row,col)download_box(self.w,entity,title,label)classDownloadView(EntityView):"""this view is replacing the deprecated 'download' controller and allow downloading of entities providing the necessary interface """__regid__='download'__select__=one_line_rset()&implements(IDownloadable)templatable=Falsecontent_type='application/octet-stream'binary=Trueadd_to_breadcrumbs=Falsedefset_request_content_type(self):"""overriden to set the correct filetype and filename"""entity=self.cw_rset.complete_entity(0,0)encoding=entity.download_encoding()ifencodinginBINARY_ENCODINGS:contenttype='application/%s'%encodingencoding=Noneelse:contenttype=entity.download_content_type()self._cw.set_content_type(contenttypeorself.content_type,filename=entity.download_file_name(),encoding=encoding)defcall(self):self.w(self.cw_rset.complete_entity(0,0).download_data())classDownloadLinkView(EntityView):"""view displaying a link to download the file"""__regid__='downloadlink'__select__=implements(IDownloadable)title=None# should not be listed in possible viewsdefcell_call(self,row,col,title=None,**kwargs):entity=self.cw_rset.get_entity(row,col)url=xml_escape(entity.download_url())self.w(u'<a href="%s">%s</a>'%(url,xml_escape(titleorentity.dc_title())))classIDownloadablePrimaryView(primary.PrimaryView):__select__=implements(IDownloadable)defrender_entity_attributes(self,entity):super(IDownloadablePrimaryView,self).render_entity_attributes(entity)self.w(u'<div class="content">')contenttype=entity.download_content_type()ifcontenttype.startswith('image/'):self.wview('image',entity.cw_rset,row=entity.cw_row)else:self.wview('downloadlink',entity.cw_rset,title=self._cw._('download'),row=entity.cw_row)try:ifENGINE.has_input(contenttype):self.w(entity.printable_value('data'))exceptTransformError:passexceptException,ex:msg=self._cw._("can't display data, unexpected error: %s") \%xml_escape(str(ex))self.w('<div class="error">%s</div>'%msg)self.w(u'</div>')classIDownloadableLineView(baseviews.OneLineView):__select__=implements(IDownloadable)defcell_call(self,row,col,title=None,**kwargs):"""the oneline view is a link to download the file"""entity=self.cw_rset.get_entity(row,col)url=xml_escape(entity.absolute_url())name=xml_escape(titleorentity.download_file_name())durl=xml_escape(entity.download_url())self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]'%(url,name,durl,self._cw._('download')))classImageView(EntityView):__regid__='image'__select__=implements(IDownloadable)&score_entity(is_image)title=_('image')defcall(self):rset=self.cw_rsetforiinxrange(len(rset)):self.w(u'<div class="efile">')self.wview(self.__regid__,rset,row=i,col=0)self.w(u'</div>')defcell_call(self,row,col,width=None,height=None,link=False):entity=self.cw_rset.get_entity(row,col)#if entity.data_format.startswith('image/'):imgtag=u'<img src="%s" alt="%s" '%(xml_escape(entity.download_url()),(self._cw._('download %s')%xml_escape(entity.download_file_name())))ifwidth:imgtag+=u'width="%i" '%widthifheight:imgtag+=u'height="%i" '%heightimgtag+=u'/>'iflink:self.w(u'<a href="%s">%s</a>'%(entity.absolute_url(vid='download'),imgtag))else:self.w(imgtag)