changeset d657d097b796 in modules/currency:default
details: https://hg.tryton.org/modules/currency?cmd=changeset&node=d657d097b796
description:
        Add monetary field

        issue10598
        review338511002
diffstat:

 CHANGELOG         |   3 +++
 currency.py       |   6 ++++--
 doc/index.rst     |   1 +
 doc/reference.rst |  17 +++++++++++++++++
 fields.py         |  36 ++++++++++++++++++++++++++++++++++++
 5 files changed, 61 insertions(+), 2 deletions(-)

diffs (102 lines):

diff -r ec5685b8b0dc -r d657d097b796 CHANGELOG
--- a/CHANGELOG Sun Jul 04 17:54:47 2021 +0200
+++ b/CHANGELOG Sat Aug 21 09:09:55 2021 +0200
@@ -1,3 +1,6 @@
+* Add monetary field
+* Provide digits from currency
+
 Version 6.0.0 - 2021-05-03
 * Bug fixes (see mercurial logs for details)
 * Add parameter rate_decimal
diff -r ec5685b8b0dc -r d657d097b796 currency.py
--- a/currency.py       Sun Jul 04 17:54:47 2021 +0200
+++ b/currency.py       Sat Aug 21 09:09:55 2021 +0200
@@ -18,7 +18,8 @@
 
 from trytond.i18n import gettext
 from trytond.model import (
-    ModelView, ModelSQL, DeactivableMixin, fields, Unique, Check, SymbolMixin)
+    ModelView, ModelSQL, DeactivableMixin, fields, Unique, Check, SymbolMixin,
+    DigitsMixin)
 from trytond.transaction import Transaction
 from trytond.pool import Pool
 from trytond.rpc import RPC
@@ -30,7 +31,8 @@
 logger = logging.getLogger(__name__)
 
 
-class Currency(SymbolMixin, DeactivableMixin, ModelSQL, ModelView):
+class Currency(
+        SymbolMixin, DigitsMixin, DeactivableMixin, ModelSQL, ModelView):
     'Currency'
     __name__ = 'currency.currency'
     name = fields.Char('Name', required=True, translate=True,
diff -r ec5685b8b0dc -r d657d097b796 doc/index.rst
--- a/doc/index.rst     Sun Jul 04 17:54:47 2021 +0200
+++ b/doc/index.rst     Sat Aug 21 09:09:55 2021 +0200
@@ -14,3 +14,4 @@
    usage
    configuration
    design
+   reference
diff -r ec5685b8b0dc -r d657d097b796 doc/reference.rst
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/reference.rst Sat Aug 21 09:09:55 2021 +0200
@@ -0,0 +1,17 @@
+*************
+API Reference
+*************
+
+.. module:: trytond.modules.currency.fields
+
+Monetary field
+==============
+
+.. class:: Monetary(string[, currency[, \**options]])
+
+A subclass of :class:`~trytond:trytond.model.fields.Numeric` used to store 
monetary values.
+
+.. attribute:: Monetary.currency
+
+   The name of the :class:`~trytond:trytond.model.fields.Many2One` field which
+   stores the currency used to display the symbol.
diff -r ec5685b8b0dc -r d657d097b796 fields.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/fields.py Sat Aug 21 09:09:55 2021 +0200
@@ -0,0 +1,36 @@
+# This file is part of Tryton.  The COPYRIGHT file at the toplevel of this
+# repository contains the full copyright notices and license terms.
+from trytond.model import fields
+
+__all__ = ['Monetary']
+
+
+class Monetary(fields.Numeric):
+    """
+    Define a numeric field with currency (``decimal``).
+    """
+    def __init__(self, string='', currency=None, digits=None, help='',
+            required=False, readonly=False, domain=None, states=None,
+            select=False, on_change=None, on_change_with=None, depends=None,
+            context=None, loading='eager'):
+        '''
+        :param currency: the name of the Many2One field which stores
+            the currency
+        '''
+        if currency:
+            if depends is None:
+                depends = [currency]
+            elif currency not in depends:
+                depends = depends.copy()
+                depends.append(currency)
+        super().__init__(string=string, digits=digits, help=help,
+            required=required, readonly=readonly, domain=domain, states=states,
+            select=select, on_change=on_change, on_change_with=on_change_with,
+            depends=depends, context=context, loading=loading)
+        self.currency = currency
+
+    def definition(self, model, language):
+        definition = super().definition(model, language)
+        definition['symbol'] = self.currency
+        definition['monetary'] = True
+        return definition

Reply via email to