[MERGE] forward port of branch 8.0 up to 2b192be
[odoo/odoo.git] / addons / website / models / ir_http.py
index bda1275..44f9dca 100644 (file)
@@ -65,7 +65,7 @@ class ir_http(orm.AbstractModel):
                     import GeoIP
                     # updated database can be downloaded on MaxMind website
                     # http://dev.maxmind.com/geoip/legacy/install/city/
-                    geofile = config.get('geoip_database', '/usr/share/GeoIP/GeoLiteCity.dat')
+                    geofile = config.get('geoip_database')
                     if os.path.exists(geofile):
                         self.geo_ip_resolver = GeoIP.open(geofile, GeoIP.GEOIP_STANDARD)
                     else:
@@ -78,12 +78,17 @@ class ir_http(orm.AbstractModel):
             request.session['geoip'] = record
             
         if request.website_enabled:
-            if func:
-                self._authenticate(func.routing['auth'])
-            else:
-                self._auth_method_public()
+            try:
+                if func:
+                    self._authenticate(func.routing['auth'])
+                else:
+                    self._auth_method_public()
+            except Exception as e:
+                return self._handle_exception(e)
+
             request.redirect = lambda url, code=302: werkzeug.utils.redirect(url_for(url), code)
             request.website = request.registry['website'].get_current_website(request.cr, request.uid, context=request.context)
+            request.context['website_id'] = request.website.id
             langs = [lg[0] for lg in request.website.get_languages()]
             path = request.httprequest.path.split('/')
             if first_pass:
@@ -107,6 +112,8 @@ class ir_http(orm.AbstractModel):
                         request.lang = request.website.default_lang_code
 
             request.context['lang'] = request.lang
+            if not request.context.get('tz'):
+                request.context['tz'] = request.session['geoip'].get('time_zone')
             if not func:
                 if path[1] in langs:
                     request.lang = request.context['lang'] = path.pop(1)
@@ -114,8 +121,10 @@ class ir_http(orm.AbstractModel):
                     if request.lang == request.website.default_lang_code:
                         # If language is in the url and it is the default language, redirect
                         # to url without language so google doesn't see duplicate content
-                        return request.redirect(path + '?' + request.httprequest.query_string)
+                        return request.redirect(path + '?' + request.httprequest.query_string, code=301)
                     return self.reroute(path)
+            # bind modified context
+            request.website = request.website.with_context(request.context)
         return super(ir_http, self)._dispatch()
 
     def reroute(self, path):
@@ -155,7 +164,7 @@ class ir_http(orm.AbstractModel):
                     path = '/' + request.lang + path
                 if request.httprequest.query_string:
                     path += '?' + request.httprequest.query_string
-                return werkzeug.utils.redirect(path)
+                return werkzeug.utils.redirect(path, code=301)
 
     def _serve_attachment(self):
         domain = [('type', '=', 'binary'), ('url', '=', request.httprequest.path)]
@@ -208,7 +217,14 @@ class ir_http(orm.AbstractModel):
                 exception=exception,
                 traceback=traceback.format_exc(exception),
             )
-            code = getattr(exception, 'code', code)
+
+            if isinstance(exception, werkzeug.exceptions.HTTPException):
+                if exception.code is None:
+                    # Hand-crafted HTTPException likely coming from abort(),
+                    # usually for a redirect response -> return it directly
+                    return exception
+                else:
+                    code = exception.code
 
             if isinstance(exception, openerp.exceptions.AccessError):
                 code = 403
@@ -218,11 +234,6 @@ class ir_http(orm.AbstractModel):
                 if isinstance(exception.qweb.get('cause'), openerp.exceptions.AccessError):
                     code = 403
 
-            if isinstance(exception, werkzeug.exceptions.HTTPException) and code is None:
-                # Hand-crafted HTTPException likely coming from abort(),
-                # usually for a redirect response -> return it directly
-                return exception
-
             if code == 500:
                 logger.error("500 Internal Server Error:\n\n%s", values['traceback'])
                 if 'qweb_exception' in values: