web/views/idownloadable.py
branchtls-sprint
changeset 1639 375c857aa0f5
parent 1554 3a3263df6cdd
child 1885 c2011d238e98
equal deleted inserted replaced
1638:6f9003a32ecc 1639:375c857aa0f5
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
       
     8 _ = unicode
     8 
     9 
     9 from logilab.mtconverter import BINARY_ENCODINGS, TransformError, html_escape
    10 from logilab.mtconverter import BINARY_ENCODINGS, TransformError, html_escape
    10 
    11 
       
    12 from cubicweb.view import EntityView
    11 from cubicweb.selectors import (one_line_rset, score_entity,
    13 from cubicweb.selectors import (one_line_rset, score_entity,
    12                                 implements, match_context_prop)
    14                                 implements, match_context_prop)
    13 from cubicweb.interfaces import IDownloadable
    15 from cubicweb.interfaces import IDownloadable
    14 from cubicweb.common.mttransforms import ENGINE
    16 from cubicweb.common.mttransforms import ENGINE
    15 from cubicweb.web.box import EntityBoxTemplate
    17 from cubicweb.web.box import EntityBoxTemplate
    16 from cubicweb.web.views import baseviews
    18 from cubicweb.web.views import primary, baseviews
    17 
    19 
    18 _ = unicode
       
    19 
    20 
    20 def is_image(entity):
    21 def is_image(entity):
    21     mt = entity.download_content_type()
    22     mt = entity.download_content_type()
    22     if not (mt and mt.startswith('image/')):
    23     if not (mt and mt.startswith('image/')):
    23         return 0
    24         return 0
    50     def cell_call(self, row, col, title=None, label=None, **kwargs):
    51     def cell_call(self, row, col, title=None, label=None, **kwargs):
    51         entity = self.entity(row, col)
    52         entity = self.entity(row, col)
    52         download_box(self.w, entity, title, label)
    53         download_box(self.w, entity, title, label)
    53 
    54 
    54 
    55 
    55 class DownloadView(baseviews.EntityView):
    56 class DownloadView(EntityView):
    56     """this view is replacing the deprecated 'download' controller and allow
    57     """this view is replacing the deprecated 'download' controller and allow
    57     downloading of entities providing the necessary interface
    58     downloading of entities providing the necessary interface
    58     """
    59     """
    59     id = 'download'
    60     id = 'download'
    60     __select__ = one_line_rset() & implements(IDownloadable)
    61     __select__ = one_line_rset() & implements(IDownloadable)
    79 
    80 
    80     def call(self):
    81     def call(self):
    81         self.w(self.complete_entity(0).download_data())
    82         self.w(self.complete_entity(0).download_data())
    82 
    83 
    83 
    84 
    84 class DownloadLinkView(baseviews.EntityView):
    85 class DownloadLinkView(EntityView):
    85     """view displaying a link to download the file"""
    86     """view displaying a link to download the file"""
    86     id = 'downloadlink'
    87     id = 'downloadlink'
    87     __select__ = implements(IDownloadable)
    88     __select__ = implements(IDownloadable)
    88     title = None # should not be listed in possible views
    89     title = None # should not be listed in possible views
    89 
    90 
    92         entity = self.entity(row, col)
    93         entity = self.entity(row, col)
    93         url = html_escape(entity.download_url())
    94         url = html_escape(entity.download_url())
    94         self.w(u'<a href="%s">%s</a>' % (url, html_escape(title or entity.dc_title())))
    95         self.w(u'<a href="%s">%s</a>' % (url, html_escape(title or entity.dc_title())))
    95 
    96 
    96 
    97 
    97 class IDownloadablePrimaryView(baseviews.PrimaryView):
    98 class IDownloadablePrimaryView(primary.PrimaryView):
    98     __select__ = implements(IDownloadable)
    99     __select__ = implements(IDownloadable)
    99 
   100 
   100     def render_entity_attributes(self, entity):
   101     def render_entity_attributes(self, entity):
   101         super(IDownloadablePrimaryView, self).render_entity_attributes(entity)
   102         super(IDownloadablePrimaryView, self).render_entity_attributes(entity)
   102         self.w(u'<div class="content">')
   103         self.w(u'<div class="content">')
   127         durl = html_escape(entity.download_url())
   128         durl = html_escape(entity.download_url())
   128         self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' %
   129         self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' %
   129                (url, name, durl, self.req._('download')))
   130                (url, name, durl, self.req._('download')))
   130 
   131 
   131 
   132 
   132 class ImageView(baseviews.EntityView):
   133 class ImageView(EntityView):
   133     id = 'image'
   134     id = 'image'
   134     __select__ = implements(IDownloadable) & score_entity(is_image)
   135     __select__ = implements(IDownloadable) & score_entity(is_image)
   135 
   136 
   136     title = _('image')
   137     title = _('image')
   137 
   138