From 6c0785781480aeb64798b58bf1a40ab14171cd7d Mon Sep 17 00:00:00 2001 From: resteve Date: Mon, 14 Oct 2013 16:53:39 +0200 Subject: [PATCH 1/5] Transaction language by locale from path info --- nereid/application.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nereid/application.py b/nereid/application.py index 002e1bb..df58d4b 100644 --- a/nereid/application.py +++ b/nereid/application.py @@ -240,13 +240,13 @@ def database(self): """ return self._database - def transaction(self, http_host): + def transaction(self, environ): """ Allows the use of the transaction as a context manager. The transaction created loads the user from the known websites which is identified through the http_host """ - website_name = get_website_from_host(http_host) + website_name = get_website_from_host(environ.get('HTTP_HOST')) try: website = self.websites[website_name] except KeyError: @@ -259,6 +259,9 @@ def transaction(self, http_host): context = { 'company': website['company'], } + path = environ.get('PATH_INFO').split('/') + if len(path) > 2: + context['language'] = path[1] return TransactionManager( self.database_name, website['application_user'], context ) @@ -406,7 +409,7 @@ def wsgi_app(self, environ, start_response): if not self.initialised: self.initialise() - with self.transaction(environ['HTTP_HOST']) as txn: + with self.transaction(environ) as txn: with self.request_context(environ): try: response = self.full_dispatch_request() From 79d5a8756f453a5069b89b53e9cdaea22cf05c11 Mon Sep 17 00:00:00 2001 From: Raimon Esteve Date: Tue, 15 Oct 2013 08:52:27 +0200 Subject: [PATCH 2/5] Transaction. http_host variable --- nereid/application.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nereid/application.py b/nereid/application.py index df58d4b..c76cf04 100644 --- a/nereid/application.py +++ b/nereid/application.py @@ -246,7 +246,8 @@ def transaction(self, environ): The transaction created loads the user from the known websites which is identified through the http_host """ - website_name = get_website_from_host(environ.get('HTTP_HOST')) + http_host = environ.get('HTTP_HOST') + website_name = get_website_from_host(http_host) try: website = self.websites[website_name] except KeyError: From c2e11ef4ad8e0b0e03e27681574aa4720a8ddebc Mon Sep 17 00:00:00 2001 From: Raimon Esteve Date: Tue, 15 Oct 2013 16:56:32 +0200 Subject: [PATCH 3/5] Transation filter (translate selected fields) --- nereid/helpers.py | 24 ++++++++++++++++++++++++ nereid/templating.py | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/nereid/helpers.py b/nereid/helpers.py index 1fb7573..24057fe 100644 --- a/nereid/helpers.py +++ b/nereid/helpers.py @@ -16,6 +16,8 @@ get_flashed_messages, flash, url_for as flask_url_for) from werkzeug import Headers, wrap_file, redirect, abort from werkzeug.exceptions import NotFound +from trytond.pool import Pool +from trytond.transaction import Transaction from .globals import session, current_app, request @@ -310,6 +312,28 @@ def _rst_to_html_filter(value): except Exception: return value +def _translation_filter(value, name): + """ + Return a translation from ir.translation + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + :param value: str to translate + :param name: field name + + Example:: + {{ sale.state|translation('sale.sale,state') } + """ + locale = Transaction().language + Translation = Pool().get('ir.translation') + translations = Translation.search([ + ('lang', '=', locale), + ('name', '=', name), + ('src', 'ilike', value), + ], limit=1) + if translations: + translation, = translations + if translation.value: + return translation.value + return value def key_from_list(list_of_args): """ diff --git a/nereid/templating.py b/nereid/templating.py index 8bafa9f..11824ac 100644 --- a/nereid/templating.py +++ b/nereid/templating.py @@ -18,7 +18,7 @@ from trytond.transaction import Transaction from .globals import request, current_app # noqa -from .helpers import _rst_to_html_filter, make_crumbs +from .helpers import _rst_to_html_filter, _translation_filter, make_crumbs class LazyRenderer(_LazyString): @@ -116,6 +116,7 @@ def nereid_default_template_ctx_processor(): NEREID_TEMPLATE_FILTERS = dict( rst=_rst_to_html_filter, + translation=_translation_filter, ) From 11b32d7a2656854a688cd8d25b5a401d4a9c4a9e Mon Sep 17 00:00:00 2001 From: Raimon Esteve Date: Tue, 15 Oct 2013 17:19:20 +0200 Subject: [PATCH 4/5] pyflakes --- nereid/helpers.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/nereid/helpers.py b/nereid/helpers.py index 24057fe..75880ed 100644 --- a/nereid/helpers.py +++ b/nereid/helpers.py @@ -312,6 +312,7 @@ def _rst_to_html_filter(value): except Exception: return value + def _translation_filter(value, name): """ Return a translation from ir.translation @@ -325,16 +326,17 @@ def _translation_filter(value, name): locale = Transaction().language Translation = Pool().get('ir.translation') translations = Translation.search([ - ('lang', '=', locale), - ('name', '=', name), - ('src', 'ilike', value), - ], limit=1) + ('lang', '=', locale), + ('name', '=', name), + ('src', 'ilike', value), + ], limit=1) if translations: translation, = translations if translation.value: return translation.value return value + def key_from_list(list_of_args): """ Builds a key from a list of arguments which could be used for caching From 853734214c5eaf190c481981449f01026fe7fcb2 Mon Sep 17 00:00:00 2001 From: Raimon Esteve Date: Thu, 17 Oct 2013 11:31:45 +0200 Subject: [PATCH 5/5] Get value translation field from Model.fields_get --- nereid/helpers.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/nereid/helpers.py b/nereid/helpers.py index 75880ed..5d8d6b1 100644 --- a/nereid/helpers.py +++ b/nereid/helpers.py @@ -17,7 +17,6 @@ from werkzeug import Headers, wrap_file, redirect, abort from werkzeug.exceptions import NotFound from trytond.pool import Pool -from trytond.transaction import Transaction from .globals import session, current_app, request @@ -315,25 +314,20 @@ def _rst_to_html_filter(value): def _translation_filter(value, name): """ - Return a translation from ir.translation - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - :param value: str to translate - :param name: field name + Return a value from selection field + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + :param value: str key + :param name: str object,field Example:: - {{ sale.state|translation('sale.sale,state') } + {{ sale.state|translation('sale.sale,state') }} + {{ invoice.type|translation('account.invoice,type') }} """ - locale = Transaction().language - Translation = Pool().get('ir.translation') - translations = Translation.search([ - ('lang', '=', locale), - ('name', '=', name), - ('src', 'ilike', value), - ], limit=1) - if translations: - translation, = translations - if translation.value: - return translation.value + model, field = name.split(',') + Model = Pool().get(model) + fields = Model.fields_get(fields_names=[field]) + if fields.get('type').get('selection'): + return dict(fields.get('type').get('selection'))[value] return value