web/views/idownloadable.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 31 Jan 2011 17:31:09 +0100
branchstable
changeset 6926 2cda8f1b4fae
parent 6823 dc4693820fc7
child 7529 2fdc310be7cd
permissions -rw-r--r--
cleanups
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
6814
48402faff043 [idownloadable] deprecate download_box function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6813
diff changeset
    24
from logilab.common.deprecation import class_renamed, deprecated
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
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
    26
from cubicweb import tags
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    27
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
    28
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
    29
                                adaptable, has_mimetype)
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3890
diff changeset
    30
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
    31
from cubicweb.web import component, httpcache
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    32
from cubicweb.web.views import primary, baseviews
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
6814
48402faff043 [idownloadable] deprecate download_box function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6813
diff changeset
    35
@deprecated('[3.10] use a custom IDownloadable adapter instead')
3846
1a7d10864628 new argument to provide extra content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3789
diff changeset
    36
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
    37
    req = entity._cw
1885
c2011d238e98 replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1639
diff changeset
    38
    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
    39
    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
    40
        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
    41
    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
    42
      % xml_escape(title))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    w(u'<div class="sideBox downloadBox"><div class="sideBoxBody">')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    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
    45
      % (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
    46
         req.uiprops['DOWNLOAD_ICON'],
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    47
         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
    48
    w(u'%s</div>' % footer)
1a7d10864628 new argument to provide extra content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3789
diff changeset
    49
    w(u'</div></div>\n')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
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
    51
6141
b8287e54b528 [web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6140
diff changeset
    52
class DownloadBox(component.EntityCtxComponent):
6926
2cda8f1b4fae cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6823
diff changeset
    53
    __regid__ = 'download_box'    # 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
    54
    __select__ = (component.EntityCtxComponent.__select__ &
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    55
                  adaptable('IDownloadable') & ~has_mimetype('image/'))
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    56
249
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    57
    order = 10
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    58
    title = _('download')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
    59
6699
e20f350fc0af [download box] refactor to ease when one want to propose multiple elements to download
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6632
diff changeset
    60
    def init_rendering(self):
e20f350fc0af [download box] refactor to ease when one want to propose multiple elements to download
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6632
diff changeset
    61
        self.items = [self.entity]
e20f350fc0af [download box] refactor to ease when one want to propose multiple elements to download
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6632
diff changeset
    62
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    63
    def render_body(self, w):
6699
e20f350fc0af [download box] refactor to ease when one want to propose multiple elements to download
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6632
diff changeset
    64
        for item in self.items:
6793
308cf1eaf576 [idownloadable] oneline view display entity's default title instead of file name, while download box display file name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6699
diff changeset
    65
            idownloadable = item.cw_adapt_to('IDownloadable')
6699
e20f350fc0af [download box] refactor to ease when one want to propose multiple elements to download
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6632
diff changeset
    66
            w(u'<a href="%s"><img src="%s" alt="%s"/> %s</a>'
6793
308cf1eaf576 [idownloadable] oneline view display entity's default title instead of file name, while download box display file name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6699
diff changeset
    67
              % (xml_escape(idownloadable.download_url()),
6699
e20f350fc0af [download box] refactor to ease when one want to propose multiple elements to download
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6632
diff changeset
    68
                 self._cw.uiprops['DOWNLOAD_ICON'],
6793
308cf1eaf576 [idownloadable] oneline view display entity's default title instead of file name, while download box display file name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6699
diff changeset
    69
                 self._cw._('download icon'),
308cf1eaf576 [idownloadable] oneline view display entity's default title instead of file name, while download box display file name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6699
diff changeset
    70
                 xml_escape(idownloadable.download_file_name())))
249
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    71
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    72
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    73
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
    74
    """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
    75
    downloading of entities providing the necessary interface
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3072
diff changeset
    77
    __regid__ = 'download'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    78
    __select__ = one_line_rset() & adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
    templatable = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
    content_type = 'application/octet-stream'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    binary = True
5866
e676a869a3e9 [idownloadable] make the download view http cache friendly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    83
    http_cache_manager = httpcache.EntityHTTPCacheManager
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    add_to_breadcrumbs = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    def set_request_content_type(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
        """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
    88
        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
    89
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    90
        encoding = adapter.download_encoding()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        if encoding in BINARY_ENCODINGS:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
            contenttype = 'application/%s' % encoding
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
            encoding = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        else:
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    95
            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
    96
        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
    97
                                  filename=adapter.download_file_name(),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
                                  encoding=encoding)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    def call(self):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   101
        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
   102
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   103
        self.w(adapter.download_data())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
5866
e676a869a3e9 [idownloadable] make the download view http cache friendly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   105
    def last_modified(self):
e676a869a3e9 [idownloadable] make the download view http cache friendly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   106
        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
   107
5986
8339d4739f7f [images] add link around main image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   108
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
   109
class DownloadLinkView(EntityView):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
    """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
   111
    __regid__ = 'downloadlink'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   112
    __select__ = adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
    title = None # should not be listed in possible views
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   115
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    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
   117
        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
   118
        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
   119
        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
   120
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
   122
class IDownloadablePrimaryView(primary.PrimaryView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   123
    __select__ = adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   125
    def render_entity_attributes(self, entity):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
        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
   127
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   128
        contenttype = adapter.download_content_type()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        if contenttype.startswith('image/'):
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5987
diff changeset
   130
            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
   131
            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
   132
                       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
   133
            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
   134
        elif contenttype.endswith('html'):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   135
            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
   136
            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
   137
                       height='600px', width='100%')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        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
   139
            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
   140
            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
   141
            self.render_data(entity, contenttype, 'text/html')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        self.w(u'</div>')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   143
5714
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   144
    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
   145
        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
   146
        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
   147
            try:
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   148
                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
   149
                                                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
   150
            except Exception, ex:
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   151
                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
   152
                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
   153
                      % 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
   154
                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
   155
            return True
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   156
        return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
5986
8339d4739f7f [images] add link around main image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   158
6793
308cf1eaf576 [idownloadable] oneline view display entity's default title instead of file name, while download box display file name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6699
diff changeset
   159
class IDownloadableOneLineView(baseviews.OneLineView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   160
    __select__ = adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
    def cell_call(self, row, col, title=None, **kwargs):
2530
3cd117ecc3a8 bad merge?
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2524
diff changeset
   163
        """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
   164
        entity = self.cw_rset.get_entity(row, col)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2136
diff changeset
   165
        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
   166
        adapter = entity.cw_adapt_to('IDownloadable')
6793
308cf1eaf576 [idownloadable] oneline view display entity's default title instead of file name, while download box display file name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6699
diff changeset
   167
        name = xml_escape(title or entity.dc_title())
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   168
        durl = xml_escape(adapter.download_url())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
        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
   170
               (url, name, durl, self._cw._('download')))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
6813
5ad3e070b1a6 [idownloadable] restore backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6793
diff changeset
   172
IDownloadableLineView = class_renamed(
5ad3e070b1a6 [idownloadable] restore backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6793
diff changeset
   173
    'IDownloadableLineView', IDownloadableOneLineView,
6823
dc4693820fc7 [views] fix typo (invalid format character) in deprecation warning
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6814
diff changeset
   174
    '[3.10] IDownloadableLineView is deprecated, use IDownloadableOneLineView')
6813
5ad3e070b1a6 [idownloadable] restore backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6793
diff changeset
   175
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   177
class AbstractEmbeddedView(EntityView):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   178
    __abstract__ = True
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   179
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   180
    _embedding_tag = None
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   181
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5987
diff changeset
   182
    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
   183
        rset = self.cw_rset
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
        for i in xrange(len(rset)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
            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
   186
            self.wview(self.__regid__, rset, row=i, col=0, **kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
            self.w(u'</div>')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   188
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
   189
    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
   190
        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
   191
        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
   192
        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
   193
                                  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
   194
                                  **kwargs)
2524
2d0c04c8cbe4 #344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2470
diff changeset
   195
        if link:
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   196
            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
   197
        else:
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   198
            self.w(tag)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
2524
2d0c04c8cbe4 #344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2470
diff changeset
   200
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   201
class ImageView(AbstractEmbeddedView):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   202
    __regid__ = 'image'
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   203
    __select__ = has_mimetype('image/')
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
    title = _('image')
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   206
    _embedding_tag = tags.img
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   207
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   208
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   209
class EHTMLView(AbstractEmbeddedView):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   210
    __regid__ = 'ehtml'
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   211
    __select__ = has_mimetype('text/html')
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   212
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   213
    title = _('embedded html')
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   214
    _embedding_tag = tags.iframe
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   215
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   216
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   217