Passed
Push — 2.x ( a7f0a0...02ca02 )
by Jordi
07:10
created

senaite.core.browser.form.adapters.senaitesetup   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 33
dl 0
loc 93
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B EditForm.modified() 0 38 5
A EditForm.initialized() 0 24 3
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE.
4
#
5
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2025 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from senaite.core.browser.form.adapters import EditFormAdapterBase
22
23
_FIELD_PREFIX = "form.widgets."
24
25
26
class EditForm(EditFormAdapterBase):
27
    """Edit form adapter for SenaiteSetup
28
    """
29
30
    def initialized(self, data):
31
        """Handle form initialization
32
        Show/hide rejection_reasons based on enable_rejection_workflow
33
        Make restrict_worksheet_management readonly if
34
        restrict_worksheet_users_access is enabled
35
        """
36
        # Check if rejection workflow is enabled
37
        enabled = self.context.getEnableRejectionWorkflow()
38
39
        if not enabled:
40
            # Hide rejection_reasons field if workflow is not enabled
41
            self.add_hide_field(_FIELD_PREFIX + "rejection_reasons")
42
43
        # Check if worksheet access is restricted to assigned analysts
44
        restrict_users = self.context.getRestrictWorksheetUsersAccess()
45
46
        if restrict_users:
47
            # Make restrict_worksheet_management readonly and force it to True
48
            self.add_readonly_field(
49
                _FIELD_PREFIX + "restrict_worksheet_management",
50
                message=None
51
            )
52
53
        return self.data
54
55
    def modified(self, data):
56
        """Handle field modifications
57
        Show/hide rejection_reasons when enable_rejection_workflow changes
58
        Enable/readonly restrict_worksheet_management when
59
        restrict_worksheet_users_access changes
60
        """
61
        name = data.get("name")
62
        value = data.get("value")
63
64
        if name == _FIELD_PREFIX + "enable_rejection_workflow":
65
            # Show or hide rejection_reasons based on checkbox value
66
            if value:
67
                self.add_show_field(_FIELD_PREFIX + "rejection_reasons")
68
            else:
69
                self.add_hide_field(_FIELD_PREFIX + "rejection_reasons")
70
71
        elif name == _FIELD_PREFIX + "restrict_worksheet_users_access":
72
            # Handle restrict_worksheet_management based on
73
            # restrict_worksheet_users_access
74
            if value:
75
                # Enable restrict_worksheet_management checkbox
76
                self.add_update_field(
77
                    _FIELD_PREFIX + "restrict_worksheet_management",
78
                    True
79
                )
80
                # Make restrict_worksheet_management readonly
81
                self.add_readonly_field(
82
                    _FIELD_PREFIX + "restrict_worksheet_management",
83
                    message=None
84
                )
85
            else:
86
                # Make restrict_worksheet_management editable
87
                self.add_editable_field(
88
                    _FIELD_PREFIX + "restrict_worksheet_management",
89
                    message=None
90
                )
91
92
        return self.data
93