author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Wed, 20 May 2009 16:35:37 +0200 | |
branch | stable |
changeset 1884 | 893119ce3fef |
parent 1639 | 375c857aa0f5 |
child 1885 | c2011d238e98 |
permissions | -rw-r--r-- |
0 | 1 |
"""Specific views for entities implementing IDownloadable |
2 |
||
3 |
:organization: Logilab |
|
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
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
1639 | 8 |
_ = unicode |
0 | 9 |
|
10 |
from logilab.mtconverter import BINARY_ENCODINGS, TransformError, html_escape |
|
11 |
||
1639 | 12 |
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
|
13 |
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
|
14 |
implements, match_context_prop) |
0 | 15 |
from cubicweb.interfaces import IDownloadable |
16 |
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
|
17 |
from cubicweb.web.box import EntityBoxTemplate |
1639 | 18 |
from cubicweb.web.views import primary, baseviews |
0 | 19 |
|
20 |
||
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
|
21 |
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
|
22 |
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
|
23 |
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
|
24 |
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
|
25 |
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
|
26 |
|
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
27 |
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
|
28 |
req = entity.req |
0 | 29 |
w(u'<div class="sideRelated">') |
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
30 |
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
|
31 |
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
|
32 |
w(u'<div class="sideBoxTitle downloadBoxTitle"><span>%s</span></div>' |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
33 |
% html_escape(title)) |
0 | 34 |
w(u'<div class="sideBox downloadBox"><div class="sideBoxBody">') |
35 |
w(u'<a href="%s"><img src="%s" alt="%s"/> %s</a>' |
|
36 |
% (html_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
|
37 |
req.external_resource('DOWNLOAD_ICON'), |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
38 |
_('download icon'), html_escape(label or entity.dc_title()))) |
0 | 39 |
w(u'</div>') |
40 |
w(u'</div>\n</div>\n') |
|
41 |
||
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
|
42 |
|
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
# 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
|
46 |
# XXX primary_view selector ? |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
47 |
__select__ = (one_line_rset() & implements(IDownloadable) & |
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
48 |
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
|
49 |
order = 10 |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
50 |
|
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
|
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
55 |
|
1639 | 56 |
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
|
57 |
"""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
|
58 |
downloading of entities providing the necessary interface |
0 | 59 |
""" |
60 |
id = 'download' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
61 |
__select__ = one_line_rset() & implements(IDownloadable) |
0 | 62 |
|
63 |
templatable = False |
|
64 |
content_type = 'application/octet-stream' |
|
65 |
binary = True |
|
66 |
add_to_breadcrumbs = False |
|
67 |
||
68 |
def set_request_content_type(self): |
|
69 |
"""overriden to set the correct filetype and filename""" |
|
70 |
entity = self.complete_entity(0) |
|
71 |
encoding = entity.download_encoding() |
|
72 |
if encoding in BINARY_ENCODINGS: |
|
73 |
contenttype = 'application/%s' % encoding |
|
74 |
encoding = None |
|
75 |
else: |
|
76 |
contenttype = entity.download_content_type() |
|
77 |
self.req.set_content_type(contenttype or self.content_type, |
|
78 |
filename=entity.download_file_name(), |
|
79 |
encoding=encoding) |
|
80 |
||
81 |
def call(self): |
|
82 |
self.w(self.complete_entity(0).download_data()) |
|
83 |
||
84 |
||
1639 | 85 |
class DownloadLinkView(EntityView): |
0 | 86 |
"""view displaying a link to download the file""" |
87 |
id = 'downloadlink' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
88 |
__select__ = implements(IDownloadable) |
0 | 89 |
title = None # should not be listed in possible views |
90 |
||
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
91 |
|
0 | 92 |
def cell_call(self, row, col, title=None, **kwargs): |
93 |
entity = self.entity(row, col) |
|
94 |
url = html_escape(entity.download_url()) |
|
95 |
self.w(u'<a href="%s">%s</a>' % (url, html_escape(title or entity.dc_title()))) |
|
96 |
||
97 |
||
1639 | 98 |
class IDownloadablePrimaryView(primary.PrimaryView): |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
99 |
__select__ = implements(IDownloadable) |
0 | 100 |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
101 |
def render_entity_attributes(self, entity): |
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
102 |
super(IDownloadablePrimaryView, self).render_entity_attributes(entity) |
0 | 103 |
self.w(u'<div class="content">') |
104 |
contenttype = entity.download_content_type() |
|
105 |
if contenttype.startswith('image/'): |
|
106 |
self.wview('image', entity.rset, row=entity.row) |
|
107 |
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
|
108 |
self.wview('downloadlink', entity.rset, title=self.req._('download'), row=entity.row) |
0 | 109 |
try: |
110 |
if ENGINE.has_input(contenttype): |
|
111 |
self.w(entity.printable_value('data')) |
|
112 |
except TransformError: |
|
113 |
pass |
|
114 |
except Exception, ex: |
|
115 |
msg = self.req._("can't display data, unexpected error: %s") % ex |
|
116 |
self.w('<div class="error">%s</div>' % msg) |
|
117 |
self.w(u'</div>') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
118 |
|
0 | 119 |
|
120 |
class IDownloadableLineView(baseviews.OneLineView): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
121 |
__select__ = implements(IDownloadable) |
0 | 122 |
|
123 |
def cell_call(self, row, col, title=None, **kwargs): |
|
124 |
"""the secondary view is a link to download the file""" |
|
125 |
entity = self.entity(row, col) |
|
126 |
url = html_escape(entity.absolute_url()) |
|
127 |
name = html_escape(entity.download_file_name()) |
|
128 |
durl = html_escape(entity.download_url()) |
|
129 |
self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' % |
|
130 |
(url, name, durl, self.req._('download'))) |
|
131 |
||
132 |
||
1639 | 133 |
class ImageView(EntityView): |
0 | 134 |
id = 'image' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
135 |
__select__ = implements(IDownloadable) & score_entity(is_image) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
136 |
|
0 | 137 |
title = _('image') |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
138 |
|
0 | 139 |
def call(self): |
140 |
rset = self.rset |
|
141 |
for i in xrange(len(rset)): |
|
142 |
self.w(u'<div class="efile">') |
|
143 |
self.wview(self.id, rset, row=i, col=0) |
|
144 |
self.w(u'</div>') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
987
diff
changeset
|
145 |
|
0 | 146 |
def cell_call(self, row, col): |
147 |
entity = self.entity(row, col) |
|
148 |
#if entity.data_format.startswith('image/'): |
|
149 |
self.w(u'<img src="%s" alt="%s"/>' % (html_escape(entity.download_url()), |
|
150 |
html_escape(entity.download_file_name()))) |
|
151 |