author | Sylvain Thenault <sylvain.thenault@logilab.fr> |
Mon, 05 Jan 2009 15:44:33 +0100 | |
changeset 324 | 9b51dac0bac2 |
parent 249 | 5ab64969df20 |
child 326 | 44b6345ccd3c |
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, |
249
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
14 |
interface_selector) |
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' |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
38 |
__selectors__ = (one_line_rset, interface_selector) |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
39 |
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
|
40 |
order = 10 |
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
41 |
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
|
42 |
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
|
43 |
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
|
44 |
|
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
45 |
|
0 | 46 |
class DownloadView(baseviews.EntityView): |
47 |
"""this view is replacing the deprecated 'download' controller and allow downloading |
|
48 |
of entities providing the necessary interface |
|
49 |
""" |
|
50 |
id = 'download' |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
51 |
__selectors__ = (one_line_rset, interface_selector) |
0 | 52 |
accepts_interfaces = (IDownloadable,) |
53 |
||
54 |
templatable = False |
|
55 |
content_type = 'application/octet-stream' |
|
56 |
binary = True |
|
57 |
add_to_breadcrumbs = False |
|
58 |
||
59 |
def set_request_content_type(self): |
|
60 |
"""overriden to set the correct filetype and filename""" |
|
61 |
entity = self.complete_entity(0) |
|
62 |
encoding = entity.download_encoding() |
|
63 |
if encoding in BINARY_ENCODINGS: |
|
64 |
contenttype = 'application/%s' % encoding |
|
65 |
encoding = None |
|
66 |
else: |
|
67 |
contenttype = entity.download_content_type() |
|
68 |
self.req.set_content_type(contenttype or self.content_type, |
|
69 |
filename=entity.download_file_name(), |
|
70 |
encoding=encoding) |
|
71 |
||
72 |
def call(self): |
|
73 |
self.w(self.complete_entity(0).download_data()) |
|
74 |
||
75 |
||
76 |
class DownloadLinkView(baseviews.EntityView): |
|
77 |
"""view displaying a link to download the file""" |
|
78 |
id = 'downloadlink' |
|
79 |
title = None # should not be listed in possible views |
|
80 |
__selectors__ = (interface_selector,) |
|
81 |
||
82 |
accepts_interfaces = (IDownloadable,) |
|
83 |
||
84 |
def cell_call(self, row, col, title=None, **kwargs): |
|
85 |
entity = self.entity(row, col) |
|
86 |
url = html_escape(entity.download_url()) |
|
87 |
self.w(u'<a href="%s">%s</a>' % (url, html_escape(title or entity.dc_title()))) |
|
88 |
||
89 |
||
90 |
||
91 |
class IDownloadablePrimaryView(baseviews.PrimaryView): |
|
92 |
__selectors__ = (interface_selector,) |
|
93 |
#skip_attrs = ('eid', 'data',) # XXX |
|
94 |
accepts_interfaces = (IDownloadable,) |
|
95 |
||
96 |
def render_entity_title(self, entity): |
|
97 |
self.w(u'<h1>%s %s</h1>' |
|
98 |
% (entity.dc_type().capitalize(), |
|
99 |
html_escape(entity.dc_title()))) |
|
100 |
||
101 |
def render_entity_attributes(self, entity, siderelations): |
|
102 |
super(IDownloadablePrimaryView, self).render_entity_attributes(entity, siderelations) |
|
103 |
self.wview('downloadlink', entity.rset, title=self.req._('download'), row=entity.row) |
|
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: |
|
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>') |
|
118 |
||
119 |
def is_side_related(self, rschema, eschema): |
|
120 |
"""display all relations as side related""" |
|
121 |
return True |
|
122 |
||
123 |
||
124 |
def render_side_related(self, entity, siderelations): |
|
125 |
download_box(self.w, entity) |
|
126 |
super(IDownloadablePrimaryView, self).render_side_related(entity, siderelations) |
|
127 |
||
128 |
class IDownloadableLineView(baseviews.OneLineView): |
|
129 |
__selectors__ = (interface_selector,) |
|
130 |
# don't kick default oneline view |
|
131 |
accepts_interfaces = (IDownloadable,) |
|
132 |
||
133 |
||
134 |
def cell_call(self, row, col, title=None, **kwargs): |
|
135 |
"""the secondary view is a link to download the file""" |
|
136 |
entity = self.entity(row, col) |
|
137 |
url = html_escape(entity.absolute_url()) |
|
138 |
name = html_escape(entity.download_file_name()) |
|
139 |
durl = html_escape(entity.download_url()) |
|
140 |
self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' % |
|
141 |
(url, name, durl, self.req._('download'))) |
|
142 |
||
143 |
||
144 |
class ImageView(baseviews.EntityView): |
|
145 |
__selectors__ = (interface_selector, score_entity_selector) |
|
146 |
id = 'image' |
|
147 |
title = _('image') |
|
148 |
accepts_interfaces = (IDownloadable,) |
|
149 |
||
150 |
def call(self): |
|
151 |
rset = self.rset |
|
152 |
for i in xrange(len(rset)): |
|
153 |
self.w(u'<div class="efile">') |
|
154 |
self.wview(self.id, rset, row=i, col=0) |
|
155 |
self.w(u'</div>') |
|
156 |
||
157 |
@classmethod |
|
158 |
def score_entity(cls, entity): |
|
159 |
mt = entity.download_content_type() |
|
160 |
if not (mt and mt.startswith('image/')): |
|
161 |
return 0 |
|
162 |
return 1 |
|
163 |
||
164 |
def cell_call(self, row, col): |
|
165 |
entity = self.entity(row, col) |
|
166 |
#if entity.data_format.startswith('image/'): |
|
167 |
self.w(u'<img src="%s" alt="%s"/>' % (html_escape(entity.download_url()), |
|
168 |
html_escape(entity.download_file_name()))) |
|
169 |