fix html headers for forced html content type : main template and json controller
authorFabrice <fabrice@secondweb.fr>
Tue, 28 Jul 2009 10:23:48 +0200
changeset 2555 ca7b122f34fa
parent 2554 3b6a6d2f9d7e
child 2556 4608d259c3db
fix html headers for forced html content type : main template and json controller
view.py
web/views/basecontrollers.py
web/views/basetemplates.py
--- a/view.py	Tue Jul 28 10:19:14 2009 +0200
+++ b/view.py	Tue Jul 28 10:23:48 2009 +0200
@@ -403,7 +403,7 @@
 
     @property
     def doctype(self):
-        if self.req.xhtml_browser():
+        if not self.config['force-html-content-type'] and self.req.xhtml_browser():
             return STRICT_DOCTYPE
         return STRICT_DOCTYPE_NOEXT
 
--- a/web/views/basecontrollers.py	Tue Jul 28 10:19:14 2009 +0200
+++ b/web/views/basecontrollers.py	Tue Jul 28 10:23:48 2009 +0200
@@ -33,15 +33,20 @@
 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):
-    # XXX factor out, watch view.py ~ Maintemplate.doctype
-    if self.req.xhtml_browser():
-        dt = STRICT_DOCTYPE
-    else:
-        dt = STRICT_DOCTYPE_NOEXT
-    head = u'<?xml version="1.0"?>\n' + dt
-    return head + u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">%s</div>' % source.strip()
+    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
--- a/web/views/basetemplates.py	Tue Jul 28 10:19:14 2009 +0200
+++ b/web/views/basetemplates.py	Tue Jul 28 10:23:48 2009 +0200
@@ -13,8 +13,10 @@
 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
 
+
 # main templates ##############################################################
 
 class LogInOutTemplate(MainTemplate):
@@ -83,15 +85,12 @@
     def call(self, view):
         view.set_request_content_type()
         view.set_stream()
-        xhtml_wrap = (self.req.form.has_key('__notemplate') and view.templatable
-                      and view.content_type == self.req.html_content_type())
-        if xhtml_wrap:
-            view.w(u'<?xml version="1.0"?>\n' + self.doctype)
-            view.w(u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">')
-        # have to replace our unicode stream using view's binary stream
-        view.render()
-        if xhtml_wrap:
-            view.w(u'</div>')
+        if view.content_type == self.req.html_content_type():
+            view.w(xhtml_wrap_header(self))
+            view.render()
+            view.w(xhtml_wrap_tail(self))
+        else:
+            view.render()
         self._stream = view._stream