Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation filter on select field #101

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions nereid/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +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
"""
http_host = environ.get('HTTP_HOST')
website_name = get_website_from_host(http_host)
try:
website = self.websites[website_name]
Expand All @@ -259,6 +260,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
)
Expand Down Expand Up @@ -406,7 +410,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()
Expand Down
20 changes: 20 additions & 0 deletions nereid/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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 .globals import session, current_app, request

Expand Down Expand Up @@ -311,6 +312,25 @@ def _rst_to_html_filter(value):
return value


def _translation_filter(value, name):
"""
Return a value from selection field
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:param value: str key
:param name: str object,field

Example::
{{ sale.state|translation('sale.sale,state') }}
{{ invoice.type|translation('account.invoice,type') }}
"""
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


def key_from_list(list_of_args):
"""
Builds a key from a list of arguments which could be used for caching
Expand Down
3 changes: 2 additions & 1 deletion nereid/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -116,6 +116,7 @@ def nereid_default_template_ctx_processor():

NEREID_TEMPLATE_FILTERS = dict(
rst=_rst_to_html_filter,
translation=_translation_filter,
)


Expand Down