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