diff -r 000000000000 -r b97547f5f1fa web/views/idownloadable.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/views/idownloadable.py Wed Nov 05 15:52:50 2008 +0100 @@ -0,0 +1,154 @@ +"""Specific views for entities implementing IDownloadable + +:organization: Logilab +:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr +""" +__docformat__ = "restructuredtext en" + +from logilab.mtconverter import BINARY_ENCODINGS, TransformError, html_escape + +from cubicweb.interfaces import IDownloadable +from cubicweb.common.mttransforms import ENGINE +from cubicweb.common.selectors import (onelinerset_selector, score_entity_selector, + interface_selector) +from cubicweb.web.views import baseviews + +_ = unicode + + +def download_box(w, entity): + w(u'
\n') + +class DownloadView(baseviews.EntityView): + """this view is replacing the deprecated 'download' controller and allow downloading + of entities providing the necessary interface + """ + id = 'download' + __selectors__ = (onelinerset_selector, interface_selector) + accepts_interfaces = (IDownloadable,) + + templatable = False + content_type = 'application/octet-stream' + binary = True + add_to_breadcrumbs = False + + def set_request_content_type(self): + """overriden to set the correct filetype and filename""" + entity = self.complete_entity(0) + encoding = entity.download_encoding() + if encoding in BINARY_ENCODINGS: + contenttype = 'application/%s' % encoding + encoding = None + else: + contenttype = entity.download_content_type() + self.req.set_content_type(contenttype or self.content_type, + filename=entity.download_file_name(), + encoding=encoding) + + def call(self): + self.w(self.complete_entity(0).download_data()) + + +class DownloadLinkView(baseviews.EntityView): + """view displaying a link to download the file""" + id = 'downloadlink' + title = None # should not be listed in possible views + __selectors__ = (interface_selector,) + + accepts_interfaces = (IDownloadable,) + + def cell_call(self, row, col, title=None, **kwargs): + entity = self.entity(row, col) + url = html_escape(entity.download_url()) + self.w(u'%s' % (url, html_escape(title or entity.dc_title()))) + + + +class IDownloadablePrimaryView(baseviews.PrimaryView): + __selectors__ = (interface_selector,) + #skip_attrs = ('eid', 'data',) # XXX + accepts_interfaces = (IDownloadable,) + + def render_entity_title(self, entity): + self.w(u'