author | sylvain.thenault@logilab.fr |
Thu, 09 Apr 2009 12:40:30 +0200 | |
branch | tls-sprint |
changeset 1314 | dc5499bff1a9 |
parent 987 | 0b39f59530fa |
child 1498 | 2c6eec0b46b9 |
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" |
|
8 |
||
9 |
from logilab.mtconverter import BINARY_ENCODINGS, TransformError, html_escape |
|
10 |
||
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
|
11 |
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
|
12 |
implements, match_context_prop) |
0 | 13 |
from cubicweb.interfaces import IDownloadable |
14 |
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
|
15 |
from cubicweb.web.box import EntityBoxTemplate |
0 | 16 |
from cubicweb.web.views import baseviews |
17 |
||
18 |
_ = unicode |
|
19 |
||
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
|
20 |
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
|
21 |
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
|
22 |
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
|
23 |
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
|
24 |
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
|
25 |
|
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
26 |
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
|
27 |
req = entity.req |
0 | 28 |
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
|
29 |
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
|
30 |
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
|
31 |
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
|
32 |
% html_escape(title)) |
0 | 33 |
w(u'<div class="sideBox downloadBox"><div class="sideBoxBody">') |
34 |
w(u'<a href="%s"><img src="%s" alt="%s"/> %s</a>' |
|
35 |
% (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
|
36 |
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
|
37 |
_('download icon'), html_escape(label or entity.dc_title()))) |
0 | 38 |
w(u'</div>') |
39 |
w(u'</div>\n</div>\n') |
|
40 |
||
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
|
41 |
|
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
42 |
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
|
43 |
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
|
44 |
# 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
|
45 |
# XXX primary_view selector ? |
987 | 46 |
__select__ = (one_line_rset() & implements(IDownloadable) & 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
|
47 |
order = 10 |
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
|
48 |
|
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 |
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
|
50 |
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
|
51 |
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
|
52 |
|
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
53 |
|
0 | 54 |
class DownloadView(baseviews.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
|
55 |
"""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
|
56 |
downloading of entities providing the necessary interface |
0 | 57 |
""" |
58 |
id = 'download' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
59 |
__select__ = one_line_rset() & implements(IDownloadable) |
0 | 60 |
|
61 |
templatable = False |
|
62 |
content_type = 'application/octet-stream' |
|
63 |
binary = True |
|
64 |
add_to_breadcrumbs = False |
|
65 |
||
66 |
def set_request_content_type(self): |
|
67 |
"""overriden to set the correct filetype and filename""" |
|
68 |
entity = self.complete_entity(0) |
|
69 |
encoding = entity.download_encoding() |
|
70 |
if encoding in BINARY_ENCODINGS: |
|
71 |
contenttype = 'application/%s' % encoding |
|
72 |
encoding = None |
|
73 |
else: |
|
74 |
contenttype = entity.download_content_type() |
|
75 |
self.req.set_content_type(contenttype or self.content_type, |
|
76 |
filename=entity.download_file_name(), |
|
77 |
encoding=encoding) |
|
78 |
||
79 |
def call(self): |
|
80 |
self.w(self.complete_entity(0).download_data()) |
|
81 |
||
82 |
||
83 |
class DownloadLinkView(baseviews.EntityView): |
|
84 |
"""view displaying a link to download the file""" |
|
85 |
id = 'downloadlink' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
86 |
__select__ = implements(IDownloadable) |
0 | 87 |
title = None # should not be listed in possible views |
88 |
||
89 |
||
90 |
def cell_call(self, row, col, title=None, **kwargs): |
|
91 |
entity = self.entity(row, col) |
|
92 |
url = html_escape(entity.download_url()) |
|
93 |
self.w(u'<a href="%s">%s</a>' % (url, html_escape(title or entity.dc_title()))) |
|
94 |
||
95 |
||
96 |
||
97 |
class IDownloadablePrimaryView(baseviews.PrimaryView): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
98 |
__select__ = implements(IDownloadable) |
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
|
99 |
# XXX File/Image attributes but this is not specified in the IDownloadable interface |
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
|
100 |
skip_attrs = baseviews.PrimaryView.skip_attrs + ('data', 'name') |
0 | 101 |
|
102 |
def render_entity_title(self, entity): |
|
103 |
self.w(u'<h1>%s %s</h1>' |
|
104 |
% (entity.dc_type().capitalize(), |
|
105 |
html_escape(entity.dc_title()))) |
|
106 |
||
107 |
def render_entity_attributes(self, entity, siderelations): |
|
108 |
super(IDownloadablePrimaryView, self).render_entity_attributes(entity, siderelations) |
|
109 |
self.w(u'<div class="content">') |
|
110 |
contenttype = entity.download_content_type() |
|
111 |
if contenttype.startswith('image/'): |
|
112 |
self.wview('image', entity.rset, row=entity.row) |
|
113 |
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
|
114 |
self.wview('downloadlink', entity.rset, title=self.req._('download'), row=entity.row) |
0 | 115 |
try: |
116 |
if ENGINE.has_input(contenttype): |
|
117 |
self.w(entity.printable_value('data')) |
|
118 |
except TransformError: |
|
119 |
pass |
|
120 |
except Exception, ex: |
|
121 |
msg = self.req._("can't display data, unexpected error: %s") % ex |
|
122 |
self.w('<div class="error">%s</div>' % msg) |
|
123 |
self.w(u'</div>') |
|
124 |
||
125 |
def is_side_related(self, rschema, eschema): |
|
126 |
"""display all relations as side related""" |
|
127 |
return True |
|
128 |
||
129 |
||
130 |
class IDownloadableLineView(baseviews.OneLineView): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
131 |
__select__ = implements(IDownloadable) |
0 | 132 |
|
133 |
def cell_call(self, row, col, title=None, **kwargs): |
|
134 |
"""the secondary view is a link to download the file""" |
|
135 |
entity = self.entity(row, col) |
|
136 |
url = html_escape(entity.absolute_url()) |
|
137 |
name = html_escape(entity.download_file_name()) |
|
138 |
durl = html_escape(entity.download_url()) |
|
139 |
self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' % |
|
140 |
(url, name, durl, self.req._('download'))) |
|
141 |
||
142 |
||
143 |
class ImageView(baseviews.EntityView): |
|
144 |
id = 'image' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
145 |
__select__ = implements(IDownloadable) & score_entity(is_image) |
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
|
146 |
|
0 | 147 |
title = _('image') |
148 |
||
149 |
def call(self): |
|
150 |
rset = self.rset |
|
151 |
for i in xrange(len(rset)): |
|
152 |
self.w(u'<div class="efile">') |
|
153 |
self.wview(self.id, rset, row=i, col=0) |
|
154 |
self.w(u'</div>') |
|
155 |
||
156 |
def cell_call(self, row, col): |
|
157 |
entity = self.entity(row, col) |
|
158 |
#if entity.data_format.startswith('image/'): |
|
159 |
self.w(u'<img src="%s" alt="%s"/>' % (html_escape(entity.download_url()), |
|
160 |
html_escape(entity.download_file_name()))) |
|
161 |