author | sylvain.thenault@logilab.fr |
Wed, 18 Feb 2009 01:42:07 +0100 | |
branch | tls-sprint |
changeset 750 | 89e997bc2bf1 |
parent 742 | 99115e029dca |
child 985 | 6a25c58a1c23 |
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 |
||
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' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
38 |
__select__ = (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' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
50 |
__select__ = 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' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
77 |
__select__ = 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): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
89 |
__select__ = 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): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
121 |
__select__ = 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' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
141 |
__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
|
142 |
|
0 | 143 |
title = _('image') |
144 |
||
145 |
def call(self): |
|
146 |
rset = self.rset |
|
147 |
for i in xrange(len(rset)): |
|
148 |
self.w(u'<div class="efile">') |
|
149 |
self.wview(self.id, rset, row=i, col=0) |
|
150 |
self.w(u'</div>') |
|
151 |
||
152 |
def cell_call(self, row, col): |
|
153 |
entity = self.entity(row, col) |
|
154 |
#if entity.data_format.startswith('image/'): |
|
155 |
self.w(u'<img src="%s" alt="%s"/>' % (html_escape(entity.download_url()), |
|
156 |
html_escape(entity.download_file_name()))) |
|
157 |