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