author | sylvain.thenault@logilab.fr |
Mon, 16 Feb 2009 18:26:13 +0100 | |
branch | tls-sprint |
changeset 631 | 99f5852f8604 |
parent 431 | 18b4dd650ef8 |
child 692 | 800592b8d39b |
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 |
||
11 |
from cubicweb.interfaces import IDownloadable |
|
12 |
from cubicweb.common.mttransforms import ENGINE |
|
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
|
13 |
from cubicweb.common.selectors import (one_line_rset, score_entity, |
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
|
14 |
implements, 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' |
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
|
38 |
__selectors__ = (one_line_rset, implements(IDownloadable), 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 |
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
|
40 |
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
|
41 |
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
|
42 |
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
|
43 |
|
5ab64969df20
define an actual download box, keeping download_box function for bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
44 |
|
0 | 45 |
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
|
46 |
"""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
|
47 |
downloading of entities providing the necessary interface |
0 | 48 |
""" |
49 |
id = 'download' |
|
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
|
50 |
__selectors__ = (one_line_rset, implements(IDownloadable)) |
0 | 51 |
|
52 |
templatable = False |
|
53 |
content_type = 'application/octet-stream' |
|
54 |
binary = True |
|
55 |
add_to_breadcrumbs = False |
|
56 |
||
57 |
def set_request_content_type(self): |
|
58 |
"""overriden to set the correct filetype and filename""" |
|
59 |
entity = self.complete_entity(0) |
|
60 |
encoding = entity.download_encoding() |
|
61 |
if encoding in BINARY_ENCODINGS: |
|
62 |
contenttype = 'application/%s' % encoding |
|
63 |
encoding = None |
|
64 |
else: |
|
65 |
contenttype = entity.download_content_type() |
|
66 |
self.req.set_content_type(contenttype or self.content_type, |
|
67 |
filename=entity.download_file_name(), |
|
68 |
encoding=encoding) |
|
69 |
||
70 |
def call(self): |
|
71 |
self.w(self.complete_entity(0).download_data()) |
|
72 |
||
73 |
||
74 |
class DownloadLinkView(baseviews.EntityView): |
|
75 |
"""view displaying a link to download the file""" |
|
76 |
id = 'downloadlink' |
|
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
|
77 |
__selectors__ = (implements(IDownloadable),) |
0 | 78 |
title = None # should not be listed in possible views |
79 |
||
80 |
||
81 |
def cell_call(self, row, col, title=None, **kwargs): |
|
82 |
entity = self.entity(row, col) |
|
83 |
url = html_escape(entity.download_url()) |
|
84 |
self.w(u'<a href="%s">%s</a>' % (url, html_escape(title or entity.dc_title()))) |
|
85 |
||
86 |
||
87 |
||
88 |
class IDownloadablePrimaryView(baseviews.PrimaryView): |
|
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
|
89 |
__selectors__ = (implements(IDownloadable),) |
0 | 90 |
#skip_attrs = ('eid', 'data',) # XXX |
91 |
||
92 |
def render_entity_title(self, entity): |
|
93 |
self.w(u'<h1>%s %s</h1>' |
|
94 |
% (entity.dc_type().capitalize(), |
|
95 |
html_escape(entity.dc_title()))) |
|
96 |
||
97 |
def render_entity_attributes(self, entity, siderelations): |
|
98 |
super(IDownloadablePrimaryView, self).render_entity_attributes(entity, siderelations) |
|
99 |
self.wview('downloadlink', entity.rset, title=self.req._('download'), row=entity.row) |
|
100 |
self.w(u'<div class="content">') |
|
101 |
contenttype = entity.download_content_type() |
|
102 |
if contenttype.startswith('image/'): |
|
103 |
self.wview('image', entity.rset, row=entity.row) |
|
104 |
else: |
|
105 |
try: |
|
106 |
if ENGINE.has_input(contenttype): |
|
107 |
self.w(entity.printable_value('data')) |
|
108 |
except TransformError: |
|
109 |
pass |
|
110 |
except Exception, ex: |
|
111 |
msg = self.req._("can't display data, unexpected error: %s") % ex |
|
112 |
self.w('<div class="error">%s</div>' % msg) |
|
113 |
self.w(u'</div>') |
|
114 |
||
115 |
def is_side_related(self, rschema, eschema): |
|
116 |
"""display all relations as side related""" |
|
117 |
return True |
|
118 |
||
119 |
||
120 |
class IDownloadableLineView(baseviews.OneLineView): |
|
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
|
121 |
__selectors__ = (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 |
||
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
|
133 |
def is_image(entity): |
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
|
134 |
mt = entity.download_content_type() |
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
|
135 |
if not (mt and mt.startswith('image/')): |
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
|
136 |
return 0 |
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
|
137 |
return 1 |
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
|
138 |
|
0 | 139 |
class ImageView(baseviews.EntityView): |
140 |
id = '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
|
141 |
__selectors__ = (implements(IDownloadable), |
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
|
142 |
score_entity(is_image)) |
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
|
143 |
|
0 | 144 |
title = _('image') |
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 |
def cell_call(self, row, col): |
|
154 |
entity = self.entity(row, col) |
|
155 |
#if entity.data_format.startswith('image/'): |
|
156 |
self.w(u'<img src="%s" alt="%s"/>' % (html_escape(entity.download_url()), |
|
157 |
html_escape(entity.download_file_name()))) |
|
158 |