author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 22 Sep 2009 14:08:21 +0200 | |
branch | stable |
changeset 3370 | 62ed9981a770 |
parent 3065 | 694c03f7d72f |
child 3072 | 6fb42c53f6df |
child 3789 | fb22b55f80f8 |
permissions | -rw-r--r-- |
0 | 1 |
"""Specific views for entities implementing IDownloadable |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1885
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1885
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
1639 | 9 |
_ = unicode |
0 | 10 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2136
diff
changeset
|
11 |
from logilab.mtconverter import BINARY_ENCODINGS, TransformError, xml_escape |
0 | 12 |
|
1639 | 13 |
from cubicweb.view import EntityView |
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
631
diff
changeset
|
14 |
from cubicweb.selectors import (one_line_rset, score_entity, |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
631
diff
changeset
|
15 |
implements, match_context_prop) |
0 | 16 |
from cubicweb.interfaces import IDownloadable |
17 |
from cubicweb.common.mttransforms import ENGINE |
|
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
18 |
from cubicweb.web.box import EntityBoxTemplate |
1639 | 19 |
from cubicweb.web.views import primary, baseviews |
0 | 20 |
|
21 |
||
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
diff
changeset
|
22 |
def is_image(entity): |
6a25c58a1c23
backport stable branch, take care a lot of conflicts occured, this may be the revision you're looking for...
sylvain.thenault@logilab.fr
diff
changeset
|
23 |
mt = entity.download_content_type() |
6a25c58a1c23
backport stable branch, take care a lot of conflicts occured, this may be the revision you're looking for...
sylvain.thenault@logilab.fr
diff
changeset
|
24 |
if not (mt and mt.startswith('image/')): |
6a25c58a1c23
backport stable branch, take care a lot of conflicts occured, this may be the revision you're looking for...
sylvain.thenault@logilab.fr
diff
changeset
|
25 |
return 0 |
6a25c58a1c23
backport stable branch, take care a lot of conflicts occured, this may be the revision you're looking for...
sylvain.thenault@logilab.fr
diff
changeset
|
26 |
return 1 |
6a25c58a1c23
backport stable branch, take care a lot of conflicts occured, this may be the revision you're looking for...
sylvain.thenault@logilab.fr
diff
changeset
|
27 |
|
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
28 |
def download_box(w, entity, title=None, label=None): |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
29 |
req = entity.req |
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1639
diff
changeset
|
30 |
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
|
31 |
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
|
32 |
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
|
33 |
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
|
34 |
% xml_escape(title)) |
0 | 35 |
w(u'<div class="sideBox downloadBox"><div class="sideBoxBody">') |
36 |
w(u'<a href="%s"><img src="%s" alt="%s"/> %s</a>' |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2136
diff
changeset
|
37 |
% (xml_escape(entity.download_url()), |
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
38 |
req.external_resource('DOWNLOAD_ICON'), |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2136
diff
changeset
|
39 |
_('download icon'), xml_escape(label or entity.dc_title()))) |
0 | 40 |
w(u'</div>') |
41 |
w(u'</div>\n</div>\n') |
|
42 |
||
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
diff
changeset
|
43 |
|
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
44 |
class DownloadBox(EntityBoxTemplate): |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
45 |
id = '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
diff
changeset
|
46 |
# 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
|
47 |
# XXX primary_view selector ? |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
48 |
__select__ = (one_line_rset() & implements(IDownloadable) & |
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
49 |
match_context_prop() & ~score_entity(is_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
|
50 |
order = 10 |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
51 |
|
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
52 |
def cell_call(self, row, col, title=None, label=None, **kwargs): |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
53 |
entity = self.entity(row, col) |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
54 |
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
|
55 |
|
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
56 |
|
1639 | 57 |
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
|
58 |
"""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
|
59 |
downloading of entities providing the necessary interface |
0 | 60 |
""" |
61 |
id = 'download' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
62 |
__select__ = one_line_rset() & implements(IDownloadable) |
0 | 63 |
|
64 |
templatable = False |
|
65 |
content_type = 'application/octet-stream' |
|
66 |
binary = True |
|
67 |
add_to_breadcrumbs = False |
|
68 |
||
69 |
def set_request_content_type(self): |
|
70 |
"""overriden to set the correct filetype and filename""" |
|
71 |
entity = self.complete_entity(0) |
|
72 |
encoding = entity.download_encoding() |
|
73 |
if encoding in BINARY_ENCODINGS: |
|
74 |
contenttype = 'application/%s' % encoding |
|
75 |
encoding = None |
|
76 |
else: |
|
77 |
contenttype = entity.download_content_type() |
|
78 |
self.req.set_content_type(contenttype or self.content_type, |
|
79 |
filename=entity.download_file_name(), |
|
80 |
encoding=encoding) |
|
81 |
||
82 |
def call(self): |
|
83 |
self.w(self.complete_entity(0).download_data()) |
|
84 |
||
85 |
||
1639 | 86 |
class DownloadLinkView(EntityView): |
0 | 87 |
"""view displaying a link to download the file""" |
88 |
id = 'downloadlink' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
89 |
__select__ = implements(IDownloadable) |
0 | 90 |
title = None # should not be listed in possible views |
91 |
||
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
92 |
|
0 | 93 |
def cell_call(self, row, col, title=None, **kwargs): |
94 |
entity = self.entity(row, col) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2136
diff
changeset
|
95 |
url = xml_escape(entity.download_url()) |
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2136
diff
changeset
|
96 |
self.w(u'<a href="%s">%s</a>' % (url, xml_escape(title or entity.dc_title()))) |
0 | 97 |
|
98 |
||
1639 | 99 |
class IDownloadablePrimaryView(primary.PrimaryView): |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
100 |
__select__ = implements(IDownloadable) |
0 | 101 |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
102 |
def render_entity_attributes(self, entity): |
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
103 |
super(IDownloadablePrimaryView, self).render_entity_attributes(entity) |
0 | 104 |
self.w(u'<div class="content">') |
105 |
contenttype = entity.download_content_type() |
|
106 |
if contenttype.startswith('image/'): |
|
107 |
self.wview('image', entity.rset, row=entity.row) |
|
108 |
else: |
|
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
|
109 |
self.wview('downloadlink', entity.rset, title=self.req._('download'), row=entity.row) |
0 | 110 |
try: |
111 |
if ENGINE.has_input(contenttype): |
|
112 |
self.w(entity.printable_value('data')) |
|
113 |
except TransformError: |
|
114 |
pass |
|
115 |
except Exception, ex: |
|
116 |
msg = self.req._("can't display data, unexpected error: %s") % ex |
|
117 |
self.w('<div class="error">%s</div>' % msg) |
|
118 |
self.w(u'</div>') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
119 |
|
0 | 120 |
|
121 |
class IDownloadableLineView(baseviews.OneLineView): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
122 |
__select__ = implements(IDownloadable) |
0 | 123 |
|
124 |
def cell_call(self, row, col, title=None, **kwargs): |
|
2530 | 125 |
"""the oneline view is a link to download the file""" |
0 | 126 |
entity = self.entity(row, col) |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2136
diff
changeset
|
127 |
url = xml_escape(entity.absolute_url()) |
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2136
diff
changeset
|
128 |
name = xml_escape(title or entity.download_file_name()) |
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2136
diff
changeset
|
129 |
durl = xml_escape(entity.download_url()) |
0 | 130 |
self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' % |
131 |
(url, name, durl, self.req._('download'))) |
|
132 |
||
133 |
||
1639 | 134 |
class ImageView(EntityView): |
0 | 135 |
id = 'image' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
136 |
__select__ = implements(IDownloadable) & score_entity(is_image) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
137 |
|
0 | 138 |
title = _('image') |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
139 |
|
0 | 140 |
def call(self): |
141 |
rset = self.rset |
|
142 |
for i in xrange(len(rset)): |
|
143 |
self.w(u'<div class="efile">') |
|
144 |
self.wview(self.id, rset, row=i, col=0) |
|
145 |
self.w(u'</div>') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
146 |
|
2524
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
147 |
def cell_call(self, row, col, width=None, height=None, link=False): |
0 | 148 |
entity = self.entity(row, col) |
149 |
#if entity.data_format.startswith('image/'): |
|
3065
694c03f7d72f
<a> doesn't have alt attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2530
diff
changeset
|
150 |
imgtag = u'<img src="%s" alt="%s" ' % ( |
694c03f7d72f
<a> doesn't have alt attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2530
diff
changeset
|
151 |
xml_escape(entity.download_url()), |
694c03f7d72f
<a> doesn't have alt attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2530
diff
changeset
|
152 |
(self.req._('download %s') % xml_escape(entity.download_file_name()))) |
2524
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
153 |
if width: |
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
154 |
imgtag += u'width="%i" ' % width |
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
155 |
if height: |
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
156 |
imgtag += u'height="%i" ' % height |
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
157 |
imgtag += u'/>' |
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
158 |
if link: |
3065
694c03f7d72f
<a> doesn't have alt attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2530
diff
changeset
|
159 |
self.w(u'<a href="%s">%s</a>' % (entity.absolute_url(vid='download'), |
694c03f7d72f
<a> doesn't have alt attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2530
diff
changeset
|
160 |
imgtag)) |
2524
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
161 |
else: |
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
162 |
self.w(imgtag) |
0 | 163 |
|
2524
2d0c04c8cbe4
#344787: add options to idownloadable
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2470
diff
changeset
|
164 |