web/views/idownloadable.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 19 Oct 2010 13:19:50 +0200
changeset 6548 fd36c1049eed
parent 6141 b8287e54b528
child 6632 78878f5a8166
permissions -rw-r--r--
[css] cleanup and fix by removing display: block on <a> in boxes also, add class a.action to ease action links like [+] in boxes like bookmark box without having to refine css per box id.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    18
"""Specific views for entities adapting to IDownloadable"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    21
_ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2136
diff changeset
    23
from logilab.mtconverter import BINARY_ENCODINGS, TransformError, xml_escape
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
5987
a9d508973433 [images] use tags.img and allow to add arbitrary attributes on the image. Use it to set a class when image is the primary entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5986
diff changeset
    25
from cubicweb import tags
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    26
from cubicweb.view import EntityView
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5868
diff changeset
    27
from cubicweb.selectors import (one_line_rset, is_instance, match_context_prop,
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    28
                                adaptable, has_mimetype)
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3890
diff changeset
    29
from cubicweb.mttransforms import ENGINE
6141
b8287e54b528 [web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6140
diff changeset
    30
from cubicweb.web import component, httpcache
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    31
from cubicweb.web.views import primary, baseviews
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
3846
1a7d10864628 new argument to provide extra content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3789
diff changeset
    34
def download_box(w, entity, title=None, label=None, footer=u''):
3460
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    35
    req = entity._cw
1885
c2011d238e98 replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1639
diff changeset
    36
    w(u'<div class="sideBox">')
249
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    37
    if title is None:
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    38
        title = req._('download')
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    39
    w(u'<div class="sideBoxTitle downloadBoxTitle"><span>%s</span></div>'
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2136
diff changeset
    40
      % xml_escape(title))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    w(u'<div class="sideBox downloadBox"><div class="sideBoxBody">')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    w(u'<a href="%s"><img src="%s" alt="%s"/> %s</a>'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    43
      % (xml_escape(entity.cw_adapt_to('IDownloadable').download_url()),
5467
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
    44
         req.uiprops['DOWNLOAD_ICON'],
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    45
         req._('download icon'), xml_escape(label or entity.dc_title())))
3846
1a7d10864628 new argument to provide extra content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3789
diff changeset
    46
    w(u'%s</div>' % footer)
1a7d10864628 new argument to provide extra content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3789
diff changeset
    47
    w(u'</div></div>\n')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
985
6a25c58a1c23 backport stable branch, take care a lot of conflicts occured, this may be the revision you're looking for...
sylvain.thenault@logilab.fr
parents: 742 957
diff changeset
    49
6141
b8287e54b528 [web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6140
diff changeset
    50
class DownloadBox(component.EntityCtxComponent):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3072
diff changeset
    51
    __regid__ = 'download_box'
985
6a25c58a1c23 backport stable branch, take care a lot of conflicts occured, this may be the revision you're looking for...
sylvain.thenault@logilab.fr
parents: 742 957
diff changeset
    52
    # no download box for images
6141
b8287e54b528 [web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6140
diff changeset
    53
    __select__ = (component.EntityCtxComponent.__select__ &
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    54
                  adaptable('IDownloadable') & ~has_mimetype('image/'))
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    55
249
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    56
    order = 10
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    57
    title = _('download')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
    58
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    59
    def render_body(self, w):
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    60
        w(u'<a href="%s"><img src="%s" alt="%s"/> %s</a>'
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    61
          % (xml_escape(self.entity.cw_adapt_to('IDownloadable').download_url()),
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    62
             self._cw.uiprops['DOWNLOAD_ICON'],
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    63
             self._cw._('download icon'), xml_escape(self.entity.dc_title())))
249
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    64
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    65
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    66
class DownloadView(EntityView):
631
99f5852f8604 major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    67
    """this view is replacing the deprecated 'download' controller and allow
99f5852f8604 major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    68
    downloading of entities providing the necessary interface
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3072
diff changeset
    70
    __regid__ = 'download'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    71
    __select__ = one_line_rset() & adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
    templatable = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
    content_type = 'application/octet-stream'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    binary = True
5866
e676a869a3e9 [idownloadable] make the download view http cache friendly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    76
    http_cache_manager = httpcache.EntityHTTPCacheManager
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    add_to_breadcrumbs = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
    def set_request_content_type(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
        """overriden to set the correct filetype and filename"""
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    81
        entity = self.cw_rset.complete_entity(self.cw_row or 0, self.cw_col or 0)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    82
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    83
        encoding = adapter.download_encoding()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        if encoding in BINARY_ENCODINGS:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
            contenttype = 'application/%s' % encoding
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
            encoding = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
        else:
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    88
            contenttype = adapter.download_content_type()
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    89
        self._cw.set_content_type(contenttype or self.content_type,
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    90
                                  filename=adapter.download_file_name(),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
                                  encoding=encoding)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
    def call(self):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    94
        entity = self.cw_rset.complete_entity(self.cw_row or 0, self.cw_col or 0)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    95
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    96
        self.w(adapter.download_data())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
5866
e676a869a3e9 [idownloadable] make the download view http cache friendly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    98
    def last_modified(self):
e676a869a3e9 [idownloadable] make the download view http cache friendly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    99
        return self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0).modification_date
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
5986
8339d4739f7f [images] add link around main image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   101
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
   102
class DownloadLinkView(EntityView):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    """view displaying a link to download the file"""
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3072
diff changeset
   104
    __regid__ = 'downloadlink'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   105
    __select__ = adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
    title = None # should not be listed in possible views
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   108
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
    def cell_call(self, row, col, title=None, **kwargs):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   110
        entity = self.cw_rset.get_entity(row, col)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   111
        url = xml_escape(entity.cw_adapt_to('IDownloadable').download_url())
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2136
diff changeset
   112
        self.w(u'<a href="%s">%s</a>' % (url, xml_escape(title or entity.dc_title())))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
   115
class IDownloadablePrimaryView(primary.PrimaryView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   116
    __select__ = adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   118
    def render_entity_attributes(self, entity):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
        self.w(u'<div class="content">')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   120
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   121
        contenttype = adapter.download_content_type()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        if contenttype.startswith('image/'):
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5987
diff changeset
   123
            self._cw.add_js('cubicweb.image.js')
5986
8339d4739f7f [images] add link around main image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   124
            self.wview('image', entity.cw_rset, row=entity.cw_row, col=entity.cw_col,
5987
a9d508973433 [images] use tags.img and allow to add arbitrary attributes on the image. Use it to set a class when image is the primary entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5986
diff changeset
   125
                       link=True, klass='contentimage')
6009
9633d2376687 [idownloadable primary view] when file is an image, display attributes below the image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
   126
            super(IDownloadablePrimaryView, self).render_entity_attributes(entity)
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   127
        elif contenttype.endswith('html'):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   128
            self.wview('downloadlink', entity.cw_rset, title=self._cw._('download'), row=entity.cw_row)
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   129
            self.wview('ehtml', entity.cw_rset, row=entity.cw_row, col=entity.cw_col,
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   130
                       height='600px', width='100%')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
        else:
6009
9633d2376687 [idownloadable primary view] when file is an image, display attributes below the image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
   132
            super(IDownloadablePrimaryView, self).render_entity_attributes(entity)
3460
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   133
            self.wview('downloadlink', entity.cw_rset, title=self._cw._('download'), row=entity.cw_row)
5714
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   134
            self.render_data(entity, contenttype, 'text/html')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
        self.w(u'</div>')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   136
5714
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   137
    def render_data(self, entity, sourcemt, targetmt):
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   138
        adapter = entity.cw_adapt_to('IDownloadable')
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   139
        if ENGINE.find_path(sourcemt, targetmt):
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   140
            try:
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   141
                self.w(entity._cw_mtc_transform(adapter.download_data(), sourcemt,
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   142
                                                targetmt, adapter.download_encoding()))
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   143
            except Exception, ex:
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   144
                self.exception('while rendering data for %s', entity)
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   145
                msg = self._cw._("can't display data, unexpected error: %s") \
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   146
                      % xml_escape(unicode(ex))
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   147
                self.w('<div class="error">%s</div>' % msg)
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   148
            return True
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   149
        return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
5986
8339d4739f7f [images] add link around main image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   151
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
class IDownloadableLineView(baseviews.OneLineView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   153
    __select__ = adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
    def cell_call(self, row, col, title=None, **kwargs):
2530
3cd117ecc3a8 bad merge?
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2524
diff changeset
   156
        """the oneline view is a link to download the file"""
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   157
        entity = self.cw_rset.get_entity(row, col)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2136
diff changeset
   158
        url = xml_escape(entity.absolute_url())
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   159
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   160
        name = xml_escape(title or adapter.download_file_name())
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   161
        durl = xml_escape(adapter.download_url())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
        self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' %
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   163
               (url, name, durl, self._cw._('download')))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   166
class AbstractEmbeddedView(EntityView):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   167
    __abstract__ = True
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   168
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   169
    _embedding_tag = None
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   170
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5987
diff changeset
   171
    def call(self, **kwargs):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   172
        rset = self.cw_rset
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        for i in xrange(len(rset)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
            self.w(u'<div class="efile">')
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5987
diff changeset
   175
            self.wview(self.__regid__, rset, row=i, col=0, **kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
            self.w(u'</div>')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   177
5987
a9d508973433 [images] use tags.img and allow to add arbitrary attributes on the image. Use it to set a class when image is the primary entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5986
diff changeset
   178
    def cell_call(self, row, col, link=False, **kwargs):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   179
        entity = self.cw_rset.get_entity(row, col)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   180
        adapter = entity.cw_adapt_to('IDownloadable')
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   181
        tag = self._embedding_tag(src=adapter.download_url(),
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   182
                                  alt=(self._cw._('download %s') % adapter.download_file_name()),
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   183
                                  **kwargs)
2524
2d0c04c8cbe4 #344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2470
diff changeset
   184
        if link:
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   185
            self.w(u'<a href="%s">%s</a>' % (adapter.download_url(), tag))
2524
2d0c04c8cbe4 #344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2470
diff changeset
   186
        else:
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   187
            self.w(tag)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
2524
2d0c04c8cbe4 #344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2470
diff changeset
   189
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   190
class ImageView(AbstractEmbeddedView):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   191
    __regid__ = 'image'
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   192
    __select__ = has_mimetype('image/')
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   193
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   194
    title = _('image')
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   195
    _embedding_tag = tags.img
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   196
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   197
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   198
class EHTMLView(AbstractEmbeddedView):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   199
    __regid__ = 'ehtml'
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   200
    __select__ = has_mimetype('text/html')
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   201
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   202
    title = _('embedded html')
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   203
    _embedding_tag = tags.iframe
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   204
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   205
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   206