only kill Singleton[V]Component, keep [V]Component classes
"""Specific views for entities implementing IDownloadable:organization: Logilab:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr"""__docformat__="restructuredtext en"fromlogilab.mtconverterimportBINARY_ENCODINGS,TransformError,html_escapefromcubicweb.interfacesimportIDownloadablefromcubicweb.common.mttransformsimportENGINEfromcubicweb.common.selectorsimport(one_line_rset,score_entity,implements,match_context_prop)fromcubicweb.web.boximportEntityBoxTemplatefromcubicweb.web.viewsimportbaseviews_=unicodedefdownload_box(w,entity,title=None,label=None):req=entity.reqw(u'<div class="sideRelated">')iftitleisNone:title=req._('download')w(u'<div class="sideBoxTitle downloadBoxTitle"><span>%s</span></div>'%html_escape(title))w(u'<div class="sideBox downloadBox"><div class="sideBoxBody">')w(u'<a href="%s"><img src="%s" alt="%s"/> %s</a>'%(html_escape(entity.download_url()),req.external_resource('DOWNLOAD_ICON'),_('download icon'),html_escape(labelorentity.dc_title())))w(u'</div>')w(u'</div>\n</div>\n')classDownloadBox(EntityBoxTemplate):id='download_box'__selectors__=(one_line_rset,implements(IDownloadable),match_context_prop)order=10defcell_call(self,row,col,title=None,label=None,**kwargs):entity=self.entity(row,col)download_box(self.w,entity,title,label)classDownloadView(baseviews.EntityView):"""this view is replacing the deprecated 'download' controller and allow downloading of entities providing the necessary interface """id='download'__selectors__=(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.complete_entity(0)encoding=entity.download_encoding()ifencodinginBINARY_ENCODINGS:contenttype='application/%s'%encodingencoding=Noneelse:contenttype=entity.download_content_type()self.req.set_content_type(contenttypeorself.content_type,filename=entity.download_file_name(),encoding=encoding)defcall(self):self.w(self.complete_entity(0).download_data())classDownloadLinkView(baseviews.EntityView):"""view displaying a link to download the file"""id='downloadlink'__selectors__=(implements(IDownloadable),)title=None# should not be listed in possible viewsdefcell_call(self,row,col,title=None,**kwargs):entity=self.entity(row,col)url=html_escape(entity.download_url())self.w(u'<a href="%s">%s</a>'%(url,html_escape(titleorentity.dc_title())))classIDownloadablePrimaryView(baseviews.PrimaryView):__selectors__=(implements(IDownloadable),)#skip_attrs = ('eid', 'data',) # XXXdefrender_entity_title(self,entity):self.w(u'<h1>%s%s</h1>'%(entity.dc_type().capitalize(),html_escape(entity.dc_title())))defrender_entity_attributes(self,entity,siderelations):super(IDownloadablePrimaryView,self).render_entity_attributes(entity,siderelations)self.wview('downloadlink',entity.rset,title=self.req._('download'),row=entity.row)self.w(u'<div class="content">')contenttype=entity.download_content_type()ifcontenttype.startswith('image/'):self.wview('image',entity.rset,row=entity.row)else:try:ifENGINE.has_input(contenttype):self.w(entity.printable_value('data'))exceptTransformError:passexceptException,ex:msg=self.req._("can't display data, unexpected error: %s")%exself.w('<div class="error">%s</div>'%msg)self.w(u'</div>')defis_side_related(self,rschema,eschema):"""display all relations as side related"""returnTrueclassIDownloadableLineView(baseviews.OneLineView):__selectors__=(implements(IDownloadable),)defcell_call(self,row,col,title=None,**kwargs):"""the secondary view is a link to download the file"""entity=self.entity(row,col)url=html_escape(entity.absolute_url())name=html_escape(entity.download_file_name())durl=html_escape(entity.download_url())self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]'%(url,name,durl,self.req._('download')))defis_image(entity):mt=entity.download_content_type()ifnot(mtandmt.startswith('image/')):return0return1classImageView(baseviews.EntityView):id='image'__selectors__=(implements(IDownloadable),score_entity(is_image))title=_('image')defcall(self):rset=self.rsetforiinxrange(len(rset)):self.w(u'<div class="efile">')self.wview(self.id,rset,row=i,col=0)self.w(u'</div>')defcell_call(self,row,col):entity=self.entity(row,col)#if entity.data_format.startswith('image/'):self.w(u'<img src="%s" alt="%s"/>'%(html_escape(entity.download_url()),html_escape(entity.download_file_name())))