author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 11 Feb 2009 18:46:14 +0100 | |
changeset 604 | e85042d18b48 |
parent 431 | 18b4dd650ef8 |
child 631 | 99f5852f8604 |
child 957 | b1f68f725fe9 |
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' |
431 | 38 |
__selectors__ = (one_line_rset, 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
|
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' |
|
431 | 51 |
__selectors__ = (one_line_rset, implement_interface) |
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 |
|
431 | 80 |
__selectors__ = (implement_interface,) |
0 | 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): |
|
431 | 92 |
__selectors__ = (implement_interface,) |
0 | 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 |
class IDownloadableLineView(baseviews.OneLineView): |
|
431 | 125 |
__selectors__ = (implement_interface,) |
0 | 126 |
# don't kick default oneline view |
127 |
accepts_interfaces = (IDownloadable,) |
|
128 |
||
129 |
||
130 |
def cell_call(self, row, col, title=None, **kwargs): |
|
131 |
"""the secondary view is a link to download the file""" |
|
132 |
entity = self.entity(row, col) |
|
133 |
url = html_escape(entity.absolute_url()) |
|
134 |
name = html_escape(entity.download_file_name()) |
|
135 |
durl = html_escape(entity.download_url()) |
|
136 |
self.w(u'<a href="%s">%s</a> [<a href="%s">%s</a>]' % |
|
137 |
(url, name, durl, self.req._('download'))) |
|
138 |
||
139 |
||
140 |
class ImageView(baseviews.EntityView): |
|
431 | 141 |
__selectors__ = (implement_interface, score_entity_selector) |
0 | 142 |
id = 'image' |
143 |
title = _('image') |
|
144 |
accepts_interfaces = (IDownloadable,) |
|
145 |
||
146 |
def call(self): |
|
147 |
rset = self.rset |
|
148 |
for i in xrange(len(rset)): |
|
149 |
self.w(u'<div class="efile">') |
|
150 |
self.wview(self.id, rset, row=i, col=0) |
|
151 |
self.w(u'</div>') |
|
152 |
||
153 |
@classmethod |
|
154 |
def score_entity(cls, entity): |
|
155 |
mt = entity.download_content_type() |
|
156 |
if not (mt and mt.startswith('image/')): |
|
157 |
return 0 |
|
158 |
return 1 |
|
159 |
||
160 |
def cell_call(self, row, col): |
|
161 |
entity = self.entity(row, col) |
|
162 |
#if entity.data_format.startswith('image/'): |
|
163 |
self.w(u'<img src="%s" alt="%s"/>' % (html_escape(entity.download_url()), |
|
164 |
html_escape(entity.download_file_name()))) |
|
165 |