Code Duplication    Length = 29-36 lines in 2 locations

src/senaite/core/browser/widgets/selectotherwidget.py 1 location

@@ 32-67 (lines=36) @@
29
30
        return value, {}
31
32
    def get_input_widget_attributes(self, context, field, value):
33
        """Return input widget attributes for the ReactJS component
34
35
        This method get called from the page template to populate the
36
        attributes that are used by the ReactJS widget component.
37
38
        :param context: The current context of the field
39
        :param field: The current field of the widget
40
        :param value: The current field value
41
        """
42
        option = ""
43
        other = ""
44
45
        # find out if the value is a predefined option
46
        choices = self.get_choices(context, field)
47
        options = dict(choices).keys()
48
        if value in options:
49
            option = value
50
        elif value:
51
            option = OTHER_OPTION_VALUE
52
            other = value
53
54
        attributes = {
55
            "data-id": field.getName(),
56
            "data-name": field.getName(),
57
            "data-choices": choices,
58
            "data-option": option,
59
            "data-option_other": OTHER_OPTION_VALUE,
60
            "data-other": other,
61
        }
62
63
        # convert all attributes to JSON
64
        for key, value in attributes.items():
65
            attributes[key] = json.dumps(value)
66
67
        return attributes
68
69
    def get_vocabulary(self, context, field):
70
        func = getattr(field, "Vocabulary", None)

src/senaite/core/z3cform/widgets/selectother/widget.py 1 location

@@ 75-103 (lines=29) @@
72
        choices = dict(choices)
73
        return choices.get(self.value) or self.value
74
75
    def get_input_widget_attributes(self):
76
        """Return input widget attributes for the ReactJS component
77
        """
78
        option = ""
79
        other = ""
80
81
        # find out if the value is a predefined option
82
        choices = self.get_choices()
83
        options = dict(choices).keys()
84
        if self.value in options:
85
            option = self.value
86
        elif self.value:
87
            option = OTHER_OPTION_VALUE
88
            other = self.value
89
90
        attributes = {
91
            "data-id": self.id,
92
            "data-name": self.name,
93
            "data-choices": choices,
94
            "data-option": option,
95
            "data-option_other": OTHER_OPTION_VALUE,
96
            "data-other": other,
97
        }
98
99
        # convert all attributes to JSON
100
        for key, value in attributes.items():
101
            attributes[key] = json.dumps(value)
102
103
        return attributes
104
105
    def update(self):
106
        """Computes self.value for the widget templates