changeset 3b129cae8778 in tryton:default
details: https://hg.tryton.org/tryton?cmd=changeset&node=3b129cae8778
description:
Add monetary field
issue10598
review338511002
diffstat:
CHANGELOG | 1 +
tryton/__init__.py | 17 +++++++++++++++++
tryton/common/number_entry.py | 16 ++++++++++++----
tryton/gui/window/view_form/model/field.py | 12 ++++++++----
tryton/gui/window/view_form/view/__init__.py | 2 +-
tryton/gui/window/view_form/view/form_gtk/integer.py | 3 ++-
6 files changed, 41 insertions(+), 10 deletions(-)
diffs (147 lines):
diff -r c792849c589e -r 3b129cae8778 CHANGELOG
--- a/CHANGELOG Sat Aug 21 09:08:09 2021 +0200
+++ b/CHANGELOG Sat Aug 21 09:09:56 2021 +0200
@@ -1,3 +1,4 @@
+* Support monetary and symbol field attributes
* Support digits from relation field
* Allow PYSON Expression as key for PYSON In with dict object
* Support Binary field in CSV import/export
diff -r c792849c589e -r 3b129cae8778 tryton/__init__.py
--- a/tryton/__init__.py Sat Aug 21 09:08:09 2021 +0200
+++ b/tryton/__init__.py Sat Aug 21 09:09:56 2021 +0200
@@ -43,3 +43,20 @@
formatted = locale._strip_padding(formatted, seps)
return formatted
setattr(locale, 'localize', localize)
+
+
+def delocalize(string, monetary=False):
+ conv = locale.localeconv()
+
+ # First, get rid of the grouping
+ ts = conv[monetary and 'mon_thousands_sep' or 'thousands_sep']
+ if ts:
+ string = string.replace(ts, '')
+ # next, replace the decimal point with a dot
+ dd = conv[monetary and 'mon_decimal_point' or 'decimal_point']
+ if dd:
+ string = string.replace(dd, '.')
+ return string
+
+
+setattr(locale, 'delocalize', delocalize)
diff -r c792849c589e -r 3b129cae8778 tryton/common/number_entry.py
--- a/tryton/common/number_entry.py Sat Aug 21 09:08:09 2021 +0200
+++ b/tryton/common/number_entry.py Sat Aug 21 09:09:56 2021 +0200
@@ -16,6 +16,8 @@
__digits = None
def __init__(self, *args, **kwargs):
+ self.monetary = kwargs.pop('monetary', False)
+ self.convert = kwargs.pop('convert', float)
super().__init__(*args, **kwargs)
self.set_alignment(1.0)
self.connect('key-press-event', self.__class__.__key_press_event)
@@ -34,14 +36,20 @@
text = self.get_text()
if text:
try:
- return locale.atof(text)
+ return self.convert(locale.delocalize(text, self.monetary))
except ValueError:
pass
return None
@property
def __decimal_point(self):
- return locale.localeconv()['decimal_point']
+ return locale.localeconv()[
+ self.monetary and 'mon_decimal_point' or 'decimal_point']
+
+ @property
+ def __thousands_sep(self):
+ return locale.localeconv()[
+ self.monetary and 'mon_thousands_sep' or 'thousands_sep']
# XXX: Override vfunc because position is inout
# https://gitlab.gnome.org/GNOME/pygobject/issues/12
@@ -50,9 +58,9 @@
text = self.get_buffer().get_text()
text = text[:position] + new_text + text[position:]
value = None
- if text not in ['-', self.__decimal_point]:
+ if text not in ['-', self.__decimal_point, self.__thousands_sep]:
try:
- value = locale.atof(text)
+ value = self.convert(locale.delocalize(text, self.monetary))
except ValueError:
return position
if (value and self.__digits is not None
diff -r c792849c589e -r 3b129cae8778 tryton/gui/window/view_form/model/field.py
--- a/tryton/gui/window/view_form/model/field.py Sat Aug 21 09:08:09
2021 +0200
+++ b/tryton/gui/window/view_form/model/field.py Sat Aug 21 09:09:56
2021 +0200
@@ -396,7 +396,8 @@
def convert(self, value):
try:
- return locale.atof(value)
+ return float(locale.delocalize(
+ value, self.attrs.get('monetary', False)))
except ValueError:
return self._default
@@ -423,7 +424,8 @@
p = int(digits[1])
else:
p = -int(d.as_tuple().exponent)
- return locale.localize('{0:.{1}f}'.format(d, p), True)
+ monetary = self.attrs.get('monetary', False)
+ return locale.localize('{0:.{1}f}'.format(d, p), True, monetary)
else:
return ''
@@ -432,7 +434,8 @@
def convert(self, value):
try:
- return Decimal(locale.delocalize(value))
+ return Decimal(locale.delocalize(
+ value, self.attrs.get('monetary', False)))
except decimal.InvalidOperation:
return self._default
@@ -449,7 +452,8 @@
def convert(self, value):
try:
- return locale.atoi(value)
+ return int(locale.delocalize(
+ value, self.attrs.get('monetary', False)))
except ValueError:
return self._default
diff -r c792849c589e -r 3b129cae8778
tryton/gui/window/view_form/view/__init__.py
--- a/tryton/gui/window/view_form/view/__init__.py Sat Aug 21 09:08:09
2021 +0200
+++ b/tryton/gui/window/view_form/view/__init__.py Sat Aug 21 09:09:56
2021 +0200
@@ -118,7 +118,7 @@
'sort', 'context', 'size', 'filename', 'autocomplete',
'translate', 'create', 'delete', 'selection_change_with',
'schema_model', 'required', 'help_selection',
- 'help_field', 'order']:
+ 'help_field', 'order', 'symbol', 'monetary']:
if name in field:
node_attrs.setdefault(name, field[name])
return node_attrs
diff -r c792849c589e -r 3b129cae8778
tryton/gui/window/view_form/view/form_gtk/integer.py
--- a/tryton/gui/window/view_form/view/form_gtk/integer.py Sat Aug 21
09:08:09 2021 +0200
+++ b/tryton/gui/window/view_form/view/form_gtk/integer.py Sat Aug 21
09:09:56 2021 +0200
@@ -13,7 +13,8 @@
def __init__(self, view, attrs):
super(Integer, self).__init__(view, attrs)
self.widget = Gtk.HBox()
- self.entry = self.mnemonic_widget = NumberEntry()
+ self.entry = self.mnemonic_widget = NumberEntry(
+ monetary=attrs.get('monetary', False))
self.entry.props.activates_default = True
self.entry.connect('activate', self.sig_activate)
self.entry.connect('focus-out-event', lambda *a: self._focus_out())