web/views/idownloadable.py
branchtls-sprint
changeset 985 6a25c58a1c23
parent 742 99115e029dca
parent 957 b1f68f725fe9
child 987 0b39f59530fa
equal deleted inserted replaced
984:536e421b082b 985:6a25c58a1c23
    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 
       
    20 def is_image(entity):
       
    21     mt = entity.download_content_type()
       
    22     if not (mt and mt.startswith('image/')):
       
    23         return 0
       
    24     return 1
       
    25 
    20 def download_box(w, entity, title=None, label=None):
    26 def download_box(w, entity, title=None, label=None):
    21     req = entity.req
    27     req = entity.req
    22     w(u'<div class="sideRelated">')
    28     w(u'<div class="sideRelated">')
    23     if title is None:
    29     if title is None:
    24         title = req._('download')
    30         title = req._('download')
    30          req.external_resource('DOWNLOAD_ICON'),
    36          req.external_resource('DOWNLOAD_ICON'),
    31          _('download icon'), html_escape(label or entity.dc_title())))
    37          _('download icon'), html_escape(label or entity.dc_title())))
    32     w(u'</div>')
    38     w(u'</div>')
    33     w(u'</div>\n</div>\n')
    39     w(u'</div>\n</div>\n')
    34 
    40 
    35     
    41 
    36 class DownloadBox(EntityBoxTemplate):
    42 class DownloadBox(EntityBoxTemplate):
    37     id = 'download_box'
    43     id = 'download_box'
    38     __select__ = (one_line_rset() & implements(IDownloadable) & match_context_prop())
    44     # no download box for images
       
    45     # XXX primary_view selector ?
       
    46     __select__ = (one_line_rset() & implements(IDownloadable) & match_context_prop() & ~ score_entity(is_image)
    39     order = 10
    47     order = 10
       
    48     
    40     def cell_call(self, row, col, title=None, label=None, **kwargs):
    49     def cell_call(self, row, col, title=None, label=None, **kwargs):
    41         entity = self.entity(row, col)
    50         entity = self.entity(row, col)
    42         download_box(self.w, entity, title, label)
    51         download_box(self.w, entity, title, label)
    43 
    52 
    44 
    53 
    85 
    94 
    86 
    95 
    87                                                                                 
    96                                                                                 
    88 class IDownloadablePrimaryView(baseviews.PrimaryView):
    97 class IDownloadablePrimaryView(baseviews.PrimaryView):
    89     __select__ = implements(IDownloadable)
    98     __select__ = implements(IDownloadable)
    90     #skip_attrs = ('eid', 'data',) # XXX
    99     # XXX File/Image attributes but this is not specified in the IDownloadable interface
       
   100     skip_attrs = baseviews.PrimaryView.skip_attrs + ('data', 'name')
    91 
   101 
    92     def render_entity_title(self, entity):
   102     def render_entity_title(self, entity):
    93         self.w(u'<h1>%s %s</h1>'
   103         self.w(u'<h1>%s %s</h1>'
    94                % (entity.dc_type().capitalize(),
   104                % (entity.dc_type().capitalize(),
    95                   html_escape(entity.dc_title())))
   105                   html_escape(entity.dc_title())))
    96     
   106     
    97     def render_entity_attributes(self, entity, siderelations):
   107     def render_entity_attributes(self, entity, siderelations):
    98         super(IDownloadablePrimaryView, self).render_entity_attributes(entity, siderelations)
   108         super(IDownloadablePrimaryView, self).render_entity_attributes(entity, siderelations)
    99         self.wview('downloadlink', entity.rset, title=self.req._('download'), row=entity.row)
       
   100         self.w(u'<div class="content">')
   109         self.w(u'<div class="content">')
   101         contenttype = entity.download_content_type()
   110         contenttype = entity.download_content_type()
   102         if contenttype.startswith('image/'):
   111         if contenttype.startswith('image/'):
   103             self.wview('image', entity.rset, row=entity.row)
   112             self.wview('image', entity.rset, row=entity.row)
   104         else:
   113         else:
       
   114             self.wview('downloadlink', entity.rset, title=self.req._('download'), row=entity.row)
   105             try:
   115             try:
   106                 if ENGINE.has_input(contenttype):
   116                 if ENGINE.has_input(contenttype):
   107                     self.w(entity.printable_value('data'))
   117                     self.w(entity.printable_value('data'))
   108             except TransformError:
   118             except TransformError:
   109                 pass
   119                 pass
   128         durl = html_escape(entity.download_url())
   138         durl = html_escape(entity.download_url())
   129         self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' %
   139         self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' %
   130                (url, name, durl, self.req._('download')))
   140                (url, name, durl, self.req._('download')))
   131 
   141 
   132 
   142 
   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     
       
   139 class ImageView(baseviews.EntityView):
   143 class ImageView(baseviews.EntityView):
   140     id = 'image'
   144     id = 'image'
   141     __select__ = implements(IDownloadable) & score_entity(is_image)
   145     __select__ = implements(IDownloadable) & score_entity(is_image)
   142     
   146     
   143     title = _('image')
   147     title = _('image')