[R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 29 Jul 2009 10:44:36 +0200
changeset 2559 46859078c866
parent 2558 81c8b5312f9c
child 2560 fee7fb9832d8
[R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
web/request.py
web/test/unittest_views_basecontrollers.py
web/views/basecontrollers.py
web/views/basetemplates.py
--- a/web/request.py	Wed Jul 29 10:43:30 2009 +0200
+++ b/web/request.py	Wed Jul 29 10:44:36 2009 +0200
@@ -26,6 +26,7 @@
 from cubicweb.common.mail import header
 from cubicweb.common.uilib import remove_html_tags
 from cubicweb.utils import SizeConstrainedList, HTMLHead
+from cubicweb.view import STRICT_DOCTYPE
 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
                           RequestError, StatusResponse)
 
@@ -721,5 +722,11 @@
             return 'application/xhtml+xml'
         return 'text/html'
 
+    def document_surrounding_div(self):
+        if self.xhtml_browser():
+            return (u'<?xml version="1.0"?>\n' + STRICT_DOCTYPE +
+                    u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">')
+        return u'<div>'
+
 from cubicweb import set_log_methods
 set_log_methods(CubicWebRequestBase, LOGGER)
--- a/web/test/unittest_views_basecontrollers.py	Wed Jul 29 10:43:30 2009 +0200
+++ b/web/test/unittest_views_basecontrollers.py	Wed Jul 29 10:44:36 2009 +0200
@@ -8,16 +8,13 @@
 import simplejson
 
 from logilab.common.testlib import unittest_main, mock_object
-
-from cubicweb import Binary, Unauthorized
-from cubicweb.devtools._apptest import TestEnvironment
 from cubicweb.devtools.apptest import EnvBasedTC, ControllerTC
 
-from cubicweb.common import ValidationError
+from cubicweb import Binary, NoSelectableObject, ValidationError
+from cubicweb.view import STRICT_DOCTYPE
 from cubicweb.common.uilib import rql_for_eid
 
 from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError
-from cubicweb.web.views.basecontrollers import xhtml_wrap
 
 from cubicweb.entities.authobjs import CWUser
 
@@ -515,7 +512,7 @@
     def test_not_usable_by_guets(self):
         self.login('anon')
         req = self.request()
-        self.assertRaises(Unauthorized, self.env.app.select_controller, 'sendmail', req)
+        self.assertRaises(NoSelectableObject, self.env.vreg.select, 'controllers', 'sendmail', req)
 
 
 
@@ -538,8 +535,13 @@
         ctrl = self.ctrl(req)
         rset = self.john.as_rset()
         rset.req = req
-        self.assertTextEquals(ctrl.publish(),
-                              xhtml_wrap(mock_object(req=req), ctrl.view('primary', rset)))
+        source = ctrl.publish()
+        self.failUnless(source.startswith('<?xml version="1.0"?>\n' + STRICT_DOCTYPE +
+                                          u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">')
+                        )
+        req.xhtml_browser = lambda: False
+        source = ctrl.publish()
+        self.failUnless(source.startswith('<div>'))
 
 #     def test_json_exec(self):
 #         rql = 'Any T,N WHERE T is Tag, T name N'
--- a/web/views/basecontrollers.py	Wed Jul 29 10:43:30 2009 +0200
+++ b/web/views/basecontrollers.py	Wed Jul 29 10:44:36 2009 +0200
@@ -33,21 +33,6 @@
 except ImportError: # gae
     HAS_SEARCH_RESTRICTION = False
 
-def xhtml_wrap_header(self):
-    # XXX factor out, watch view.py ~ Maintemplate.doctype
-    if not self.vreg.config['force-html-content-type'] and self.req.xhtml_browser():
-        head = (u'<?xml version="1.0"?>\n' + STRICT_DOCTYPE +
-                u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">')
-    else:
-        head = u'<div>'
-    return head
-
-def xhtml_wrap_tail(self):
-    return u'</div>'
-
-def xhtml_wrap(self, source):
-    return u''.join((xhtml_wrap_header(self), source.strip(), xhtml_wrap_tail(self)))
-
 def jsonize(func):
     """decorator to sets correct content_type and calls `simplejson.dumps` on
     results
@@ -63,7 +48,8 @@
     def wrapper(self, *args, **kwargs):
         self.req.set_content_type(self.req.html_content_type())
         result = func(self, *args, **kwargs)
-        return xhtml_wrap(self, result)
+        return ''.join((self.req.document_surrounding_div(), result.strip(),
+                        u'</div>'))
     wrapper.__name__ = func.__name__
     return wrapper
 
--- a/web/views/basetemplates.py	Wed Jul 29 10:43:30 2009 +0200
+++ b/web/views/basetemplates.py	Wed Jul 29 10:44:36 2009 +0200
@@ -13,7 +13,6 @@
 from cubicweb.vregistry import objectify_selector
 from cubicweb.selectors import match_kwargs
 from cubicweb.view import View, MainTemplate, NOINDEX, NOFOLLOW
-from cubicweb.web.views.basecontrollers import xhtml_wrap_header, xhtml_wrap_tail
 from cubicweb.utils import make_uid, UStringIO
 
 
@@ -85,12 +84,15 @@
     def call(self, view):
         view.set_request_content_type()
         view.set_stream()
-        if view.content_type == self.req.html_content_type():
-            view.w(xhtml_wrap_header(self))
+        if (self.req.form.has_key('__notemplate') and view.templatable
+            and view.content_type == self.req.html_content_type()):
+            view.w(self.req.document_surrounding_div())
             view.render()
-            view.w(xhtml_wrap_tail(self))
+            view.w(u'</div>')
         else:
             view.render()
+        # have to replace our stream by view's stream (which may be a binary
+        # stream)
         self._stream = view._stream