web/request.py
changeset 9601 e5a80bd337e8
parent 9582 46ed25d38fe2
child 9736 e0b3fcf1c205
child 9774 b7b71be569cf
equal deleted inserted replaced
9585:3f5b59527d31 9601:e5a80bd337e8
   120                 self._headers_in.addRawHeader(k, v)
   120                 self._headers_in.addRawHeader(k, v)
   121         #: form parameters
   121         #: form parameters
   122         self.setup_params(form)
   122         self.setup_params(form)
   123         #: received body
   123         #: received body
   124         self.content = StringIO()
   124         self.content = StringIO()
   125         # use header to set default language (may ne overwriten by user one later)
   125         # set up language based on request headers or site default (we don't
   126         if vreg.config.get('language-negociation', False):
   126         # have a user yet, and might not get one)
   127             # http negociated language
   127         self.set_user_language(None)
   128             accepted_languages = self.header_accept_language()
       
   129         else:
       
   130             accepted_languages = ()
       
   131         for lang in accepted_languages:
       
   132             if lang in self.translations:
       
   133                 self.set_language(lang)
       
   134                 break
       
   135         else:
       
   136             self.set_default_language(vreg)
       
   137         # 3. default language
       
   138         #: dictionary that may be used to store request data that has to be
   128         #: dictionary that may be used to store request data that has to be
   139         #: shared among various components used to publish the request (views,
   129         #: shared among various components used to publish the request (views,
   140         #: controller, application...)
   130         #: controller, application...)
   141         self.data = {}
   131         self.data = {}
   142         #:  search state: 'normal' or 'linksearch' (eg searching for an object
   132         #:  search state: 'normal' or 'linksearch' (eg searching for an object
   984         return False
   974         return False
   985 
   975 
   986     def html_content_type(self):
   976     def html_content_type(self):
   987         return 'text/html'
   977         return 'text/html'
   988 
   978 
       
   979     def set_user_language(self, user):
       
   980         vreg = self.vreg
       
   981         if user is not None:
       
   982             try:
       
   983                 # 1. user-specified language
       
   984                 lang = vreg.typed_value('ui.language', user.properties['ui.language'])
       
   985                 self.set_language(lang)
       
   986                 return
       
   987             except KeyError:
       
   988                 pass
       
   989         if vreg.config.get('language-negociation', False):
       
   990             # 2. http accept-language
       
   991             for lang in self.header_accept_language():
       
   992                 if lang in self.translations:
       
   993                     self.set_language(lang)
       
   994                     return
       
   995         # 3. site's default language
       
   996         self.set_default_language(vreg)
       
   997 
   989 
   998 
   990 class DBAPICubicWebRequestBase(_CubicWebRequestBase, DBAPIRequest):
   999 class DBAPICubicWebRequestBase(_CubicWebRequestBase, DBAPIRequest):
   991 
  1000 
   992     def set_session(self, session):
  1001     def set_session(self, session):
   993         """method called by the session handler when the user is authenticated
  1002         """method called by the session handler when the user is authenticated
   994         or an anonymous connection is open
  1003         or an anonymous connection is open
   995         """
  1004         """
   996         super(CubicWebRequestBase, self).set_session(session)
  1005         super(CubicWebRequestBase, self).set_session(session)
   997         # set request language
  1006         # set request language
   998         user_lang = self.user.properties.get('ui.language')
  1007         self.set_user_language(session.user)
   999         if user_lang is not None:
       
  1000             lang = self.vreg.typed_value('ui.language', user_lang)
       
  1001             self.set_language(lang)
       
  1002 
       
  1003 
  1008 
  1004 
  1009 
  1005 def _cnx_func(name):
  1010 def _cnx_func(name):
  1006     def proxy(req, *args, **kwargs):
  1011     def proxy(req, *args, **kwargs):
  1007         return getattr(req.cnx, name)(*args, **kwargs)
  1012         return getattr(req.cnx, name)(*args, **kwargs)
  1028 
  1033 
  1029     def set_cnx(self, cnx):
  1034     def set_cnx(self, cnx):
  1030         self.cnx = cnx
  1035         self.cnx = cnx
  1031         self.session = cnx._session
  1036         self.session = cnx._session
  1032         self._set_user(cnx.user)
  1037         self._set_user(cnx.user)
  1033         # set user language
  1038         self.set_user_language(cnx.user)
  1034         user_lang = self.user.properties.get('ui.language')
       
  1035         if user_lang is not None:
       
  1036             lang = self.vreg.typed_value('ui.language', user_lang)
       
  1037             self.set_language(lang)
       
  1038 
       
  1039 
  1039 
  1040     def execute(self, *args, **kwargs):
  1040     def execute(self, *args, **kwargs):
  1041         rset = self.cnx.execute(*args, **kwargs)
  1041         rset = self.cnx.execute(*args, **kwargs)
  1042         rset.req = self
  1042         rset.req = self
  1043         return rset
  1043         return rset