[web] Simplify a bit language handling
Drop `set_default_language` and `set_user_language` by considering that:
* before being bound to the user, request will have the site's default language
* cnx has the user's preferred language, hence get it back to the request when
it's bound to a connection
The first point requires some change to cubicweb's vreg faking so it doesn't
break.
That should be enough.
--- a/cubicweb/devtools/fake.py Thu Nov 17 10:53:04 2016 +0100
+++ b/cubicweb/devtools/fake.py Thu Nov 17 10:15:11 2016 +0100
@@ -55,12 +55,20 @@
return {'system': {'db-driver': 'sqlite'}}
+class FakeCWRegistryStore(CWRegistryStore):
+
+ def property_value(self, key):
+ if key == 'ui.language':
+ return 'en'
+ assert False
+
+
class FakeRequest(ConnectionCubicWebRequestBase):
"""test implementation of an cubicweb request object"""
def __init__(self, *args, **kwargs):
if not (args or 'vreg' in kwargs):
- kwargs['vreg'] = CWRegistryStore(FakeConfig(), initlog=False)
+ kwargs['vreg'] = FakeCWRegistryStore(FakeConfig(), initlog=False)
kwargs['https'] = False
self._http_method = kwargs.pop('method', 'GET')
self._url = kwargs.pop('url', None)
@@ -134,7 +142,7 @@
if vreg is None:
vreg = getattr(self.repo, 'vreg', None)
if vreg is None:
- vreg = CWRegistryStore(FakeConfig(), initlog=False)
+ vreg = FakeCWRegistryStore(FakeConfig(), initlog=False)
self.vreg = vreg
self.cnxset = FakeConnectionsSet()
self.user = user or FakeUser()
@@ -176,7 +184,7 @@
self._count = 0
self.schema = schema
self.config = config or FakeConfig()
- self.vreg = vreg or CWRegistryStore(self.config, initlog=False)
+ self.vreg = vreg or FakeCWRegistryStore(self.config, initlog=False)
self.vreg.schema = schema
def internal_session(self):
--- a/cubicweb/req.py Thu Nov 17 10:53:04 2016 +0100
+++ b/cubicweb/req.py Thu Nov 17 10:15:11 2016 +0100
@@ -96,7 +96,15 @@
Raises :exc:`KeyError` if translation doesn't exist.
"""
self.lang = lang
- gettext, pgettext = self.vreg.config.translations[lang]
+ try:
+ gettext, pgettext = self.vreg.config.translations[lang]
+ except KeyError:
+ assert self.vreg.config.mode == 'test'
+ gettext = text_type
+
+ def pgettext(x, y):
+ return text_type(y)
+
# use _cw.__ to translate a message without registering it to the catalog
self._ = self.__ = gettext
self.pgettext = pgettext
--- a/cubicweb/web/request.py Thu Nov 17 10:53:04 2016 +0100
+++ b/cubicweb/web/request.py Thu Nov 17 10:15:11 2016 +0100
@@ -146,10 +146,9 @@
#: Header used for the final response
self.headers_out = Headers()
#: HTTP status use by the final response
- self.status_out = 200
- # set up language based on request headers or site default (we don't
- # have a user yet, and might not get one)
- self.set_user_language(None)
+ self.status_out = 200
+ # set up language based on site default (we don't have a user yet, and might not get one)
+ self.set_language(vreg.property_value('ui.language'))
#: dictionary that may be used to store request data that has to be
#: shared among various components used to publish the request (views,
#: controller, application...)
@@ -929,18 +928,6 @@
return lang
return None
- def set_user_language(self, user):
- vreg = self.vreg
- if user is not None:
- try:
- lang = vreg.typed_value('ui.language', user.properties['ui.language'])
- self.set_language(lang)
- return
- except KeyError:
- pass
- # site's default language
- self.set_default_language(vreg)
-
def _cnx_func(name):
def proxy(req, *args, **kwargs):
@@ -993,25 +980,15 @@
self.cnx = cnx
self.session = cnx.session
self._set_user(cnx.user)
- self.set_user_language(cnx.user)
+ # set language according to the one defined on the connection which consider user's
+ # preference
+ self.set_language(cnx.lang)
def execute(self, *args, **kwargs):
rset = self.cnx.execute(*args, **kwargs)
rset.req = self
return rset
- def set_default_language(self, vreg):
- try:
- lang = vreg.property_value('ui.language')
- except Exception: # property may not be registered
- lang = 'en'
- try:
- self.set_language(lang)
- except KeyError:
- # this occurs usually during test execution
- self._ = self.__ = text_type
- self.pgettext = lambda x, y: text_type(y)
-
entity_metas = _cnx_func('entity_metas') # XXX deprecated
entity_type = _cnx_func('entity_type')
source_defs = _cnx_func('source_defs')
--- a/cubicweb/web/test/unittest_request.py Thu Nov 17 10:53:04 2016 +0100
+++ b/cubicweb/web/test/unittest_request.py Thu Nov 17 10:15:11 2016 +0100
@@ -6,7 +6,7 @@
from six import text_type
-from cubicweb.devtools.fake import FakeConfig
+from cubicweb.devtools.fake import FakeConfig, FakeCWRegistryStore
from cubicweb.web.request import (CubicWebRequestBase, _parse_accept_header,
_mimetype_sort_key, _mimetype_parser, _charset_sort_key)
@@ -72,8 +72,7 @@
class WebRequestTC(unittest.TestCase):
def test_base_url(self):
- dummy_vreg = type('DummyVreg', (object,), {})()
- dummy_vreg.config = FakeConfig()
+ dummy_vreg = FakeCWRegistryStore(FakeConfig(), initlog=False)
dummy_vreg.config['base-url'] = 'http://babar.com/'
dummy_vreg.config['https-url'] = 'https://toto.com/'
@@ -88,8 +87,7 @@
self.assertEqual('https://toto.com/', req.base_url(True))
def test_negotiated_language(self):
- vreg = type('DummyVreg', (object,), {})()
- vreg.config = FakeConfig()
+ vreg = FakeCWRegistryStore(FakeConfig(), initlog=False)
vreg.config.translations = {'fr': (None, None), 'en': (None, None)}
headers = {
'Accept-Language': 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3',
@@ -98,7 +96,7 @@
self.assertEqual(req.negotiated_language(), 'fr')
def test_build_url_language_from_url(self):
- vreg = type('DummyVreg', (object,), {'config': FakeConfig()})()
+ vreg = FakeCWRegistryStore(FakeConfig(), initlog=False)
vreg.config['base-url'] = 'http://testing.fr/cubicweb/'
vreg.config['language-mode'] = 'url-prefix'
vreg.config.translations['fr'] = text_type, text_type