web/views/idownloadable.py
branchtls-sprint
changeset 631 99f5852f8604
parent 431 18b4dd650ef8
child 692 800592b8d39b
equal deleted inserted replaced
630:66ff0b2f7d03 631:99f5852f8604
     1 """Specific views for entities implementing IDownloadable
     1 """Specific views for entities implementing IDownloadable
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2001-2008 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 
     8 
     9 from logilab.mtconverter import BINARY_ENCODINGS, TransformError, html_escape
     9 from logilab.mtconverter import BINARY_ENCODINGS, TransformError, html_escape
    10 
    10 
    11 from cubicweb.interfaces import IDownloadable
    11 from cubicweb.interfaces import IDownloadable
    12 from cubicweb.common.mttransforms import ENGINE
    12 from cubicweb.common.mttransforms import ENGINE
    13 from cubicweb.common.selectors import (one_line_rset, score_entity_selector,
    13 from cubicweb.common.selectors import (one_line_rset, score_entity,
    14                                        implement_interface, match_context_prop)
    14                                        implements, match_context_prop)
    15 from cubicweb.web.box import EntityBoxTemplate
    15 from cubicweb.web.box import EntityBoxTemplate
    16 from cubicweb.web.views import baseviews
    16 from cubicweb.web.views import baseviews
    17 
    17 
    18 _ = unicode
    18 _ = unicode
    19 
    19 
    33     w(u'</div>\n</div>\n')
    33     w(u'</div>\n</div>\n')
    34 
    34 
    35     
    35     
    36 class DownloadBox(EntityBoxTemplate):
    36 class DownloadBox(EntityBoxTemplate):
    37     id = 'download_box'
    37     id = 'download_box'
    38     __selectors__ = (one_line_rset, implement_interface, match_context_prop)
    38     __selectors__ = (one_line_rset, implements(IDownloadable), match_context_prop)
    39     accepts_interfaces = (IDownloadable,)
       
    40     order = 10
    39     order = 10
    41     def cell_call(self, row, col, title=None, label=None, **kwargs):
    40     def cell_call(self, row, col, title=None, label=None, **kwargs):
    42         entity = self.entity(row, col)
    41         entity = self.entity(row, col)
    43         download_box(self.w, entity, title, label)
    42         download_box(self.w, entity, title, label)
    44 
    43 
    45 
    44 
    46 class DownloadView(baseviews.EntityView):
    45 class DownloadView(baseviews.EntityView):
    47     """this view is replacing the deprecated 'download' controller and allow downloading
    46     """this view is replacing the deprecated 'download' controller and allow
    48     of entities providing the necessary interface
    47     downloading of entities providing the necessary interface
    49     """
    48     """
    50     id = 'download'
    49     id = 'download'
    51     __selectors__ = (one_line_rset, implement_interface)
    50     __selectors__ = (one_line_rset, implements(IDownloadable))
    52     accepts_interfaces = (IDownloadable,)
       
    53 
    51 
    54     templatable = False
    52     templatable = False
    55     content_type = 'application/octet-stream'
    53     content_type = 'application/octet-stream'
    56     binary = True
    54     binary = True
    57     add_to_breadcrumbs = False
    55     add_to_breadcrumbs = False
    74 
    72 
    75 
    73 
    76 class DownloadLinkView(baseviews.EntityView):
    74 class DownloadLinkView(baseviews.EntityView):
    77     """view displaying a link to download the file"""
    75     """view displaying a link to download the file"""
    78     id = 'downloadlink'
    76     id = 'downloadlink'
       
    77     __selectors__ = (implements(IDownloadable),)
    79     title = None # should not be listed in possible views
    78     title = None # should not be listed in possible views
    80     __selectors__ = (implement_interface,)
       
    81 
    79 
    82     accepts_interfaces = (IDownloadable,)
       
    83     
    80     
    84     def cell_call(self, row, col, title=None, **kwargs):
    81     def cell_call(self, row, col, title=None, **kwargs):
    85         entity = self.entity(row, col)
    82         entity = self.entity(row, col)
    86         url = html_escape(entity.download_url())
    83         url = html_escape(entity.download_url())
    87         self.w(u'<a href="%s">%s</a>' % (url, html_escape(title or entity.dc_title())))
    84         self.w(u'<a href="%s">%s</a>' % (url, html_escape(title or entity.dc_title())))
    88 
    85 
    89 
    86 
    90                                                                                 
    87                                                                                 
    91 class IDownloadablePrimaryView(baseviews.PrimaryView):
    88 class IDownloadablePrimaryView(baseviews.PrimaryView):
    92     __selectors__ = (implement_interface,)
    89     __selectors__ = (implements(IDownloadable),)
    93     #skip_attrs = ('eid', 'data',) # XXX
    90     #skip_attrs = ('eid', 'data',) # XXX
    94     accepts_interfaces = (IDownloadable,)
       
    95 
    91 
    96     def render_entity_title(self, entity):
    92     def render_entity_title(self, entity):
    97         self.w(u'<h1>%s %s</h1>'
    93         self.w(u'<h1>%s %s</h1>'
    98                % (entity.dc_type().capitalize(),
    94                % (entity.dc_type().capitalize(),
    99                   html_escape(entity.dc_title())))
    95                   html_escape(entity.dc_title())))
   120         """display all relations as side related"""
   116         """display all relations as side related"""
   121         return True
   117         return True
   122 
   118 
   123 
   119 
   124 class IDownloadableLineView(baseviews.OneLineView):
   120 class IDownloadableLineView(baseviews.OneLineView):
   125     __selectors__ = (implement_interface,)
   121     __selectors__ = (implements(IDownloadable),)
   126     # don't kick default oneline view
       
   127     accepts_interfaces = (IDownloadable,)
       
   128     
       
   129 
   122 
   130     def cell_call(self, row, col, title=None, **kwargs):
   123     def cell_call(self, row, col, title=None, **kwargs):
   131         """the secondary view is a link to download the file"""
   124         """the secondary view is a link to download the file"""
   132         entity = self.entity(row, col)
   125         entity = self.entity(row, col)
   133         url = html_escape(entity.absolute_url())
   126         url = html_escape(entity.absolute_url())
   135         durl = html_escape(entity.download_url())
   128         durl = html_escape(entity.download_url())
   136         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>]' %
   137                (url, name, durl, self.req._('download')))
   130                (url, name, durl, self.req._('download')))
   138 
   131 
   139 
   132 
       
   133 def is_image(entity):
       
   134     mt = entity.download_content_type()
       
   135     if not (mt and mt.startswith('image/')):
       
   136         return 0
       
   137     return 1
       
   138     
   140 class ImageView(baseviews.EntityView):
   139 class ImageView(baseviews.EntityView):
   141     __selectors__ = (implement_interface, score_entity_selector)
       
   142     id = 'image'
   140     id = 'image'
       
   141     __selectors__ = (implements(IDownloadable),
       
   142                      score_entity(is_image))
       
   143     
   143     title = _('image')
   144     title = _('image')
   144     accepts_interfaces = (IDownloadable,)
       
   145     
   145     
   146     def call(self):
   146     def call(self):
   147         rset = self.rset
   147         rset = self.rset
   148         for i in xrange(len(rset)):
   148         for i in xrange(len(rset)):
   149             self.w(u'<div class="efile">')
   149             self.w(u'<div class="efile">')
   150             self.wview(self.id, rset, row=i, col=0)
   150             self.wview(self.id, rset, row=i, col=0)
   151             self.w(u'</div>')
   151             self.w(u'</div>')
   152 
       
   153     @classmethod
       
   154     def score_entity(cls, entity):
       
   155         mt = entity.download_content_type()
       
   156         if not (mt and mt.startswith('image/')):
       
   157             return 0
       
   158         return 1
       
   159     
   152     
   160     def cell_call(self, row, col):
   153     def cell_call(self, row, col):
   161         entity = self.entity(row, col)
   154         entity = self.entity(row, col)
   162         #if entity.data_format.startswith('image/'):
   155         #if entity.data_format.startswith('image/'):
   163         self.w(u'<img src="%s" alt="%s"/>' % (html_escape(entity.download_url()),
   156         self.w(u'<img src="%s" alt="%s"/>' % (html_escape(entity.download_url()),