web/views/idownloadable.py
author Rémi Cardona <remi.cardona@logilab.fr>
Fri, 11 Sep 2015 18:05:38 +0200
changeset 10666 7f6b5f023884
parent 10609 e2d8e81bfe68
child 10907 9ae707db5265
permissions -rw-r--r--
[py3k] replace '_ = unicode' in global scope (closes #7589459)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10006
8391bf718485 remove most 3.10 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8695
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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
     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/>.
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6926
diff changeset
    18
"""
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6926
diff changeset
    19
Specific views for entities adapting to IDownloadable
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6926
diff changeset
    20
=====================================================
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6926
diff changeset
    21
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
__docformat__ = "restructuredtext en"
10666
7f6b5f023884 [py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
    23
from cubicweb import _
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10006
diff changeset
    25
from six.moves import range
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10006
diff changeset
    26
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2136
diff changeset
    27
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
    28
from logilab.common.deprecation import class_renamed, deprecated
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
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
    30
from cubicweb import tags
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    31
from cubicweb.view import EntityView
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    32
from cubicweb.predicates import (one_line_rset, is_instance, match_context_prop,
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    33
                                 adaptable, has_mimetype)
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3890
diff changeset
    34
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
    35
from cubicweb.web import component, httpcache
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    36
from cubicweb.web.views import primary, baseviews
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
6141
b8287e54b528 [web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6140
diff changeset
    39
class DownloadBox(component.EntityCtxComponent):
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6926
diff changeset
    40
    """add download box"""
6926
2cda8f1b4fae cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6823
diff changeset
    41
    __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
    42
    __select__ = (component.EntityCtxComponent.__select__ &
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    43
                  adaptable('IDownloadable') & ~has_mimetype('image/'))
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    44
249
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    45
    order = 10
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    46
    title = _('download')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
    47
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
    48
    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
    49
        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
    50
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6048
diff changeset
    51
    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
    52
        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
    53
            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
    54
            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
    55
              % (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
    56
                 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
    57
                 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
    58
                 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
    59
5ab64969df20 define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 237
diff changeset
    60
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    61
class DownloadView(EntityView):
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6926
diff changeset
    62
    """download view
7818
be4a3e97090d [idownloadable] dtw and up copyright
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    63
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6926
diff changeset
    64
    this view is replacing the deprecated 'download' controller and allow
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
    65
    downloading of entities providing the necessary interface
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3072
diff changeset
    67
    __regid__ = 'download'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    68
    __select__ = one_line_rset() & adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    templatable = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    content_type = 'application/octet-stream'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    binary = True
5866
e676a869a3e9 [idownloadable] make the download view http cache friendly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    73
    http_cache_manager = httpcache.EntityHTTPCacheManager
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
    add_to_breadcrumbs = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
    def set_request_content_type(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
        """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
    78
        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
    79
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    80
        encoding = adapter.download_encoding()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        if encoding in BINARY_ENCODINGS:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
            contenttype = 'application/%s' % encoding
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
            encoding = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        else:
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    85
            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
    86
        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
    87
                                  filename=adapter.download_file_name(),
8612
3bb96935a6b9 [web] use `inline` `Content-Disposition` by default (closes #2535734)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8190
diff changeset
    88
                                  encoding=encoding,
3bb96935a6b9 [web] use `inline` `Content-Disposition` by default (closes #2535734)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8190
diff changeset
    89
                                  disposition='attachment')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
    def call(self):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    92
        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
    93
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
    94
        self.w(adapter.download_data())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
5866
e676a869a3e9 [idownloadable] make the download view http cache friendly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    96
    def last_modified(self):
e676a869a3e9 [idownloadable] make the download view http cache friendly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    97
        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
    98
5986
8339d4739f7f [images] add link around main image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
    99
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
   100
class DownloadLinkView(EntityView):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
    """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
   102
    __regid__ = 'downloadlink'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   103
    __select__ = adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    title = None # should not be listed in possible views
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   106
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
    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
   108
        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
   109
        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
   110
        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
   111
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
1639
375c857aa0f5 import from primary
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
   113
class IDownloadablePrimaryView(primary.PrimaryView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   114
    __select__ = adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   116
    def render_entity_attributes(self, entity):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        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
   118
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   119
        contenttype = adapter.download_content_type()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        if contenttype.startswith('image/'):
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5987
diff changeset
   121
            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
   122
            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
   123
                       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
   124
            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
   125
        elif contenttype.endswith('html'):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   126
            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
   127
            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
   128
                       height='600px', width='100%')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        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
   130
            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
   131
            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
   132
            self.render_data(entity, contenttype, 'text/html')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
        self.w(u'</div>')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   134
5714
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   135
    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
   136
        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
   137
        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
   138
            try:
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   139
                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
   140
                                                targetmt, adapter.download_encoding()))
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8612
diff changeset
   141
            except Exception as ex:
5714
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   142
                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
   143
                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
   144
                      % 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
   145
                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
   146
            return True
04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   147
        return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
5986
8339d4739f7f [images] add link around main image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   149
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
   150
class IDownloadableOneLineView(baseviews.OneLineView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5467
diff changeset
   151
    __select__ = adaptable('IDownloadable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
    def cell_call(self, row, col, title=None, **kwargs):
2530
3cd117ecc3a8 bad merge?
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2524
diff changeset
   154
        """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
   155
        entity = self.cw_rset.get_entity(row, col)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2136
diff changeset
   156
        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
   157
        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
   158
        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
   159
        durl = xml_escape(adapter.download_url())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
        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
   161
               (url, name, durl, self._cw._('download')))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   164
class AbstractEmbeddedView(EntityView):
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   165
    __abstract__ = True
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   166
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   167
    _embedding_tag = None
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   168
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5987
diff changeset
   169
    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
   170
        rset = self.cw_rset
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10006
diff changeset
   171
        for i in range(len(rset)):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
            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
   173
            self.wview(self.__regid__, rset, row=i, col=0, **kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
            self.w(u'</div>')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 987
diff changeset
   175
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
   176
    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
   177
        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
   178
        adapter = entity.cw_adapt_to('IDownloadable')
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7818
diff changeset
   179
        tag = self._embedding_tag(src=adapter.download_url(), # pylint: disable=E1102
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   180
                                  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
   181
                                  **kwargs)
2524
2d0c04c8cbe4 #344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2470
diff changeset
   182
        if link:
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   183
            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
   184
        else:
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   185
            self.w(tag)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
2524
2d0c04c8cbe4 #344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2470
diff changeset
   187
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   188
class ImageView(AbstractEmbeddedView):
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6926
diff changeset
   189
    """image embedded view"""
6048
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   190
    __regid__ = 'image'
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   191
    __select__ = has_mimetype('image/')
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   192
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   193
    title = _('image')
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   194
    _embedding_tag = tags.img
4695b1ee58a0 fix idownload view pb. for xhtml browsers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6009
diff changeset
   195
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
class EHTMLView(AbstractEmbeddedView):
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6926
diff changeset
   198
    """html embedded view"""
6048
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