fix idownload view pb. for xhtml browsers stable
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 30 Jul 2010 17:19:07 +0200
branchstable
changeset 6048 4695b1ee58a0
parent 6046 3fd4a34c4a09
child 6049 f0ae98fa85a1
fix idownload view pb. for xhtml browsers
tags.py
web/test/unittest_viewselector.py
web/views/idownloadable.py
--- a/tags.py	Fri Jul 30 13:18:08 2010 +0200
+++ b/tags.py	Fri Jul 30 17:19:07 2010 +0200
@@ -48,6 +48,7 @@
 tr = tag('tr')
 th = tag('th')
 td = tag('td')
+iframe = tag('iframe')
 
 def select(name, id=None, multiple=False, options=[], **attrs):
     if multiple:
--- a/web/test/unittest_viewselector.py	Fri Jul 30 13:18:08 2010 +0200
+++ b/web/test/unittest_viewselector.py	Fri Jul 30 17:19:07 2010 +0200
@@ -418,17 +418,27 @@
 
     def test_score_entity_selector(self):
         image = self.request().create_entity('File', data_name=u'bim.png', data=Binary('bim'))
-        # image primary view priority
+        # image/ehtml primary view priority
         req = self.request()
         rset = req.execute('File X WHERE X data_name "bim.png"')
         self.assertIsInstance(self.vreg['views'].select('image', req, rset=rset),
                               idownloadable.ImageView)
-        fileobj = self.request().create_entity('File', data_name=u'bim.txt', data=Binary('bim'))
-        # image primary view priority
+        self.assertRaises(NoSelectableObject, self.vreg['views'].select, 'ehtml', req, rset=rset)
+
+        fileobj = self.request().create_entity('File', data_name=u'bim.html', data=Binary('<html>bam</html'))
+        # image/ehtml primary view priority
+        req = self.request()
+        rset = req.execute('File X WHERE X data_name "bim.html"')
+        self.assertIsInstance(self.vreg['views'].select('ehtml', req, rset=rset),
+                              idownloadable.EHTMLView)
+        self.assertRaises(NoSelectableObject, self.vreg['views'].select, 'image', req, rset=rset)
+
+        fileobj = self.request().create_entity('File', data_name=u'bim.txt', data=Binary('boum'))
+        # image/ehtml primary view priority
         req = self.request()
         rset = req.execute('File X WHERE X data_name "bim.txt"')
         self.assertRaises(NoSelectableObject, self.vreg['views'].select, 'image', req, rset=rset)
-
+        self.assertRaises(NoSelectableObject, self.vreg['views'].select, 'ehtml', req, rset=rset)
 
 
     def _test_view(self, vid, rql, args):
--- a/web/views/idownloadable.py	Fri Jul 30 13:18:08 2010 +0200
+++ b/web/views/idownloadable.py	Fri Jul 30 17:19:07 2010 +0200
@@ -121,6 +121,10 @@
             self.wview('image', entity.cw_rset, row=entity.cw_row, col=entity.cw_col,
                        link=True, klass='contentimage')
             super(IDownloadablePrimaryView, self).render_entity_attributes(entity)
+        elif contenttype.endswith('html'):
+            self.wview('downloadlink', entity.cw_rset, title=self._cw._('download'), row=entity.cw_row)
+            self.wview('ehtml', entity.cw_rset, row=entity.cw_row, col=entity.cw_col,
+                       height='600px', width='100%')
         else:
             super(IDownloadablePrimaryView, self).render_entity_attributes(entity)
             self.wview('downloadlink', entity.cw_rset, title=self._cw._('download'), row=entity.cw_row)
@@ -156,11 +160,10 @@
                (url, name, durl, self._cw._('download')))
 
 
-class ImageView(EntityView):
-    __regid__ = 'image'
-    __select__ = has_mimetype('image/')
+class AbstractEmbeddedView(EntityView):
+    __abstract__ = True
 
-    title = _('image')
+    _embedding_tag = None
 
     def call(self, **kwargs):
         rset = self.cw_rset
@@ -172,13 +175,29 @@
     def cell_call(self, row, col, link=False, **kwargs):
         entity = self.cw_rset.get_entity(row, col)
         adapter = entity.cw_adapt_to('IDownloadable')
-        imgtag = tags.img(src=adapter.download_url(),
-                          alt=(self._cw._('download %s') % adapter.download_file_name()),
-                          **kwargs)
+        tag = self._embedding_tag(src=adapter.download_url(),
+                                  alt=(self._cw._('download %s') % adapter.download_file_name()),
+                                  **kwargs)
         if link:
-            self.w(u'<a href="%s">%s</a>' % (entity.absolute_url(vid='download'),
-                                             imgtag))
+            self.w(u'<a href="%s">%s</a>' % (adapter.download_url(), tag))
         else:
-            self.w(imgtag)
+            self.w(tag)
 
 
+class ImageView(AbstractEmbeddedView):
+    __regid__ = 'image'
+    __select__ = has_mimetype('image/')
+
+    title = _('image')
+    _embedding_tag = tags.img
+
+
+class EHTMLView(AbstractEmbeddedView):
+    __regid__ = 'ehtml'
+    __select__ = has_mimetype('text/html')
+
+    title = _('embedded html')
+    _embedding_tag = tags.iframe
+
+
+