[googlemap] #344872 set request content-type to text/html stable
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 02 Sep 2009 10:59:48 +0200
branchstable
changeset 3094 978ed8c2c0e4
parent 3093 12a69f7f963b
child 3099 006be45923da
[googlemap] #344872 set request content-type to text/html
utils.py
web/request.py
web/views/igeocodable.py
--- a/utils.py	Wed Sep 02 09:31:22 2009 +0200
+++ b/utils.py	Wed Sep 02 10:59:48 2009 +0200
@@ -308,7 +308,8 @@
         self.htmltag = u'<html xmlns="http://www.w3.org/1999/xhtml" ' \
                        'xmlns:cubicweb="http://www.logilab.org/2008/cubicweb" ' \
                        'xml:lang="%s" lang="%s">' % (req.lang, req.lang)
-
+        # keep main_stream's reference on req for easier text/html demoting
+        req.main_stream = self
 
     def write(self, data):
         """StringIO interface: this method will be assigned to self.w
--- a/web/request.py	Wed Sep 02 09:31:22 2009 +0200
+++ b/web/request.py	Wed Sep 02 10:59:48 2009 +0200
@@ -26,7 +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.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE_NOEXT
 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
                           RequestError, StatusResponse)
 
@@ -663,6 +663,16 @@
         """
         raise NotImplementedError()
 
+    def demote_to_html(self):
+        """helper method to dynamically set request content type to text/html
+
+        The global doctype and xmldec must also be changed otherwise the browser
+        will display '<[' at the beginning of the page
+        """
+        self.set_content_type('text/html')
+        self.main_stream.doctype = TRANSITIONAL_DOCTYPE_NOEXT
+        self.main_stream.xmldecl = u''
+
     # page data management ####################################################
 
     def get_page_data(self, key, default=None):
--- a/web/views/igeocodable.py	Wed Sep 02 09:31:22 2009 +0200
+++ b/web/views/igeocodable.py	Wed Sep 02 10:59:48 2009 +0200
@@ -74,6 +74,7 @@
     need_navigation = False
 
     def call(self, gmap_key, width=400, height=400, uselabel=True, urlparams=None):
+        self.req.demote_to_html()
         # remove entities that don't define latitude and longitude
         self.rset = self.rset.filtered_rset(lambda e: e.latitude and e.longitude)
         self.req.add_js('http://maps.google.com/maps?sensor=false&file=api&amp;v=2&amp;key=%s' % gmap_key,