File: active.py

package info (click to toggle)
tryton-server 7.0.40-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,748 kB
  • sloc: python: 53,502; xml: 5,194; sh: 803; sql: 217; makefile: 28
file content (43 lines) | stat: -rw-r--r-- 1,365 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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.pyson import Eval

from . import fields
from .model import Model
from .modelview import ModelView


class DeactivableMixin(Model):
    "Mixin to allow to soft deletion of records"
    __slots__ = ()

    active = fields.Boolean(
        lazy_gettext('ir.msg_active'),
        help=lazy_gettext('ir.msg_active_help'))

    @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 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')