Revert "Revert "[FIX] fields: `digits()` computation""
Thamks to parent commit, `request.env` doesn't raise `AttributeError` anymore for requests without session bound to a database. This exception was bubbling up to `digits` property (and `__getattr__`) This reverts commit49cb46fb78
. This reinstate commiteeedd2d9f5
.
This commit is contained in:
parent
291119c802
commit
1cf5723835
|
@ -1054,7 +1054,8 @@ class Float(Field):
|
||||||
@property
|
@property
|
||||||
def digits(self):
|
def digits(self):
|
||||||
if callable(self._digits):
|
if callable(self._digits):
|
||||||
return self._digits(fields.get_cursor())
|
with fields._get_cursor() as cr:
|
||||||
|
return self._digits(cr)
|
||||||
else:
|
else:
|
||||||
return self._digits
|
return self._digits
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,7 @@ import pytz
|
||||||
import re
|
import re
|
||||||
import xmlrpclib
|
import xmlrpclib
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
|
from contextlib import contextmanager
|
||||||
from psycopg2 import Binary
|
from psycopg2 import Binary
|
||||||
|
|
||||||
import openerp
|
import openerp
|
||||||
|
@ -52,13 +53,22 @@ from openerp.tools import float_repr, float_round, frozendict, html_sanitize
|
||||||
import simplejson
|
import simplejson
|
||||||
from openerp import SUPERUSER_ID, registry
|
from openerp import SUPERUSER_ID, registry
|
||||||
|
|
||||||
def get_cursor():
|
@contextmanager
|
||||||
# retrieve a valid cursor from any environment
|
def _get_cursor():
|
||||||
|
# yield a valid cursor from any environment or create a new one if none found
|
||||||
from openerp.api import Environment
|
from openerp.api import Environment
|
||||||
|
from openerp.http import request
|
||||||
|
try:
|
||||||
|
request.env # force request's env to be computed
|
||||||
|
except RuntimeError:
|
||||||
|
pass # ignore if not in a request
|
||||||
for env in Environment.envs:
|
for env in Environment.envs:
|
||||||
if not env.cr.closed:
|
if not env.cr.closed:
|
||||||
return env.cr
|
yield env.cr
|
||||||
raise RuntimeError("No valid cursor found")
|
break
|
||||||
|
else:
|
||||||
|
with registry().cursor() as cr:
|
||||||
|
yield cr
|
||||||
|
|
||||||
EMPTY_DICT = frozendict()
|
EMPTY_DICT = frozendict()
|
||||||
|
|
||||||
|
@ -396,7 +406,8 @@ class float(_column):
|
||||||
@property
|
@property
|
||||||
def digits(self):
|
def digits(self):
|
||||||
if self._digits_compute:
|
if self._digits_compute:
|
||||||
return self._digits_compute(get_cursor())
|
with _get_cursor() as cr:
|
||||||
|
return self._digits_compute(cr)
|
||||||
else:
|
else:
|
||||||
return self._digits
|
return self._digits
|
||||||
|
|
||||||
|
@ -1316,7 +1327,8 @@ class function(_column):
|
||||||
@property
|
@property
|
||||||
def digits(self):
|
def digits(self):
|
||||||
if self._digits_compute:
|
if self._digits_compute:
|
||||||
return self._digits_compute(get_cursor())
|
with _get_cursor() as cr:
|
||||||
|
return self._digits_compute(cr)
|
||||||
else:
|
else:
|
||||||
return self._digits
|
return self._digits
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue