[idownloadable] refactor IDownloadable primary view to make overriding easier

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changeset04a8e48f10bc
branchdefault
phasepublic
hiddenno
parent revision#605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
child revision#2c3e83817a8e [view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
files modified by this revision
web/views/idownloadable.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1276172209 -7200
# Thu Jun 10 14:16:49 2010 +0200
# Node ID 04a8e48f10bca41af7e8edb92c55b3fbe15f3f89
# Parent 605f571198eb175a02fb465839a133a6c2a78626
[idownloadable] refactor IDownloadable primary view to make overriding easier

diff --git a/web/views/idownloadable.py b/web/views/idownloadable.py
@@ -114,22 +114,26 @@
1          contenttype = adapter.download_content_type()
2          if contenttype.startswith('image/'):
3              self.wview('image', entity.cw_rset, row=entity.cw_row)
4          else:
5              self.wview('downloadlink', entity.cw_rset, title=self._cw._('download'), row=entity.cw_row)
6 -            try:
7 -                if ENGINE.has_input(contenttype):
8 -                    # XXX expect File like schema (access to 'data' attribute)
9 -                    self.w(entity.printable_value('data'))
10 -            except TransformError:
11 -                pass
12 -            except Exception, ex:
13 -                msg = self._cw._("can't display data, unexpected error: %s") \
14 -                      % xml_escape(str(ex))
15 -                self.w('<div class="error">%s</div>' % msg)
16 +            self.render_data(entity, contenttype, 'text/html')
17          self.w(u'</div>')
18 
19 +    def render_data(self, entity, sourcemt, targetmt):
20 +        adapter = entity.cw_adapt_to('IDownloadable')
21 +        if ENGINE.find_path(sourcemt, targetmt):
22 +            try:
23 +                self.w(entity._cw_mtc_transform(adapter.download_data(), sourcemt,
24 +                                                targetmt, adapter.download_encoding()))
25 +            except Exception, ex:
26 +                self.exception('while rendering data for %s', entity)
27 +                msg = self._cw._("can't display data, unexpected error: %s") \
28 +                      % xml_escape(unicode(ex))
29 +                self.w('<div class="error">%s</div>' % msg)
30 +            return True
31 +        return False
32 
33  class IDownloadableLineView(baseviews.OneLineView):
34      __select__ = adaptable('IDownloadable')
35 
36      def cell_call(self, row, col, title=None, **kwargs):