author | Sandrine Ribeau <sandrine.ribeau@logilab.fr> |
Thu, 23 Apr 2009 11:58:11 -0700 | |
changeset 1461 | 023b123ab76b |
parent 957 | b1f68f725fe9 |
child 985 | 6a25c58a1c23 |
permissions | -rw-r--r-- |
0 | 1 |
"""Specific views for entities implementing IDownloadable |
2 |
||
3 |
:organization: Logilab |
|
4 |
:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
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 |
||
11 |
from cubicweb.interfaces import IDownloadable |
|
12 |
from cubicweb.common.mttransforms import ENGINE |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
13 |
from cubicweb.common.selectors import (one_line_rset, score_entity_selector, |
431 | 14 |
implement_interface, match_context_prop) |
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 |
||
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
20 |
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
|
21 |
req = entity.req |
0 | 22 |
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
|
23 |
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
|
24 |
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
|
25 |
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
|
26 |
% html_escape(title)) |
0 | 27 |
w(u'<div class="sideBox downloadBox"><div class="sideBoxBody">') |
28 |
w(u'<a href="%s"><img src="%s" alt="%s"/> %s</a>' |
|
29 |
% (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
|
30 |
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
|
31 |
_('download icon'), html_escape(label or entity.dc_title()))) |
0 | 32 |
w(u'</div>') |
33 |
w(u'</div>\n</div>\n') |
|
34 |
||
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
35 |
|
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
36 |
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
|
37 |
id = 'download_box' |
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
|
38 |
# XXX primary_view selector ? |
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
|
39 |
__selectors__ = (one_line_rset, implement_interface, match_context_prop, score_entity_selector) |
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
40 |
accepts_interfaces = (IDownloadable,) |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
41 |
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
|
42 |
|
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
|
43 |
@classmethod |
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
|
44 |
def score_entity(cls, entity): |
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 |
mt = entity.download_content_type() |
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 |
# no download box for images |
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 |
if mt and mt.startswith('image/'): |
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 |
return 0 |
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
|
49 |
return 1 |
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
|
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 |
|
0 | 56 |
class DownloadView(baseviews.EntityView): |
57 |
"""this view is replacing the deprecated 'download' controller and allow downloading |
|
58 |
of entities providing the necessary interface |
|
59 |
""" |
|
60 |
id = 'download' |
|
431 | 61 |
__selectors__ = (one_line_rset, implement_interface) |
0 | 62 |
accepts_interfaces = (IDownloadable,) |
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 |
||
86 |
class DownloadLinkView(baseviews.EntityView): |
|
87 |
"""view displaying a link to download the file""" |
|
88 |
id = 'downloadlink' |
|
89 |
title = None # should not be listed in possible views |
|
431 | 90 |
__selectors__ = (implement_interface,) |
0 | 91 |
|
92 |
accepts_interfaces = (IDownloadable,) |
|
93 |
||
94 |
def cell_call(self, row, col, title=None, **kwargs): |
|
95 |
entity = self.entity(row, col) |
|
96 |
url = html_escape(entity.download_url()) |
|
97 |
self.w(u'<a href="%s">%s</a>' % (url, html_escape(title or entity.dc_title()))) |
|
98 |
||
99 |
||
100 |
||
101 |
class IDownloadablePrimaryView(baseviews.PrimaryView): |
|
431 | 102 |
__selectors__ = (implement_interface,) |
0 | 103 |
accepts_interfaces = (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
|
104 |
# 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
|
105 |
skip_attrs = baseviews.PrimaryView.skip_attrs + ('data', 'name') |
0 | 106 |
|
107 |
def render_entity_title(self, entity): |
|
108 |
self.w(u'<h1>%s %s</h1>' |
|
109 |
% (entity.dc_type().capitalize(), |
|
110 |
html_escape(entity.dc_title()))) |
|
111 |
||
112 |
def render_entity_attributes(self, entity, siderelations): |
|
113 |
super(IDownloadablePrimaryView, self).render_entity_attributes(entity, siderelations) |
|
114 |
self.w(u'<div class="content">') |
|
115 |
contenttype = entity.download_content_type() |
|
116 |
if contenttype.startswith('image/'): |
|
117 |
self.wview('image', entity.rset, row=entity.row) |
|
118 |
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
|
119 |
self.wview('downloadlink', entity.rset, title=self.req._('download'), row=entity.row) |
0 | 120 |
try: |
121 |
if ENGINE.has_input(contenttype): |
|
122 |
self.w(entity.printable_value('data')) |
|
123 |
except TransformError: |
|
124 |
pass |
|
125 |
except Exception, ex: |
|
126 |
msg = self.req._("can't display data, unexpected error: %s") % ex |
|
127 |
self.w('<div class="error">%s</div>' % msg) |
|
128 |
self.w(u'</div>') |
|
129 |
||
130 |
def is_side_related(self, rschema, eschema): |
|
131 |
"""display all relations as side related""" |
|
132 |
return True |
|
133 |
||
134 |
||
135 |
class IDownloadableLineView(baseviews.OneLineView): |
|
431 | 136 |
__selectors__ = (implement_interface,) |
0 | 137 |
# don't kick default oneline view |
138 |
accepts_interfaces = (IDownloadable,) |
|
139 |
||
140 |
||
141 |
def cell_call(self, row, col, title=None, **kwargs): |
|
142 |
"""the secondary view is a link to download the file""" |
|
143 |
entity = self.entity(row, col) |
|
144 |
url = html_escape(entity.absolute_url()) |
|
145 |
name = html_escape(entity.download_file_name()) |
|
146 |
durl = html_escape(entity.download_url()) |
|
147 |
self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' % |
|
148 |
(url, name, durl, self.req._('download'))) |
|
149 |
||
150 |
||
151 |
class ImageView(baseviews.EntityView): |
|
431 | 152 |
__selectors__ = (implement_interface, score_entity_selector) |
0 | 153 |
id = 'image' |
154 |
title = _('image') |
|
155 |
accepts_interfaces = (IDownloadable,) |
|
156 |
||
157 |
def call(self): |
|
158 |
rset = self.rset |
|
159 |
for i in xrange(len(rset)): |
|
160 |
self.w(u'<div class="efile">') |
|
161 |
self.wview(self.id, rset, row=i, col=0) |
|
162 |
self.w(u'</div>') |
|
163 |
||
164 |
@classmethod |
|
165 |
def score_entity(cls, entity): |
|
166 |
mt = entity.download_content_type() |
|
167 |
if not (mt and mt.startswith('image/')): |
|
168 |
return 0 |
|
169 |
return 1 |
|
170 |
||
171 |
def cell_call(self, row, col): |
|
172 |
entity = self.entity(row, col) |
|
173 |
#if entity.data_format.startswith('image/'): |
|
174 |
self.w(u'<img src="%s" alt="%s"/>' % (html_escape(entity.download_url()), |
|
175 |
html_escape(entity.download_file_name()))) |
|
176 |