web/request.py
changeset 8941 7b26fe71404f
parent 8889 be91151107f6
child 8974 c8520188eb4b
equal deleted inserted replaced
8940:ae898a084da2 8941:7b26fe71404f
    40 from logilab.mtconverter import xml_escape
    40 from logilab.mtconverter import xml_escape
    41 
    41 
    42 from cubicweb.dbapi import DBAPIRequest
    42 from cubicweb.dbapi import DBAPIRequest
    43 from cubicweb.uilib import remove_html_tags, js
    43 from cubicweb.uilib import remove_html_tags, js
    44 from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid
    44 from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid
    45 from cubicweb.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE_NOEXT
    45 from cubicweb.view import TRANSITIONAL_DOCTYPE_NOEXT
    46 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
    46 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
    47                           RequestError, StatusResponse)
    47                           RequestError, StatusResponse)
    48 from cubicweb.web.httpcache import GMTOFFSET, get_validators
    48 from cubicweb.web.httpcache import GMTOFFSET, get_validators
    49 from cubicweb.web.http_headers import Headers, Cookie, parseDateTime
    49 from cubicweb.web.http_headers import Headers, Cookie, parseDateTime
    50 
    50 
   900             value_parser = value_sort_key = None
   900             value_parser = value_sort_key = None
   901         accepteds = self.get_header(header, '')
   901         accepteds = self.get_header(header, '')
   902         values = _parse_accept_header(accepteds, value_parser, value_sort_key)
   902         values = _parse_accept_header(accepteds, value_parser, value_sort_key)
   903         return (raw_value for (raw_value, parsed_value, score) in values)
   903         return (raw_value for (raw_value, parsed_value, score) in values)
   904 
   904 
       
   905     @deprecated('[3.17] demote_to_html is deprecated as we always serve html')
   905     def demote_to_html(self):
   906     def demote_to_html(self):
   906         """helper method to dynamically set request content type to text/html
   907         """helper method to dynamically set request content type to text/html
   907 
   908 
   908         The global doctype and xmldec must also be changed otherwise the browser
   909         The global doctype and xmldec must also be changed otherwise the browser
   909         will display '<[' at the beginning of the page
   910         will display '<[' at the beginning of the page
   910         """
   911         """
   911         if not self.vreg.config['force-html-content-type']:
   912         pass
   912             if not hasattr(self, 'main_stream'):
   913 
   913                 raise Exception("Can't demote to html from an ajax context. You "
       
   914                                 "should change force-html-content-type to yes "
       
   915                                 "in the instance configuration file.")
       
   916             self.set_content_type('text/html')
       
   917             self.main_stream.set_doctype(TRANSITIONAL_DOCTYPE_NOEXT)
       
   918 
   914 
   919     # xml doctype #############################################################
   915     # xml doctype #############################################################
   920 
   916 
   921     def set_doctype(self, doctype, reset_xmldecl=True):
   917     def set_doctype(self, doctype, reset_xmldecl=None):
   922         """helper method to dynamically change page doctype
   918         """helper method to dynamically change page doctype
   923 
   919 
   924         :param doctype: the new doctype, e.g. '<!DOCTYPE html>'
   920         :param doctype: the new doctype, e.g. '<!DOCTYPE html>'
   925         :param reset_xmldecl: if True, remove the '<?xml version="1.0"?>'
   921         """
   926                               declaration from the page
   922         if reset_xmldecl is not None:
   927         """
   923             warn('[3.17] reset_xmldecl is deprecated as we only serve html',
       
   924                  DeprecationWarning, stacklevel=2)
   928         self.main_stream.set_doctype(doctype, reset_xmldecl)
   925         self.main_stream.set_doctype(doctype, reset_xmldecl)
   929 
   926 
   930     # page data management ####################################################
   927     # page data management ####################################################
   931 
   928 
   932     def get_page_data(self, key, default=None):
   929     def get_page_data(self, key, default=None):
   963 
   960 
   964     def ie_browser(self):
   961     def ie_browser(self):
   965         useragent = self.useragent()
   962         useragent = self.useragent()
   966         return useragent and 'MSIE' in useragent
   963         return useragent and 'MSIE' in useragent
   967 
   964 
       
   965     @deprecated('[3.17] xhtml_browser is deprecated (xhtml is no longer served)')
   968     def xhtml_browser(self):
   966     def xhtml_browser(self):
   969         """return True if the browser is considered as xhtml compatible.
   967         """return True if the browser is considered as xhtml compatible.
   970 
   968 
   971         If the instance is configured to always return text/html and not
   969         If the instance is configured to always return text/html and not
   972         application/xhtml+xml, this method will always return False, even though
   970         application/xhtml+xml, this method will always return False, even though
   973         this is semantically different
   971         this is semantically different
   974         """
   972         """
   975         if self.vreg.config['force-html-content-type']:
   973         return False
   976             return False
       
   977         useragent = self.useragent()
       
   978         # * MSIE/Konqueror does not support xml content-type
       
   979         # * Opera supports xhtml and handles namespaces properly but it breaks
       
   980         #   jQuery.attr()
       
   981         if useragent and ('MSIE' in useragent or 'KHTML' in useragent
       
   982                           or 'Opera' in useragent):
       
   983             return False
       
   984         return True
       
   985 
   974 
   986     def html_content_type(self):
   975     def html_content_type(self):
   987         if self.xhtml_browser():
       
   988             return 'application/xhtml+xml'
       
   989         return 'text/html'
   976         return 'text/html'
   990 
   977 
   991     def document_surrounding_div(self):
   978     def document_surrounding_div(self):
   992         if self.xhtml_browser():
       
   993             return (u'<?xml version="1.0"?>\n' + STRICT_DOCTYPE + # XXX encoding ?
       
   994                     u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">')
       
   995         return u'<div>'
   979         return u'<div>'
   996 
   980 
   997     @deprecated('[3.9] use req.uiprops[rid]')
   981     @deprecated('[3.9] use req.uiprops[rid]')
   998     def external_resource(self, rid, default=_MARKER):
   982     def external_resource(self, rid, default=_MARKER):
   999         """return a path to an external resource, using its identifier
   983         """return a path to an external resource, using its identifier