changeset 82df091bf5ae in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=82df091bf5ae
description:
        Set all fields readonly for inactive records

        issue3747
        review295061011
diffstat:

 CHANGELOG                 |   1 +
 doc/ref/models/models.rst |   3 ++-
 trytond/model/active.py   |  29 +++++++++++++++++++++++++++--
 3 files changed, 30 insertions(+), 3 deletions(-)

diffs (66 lines):

diff -r 2d70f0eb6684 -r 82df091bf5ae CHANGELOG
--- a/CHANGELOG Mon Mar 09 18:24:54 2020 +0100
+++ b/CHANGELOG Tue Mar 17 20:08:13 2020 +0100
@@ -1,3 +1,4 @@
+* Set all fields readonly for inactive record
 * Enable check_access context when checking wizard access (issue9108)
 * Add editable on calendar view
 * Add xalign and yalign to group
diff -r 2d70f0eb6684 -r 82df091bf5ae doc/ref/models/models.rst
--- a/doc/ref/models/models.rst Mon Mar 09 18:24:54 2020 +0100
+++ b/doc/ref/models/models.rst Tue Mar 17 20:08:13 2020 +0100
@@ -821,7 +821,8 @@
 
 .. class:: DeactivableMixin
 
-A mixin_ to add soft deletion to the model.
+A mixin_ to add soft deletion to the model. It renders all the fields as
+read-only when the record is inactive.
 
 Class attributes are:
 
diff -r 2d70f0eb6684 -r 82df091bf5ae trytond/model/active.py
--- a/trytond/model/active.py   Mon Mar 09 18:24:54 2020 +0100
+++ b/trytond/model/active.py   Tue Mar 17 20:08:13 2020 +0100
@@ -1,10 +1,11 @@
 # 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.i18n import lazy_gettext
-from trytond.model import fields
+from trytond.model import Model, ModelView, fields
+from trytond.pyson import Eval
 
 
-class DeactivableMixin(object):
+class DeactivableMixin(Model):
     "Mixin to allow to soft deletion of records"
     __slots__ = ()
 
@@ -15,3 +16,27 @@
     @classmethod
     def default_active(cls):
         return True
+
+    @classmethod
+    def __post_setup__(cls):
+        super().__post_setup__()
+
+        inactive = ~Eval('active', cls.default_active())
+        for name, field in cls._fields.items():
+            if name == 'active':
+                continue
+            if 'readonly' in field.states:
+                field.states['readonly'] |= inactive
+            else:
+                field.states['readonly'] = inactive
+            if 'active' not in field.depends:
+                field.depends.append('active')
+
+        if issubclass(cls, ModelView):
+            for states in cls._buttons.values():
+                if 'readonly' in states:
+                    states['readonly'] |= inactive
+                else:
+                    states['readonly'] = inactive
+                if 'active' not in states.setdefault('depends', []):
+                    states['depends'].append('active')

Reply via email to