Left: | ||
Right: |
OLD | NEW |
---|---|
1 # This file is part of flask_tryton. The COPYRIGHT file at the top level of | 1 # This file is part of flask_tryton. The COPYRIGHT file at the top level of |
2 # this repository contains the full copyright notices and license terms. | 2 # this repository contains the full copyright notices and license terms. |
3 | 3 |
4 from functools import wraps | 4 from functools import wraps |
5 | 5 |
6 from flask import request, current_app | 6 from flask import request, current_app |
7 from werkzeug.routing import BaseConverter | 7 from werkzeug.routing import BaseConverter |
8 from werkzeug.exceptions import BadRequest | 8 from werkzeug.exceptions import BadRequest |
9 | 9 |
10 try: | 10 try: |
11 from trytond import __version__ as trytond_version | 11 from trytond import __version__ as trytond_version |
12 except ImportError: | 12 except ImportError: |
13 from trytond.version import VERSION as trytond_version | 13 from trytond.version import VERSION as trytond_version |
14 trytond_version = tuple(map(int, trytond_version.split('.'))) | 14 trytond_version = tuple(map(int, trytond_version.split('.'))) |
15 try: | 15 try: |
16 from trytond.config import config | 16 from trytond.config import config |
17 except ImportError: | 17 except ImportError: |
18 from trytond.config import CONFIG as config | 18 from trytond.config import CONFIG as config |
19 | 19 |
20 from trytond.pool import Pool | 20 from trytond.pool import Pool |
21 from trytond.transaction import Transaction | 21 from trytond.transaction import Transaction |
22 from trytond.cache import Cache | 22 from trytond.cache import Cache |
23 from trytond import backend | 23 from trytond import backend |
24 from trytond.report import Report | |
24 from trytond.exceptions import UserError, UserWarning, ConcurrencyException | 25 from trytond.exceptions import UserError, UserWarning, ConcurrencyException |
25 | 26 |
26 __version__ = '0.8.1' | 27 __version__ = '0.8.1' |
27 __all__ = ['Tryton', 'tryton_transaction'] | 28 __all__ = ['Tryton', 'tryton_transaction'] |
28 | 29 |
29 | 30 |
30 def retry_transaction(retry): | 31 def retry_transaction(retry): |
31 """Decorator to retry a transaction if failed. The decorated method | 32 """Decorator to retry a transaction if failed. The decorated method |
32 will be run retry times in case of DatabaseOperationalError. | 33 will be run retry times in case of DatabaseOperationalError. |
33 """ | 34 """ |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 | 74 |
74 self.pool = Pool(database) | 75 self.pool = Pool(database) |
75 with Transaction().start(database, user, readonly=True): | 76 with Transaction().start(database, user, readonly=True): |
76 self.pool.init() | 77 self.pool.init() |
77 | 78 |
78 if not hasattr(app, 'extensions'): | 79 if not hasattr(app, 'extensions'): |
79 app.extensions = {} | 80 app.extensions = {} |
80 app.extensions['Tryton'] = self | 81 app.extensions['Tryton'] = self |
81 app.url_map.converters['record'] = RecordConverter | 82 app.url_map.converters['record'] = RecordConverter |
82 app.url_map.converters['records'] = RecordsConverter | 83 app.url_map.converters['records'] = RecordsConverter |
84 app.jinja_env.filters['tryton_date'] = self.format_date | |
85 app.jinja_env.filters['tryton_number'] = self.format_number | |
86 app.jinja_env.filters['tryton_currency'] = self.format_currency | |
87 if hasattr(Report, 'format_timedelta'): | |
88 app.jinja_env.filters['tryton_timedelta'] = Report.format_timedelta | |
ced
2020/04/19 10:03:15
For me it could be managed like others to set a de
| |
83 | 89 |
84 def default_context(self, callback): | 90 def default_context(self, callback): |
85 "Set the callback for the default transaction context" | 91 "Set the callback for the default transaction context" |
86 self.context_callback = callback | 92 self.context_callback = callback |
87 return callback | 93 return callback |
88 | 94 |
95 @property | |
96 def language(self): | |
97 "Return a language instance for the current request" | |
98 Lang = self.pool.get('ir.lang') | |
99 language = Transaction().context.get('language') | |
ced
2020/04/19 10:03:15
I think we should add a comment on why not using T
| |
100 if not language: | |
101 language = request.accept_languages.best_match( | |
102 Lang.get_translatable_languages()) | |
103 return Lang.get(language) | |
104 | |
105 def format_date(self, value, lang=None, *args, **kwargs): | |
106 if lang is None: | |
107 lang = self.language | |
108 return Report.format_date(value, lang, *args, **kwargs) | |
109 | |
110 def format_number(self, value, lang=None, *args, **kwargs): | |
111 if lang is None: | |
112 lang = self.language | |
113 return Report.format_number(value, lang, *args, **kwargs) | |
114 | |
115 def format_currency(self, value, currency, lang=None, *args, **kwargs): | |
116 if lang is None: | |
117 lang = self.language | |
118 return Report.format_currency(value, lang, currency, *args, **kwargs) | |
119 | |
89 def _readonly(self): | 120 def _readonly(self): |
90 return not (request | 121 return not (request |
91 and request.method in ('PUT', 'POST', 'DELETE', 'PATCH')) | 122 and request.method in ('PUT', 'POST', 'DELETE', 'PATCH')) |
92 | 123 |
93 def transaction(self, readonly=None, user=None, context=None): | 124 def transaction(self, readonly=None, user=None, context=None): |
94 """Decorator to run inside a Tryton transaction. | 125 """Decorator to run inside a Tryton transaction. |
95 The decorated method could be run multiple times in case of | 126 The decorated method could be run multiple times in case of |
96 database operational error. | 127 database operational error. |
97 | 128 |
98 If readonly is None then the transaction will be readonly except for | 129 If readonly is None then the transaction will be readonly except for |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 | 268 |
238 def __init__(self, map, model): | 269 def __init__(self, map, model): |
239 super(RecordsConverter, self).__init__(map) | 270 super(RecordsConverter, self).__init__(map) |
240 self.model = model | 271 self.model = model |
241 | 272 |
242 def to_python(self, value): | 273 def to_python(self, value): |
243 return _RecordsProxy(self.model, map(int, value.split(','))) | 274 return _RecordsProxy(self.model, map(int, value.split(','))) |
244 | 275 |
245 def to_url(self, value): | 276 def to_url(self, value): |
246 return ','.join(map(int, value)) | 277 return ','.join(map(int, value)) |
OLD | NEW |