Passed
Push — 2.x ( e94308...699219 )
by Jordi
09:28
created

senaite.core.browser.widgets.queryselect   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 142
Duplicated Lines 41.55 %

Importance

Changes 0
Metric Value
wmc 10
eloc 78
dl 59
loc 142
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A QuerySelectWidget.get_api_url() 0 7 1
A QuerySelectWidget.get_input_widget_attributes() 31 31 2
A QuerySelectWidget.to_value() 0 12 4
A QuerySelectWidget.lookup() 28 28 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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-2023 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import json
22
23
import six
24
25
from AccessControl import ClassSecurityInfo
26
from bika.lims import api
27
from Products.Archetypes.Registry import registerWidget
28
from Products.Archetypes.Widget import TypesWidget
29
from Products.CMFPlone.utils import base_hasattr
30
31
_marker = object
32
33
34
class QuerySelectWidget(TypesWidget):
35
    """AT Backport of Dexterity Queryselect Widget
36
37
    https://github.com/senaite/senaite.core/pull/2177
38
    """
39
    security = ClassSecurityInfo()
40
    _properties = TypesWidget._properties.copy()
41
    _properties.update({
42
        "macro": "senaite_widgets/queryselectwidget",
43
        "query": {},
44
        "limit": 25,
45
        "catalog": "portal_catalog",
46
        "api_url": None,
47
        "disabled": False,
48
        "readonly": False,
49
        "multi_valued": False,
50
        "allow_user_value": False,
51
        "search_wildcard": True,
52
        "value_key": "uid",
53
        "padding": 3,
54
        "display_template": None,
55
        "hide_input_after_select": False,
56
    })
57
58 View Code Duplication
    def lookup(self, name, field, context, default=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
59
        """Check if the context has an override for the given named property
60
61
        The context can either define an attribute or a method with the
62
        following naming convention:
63
64
            <fieldname>_<propertyname>
65
66
        If an attribute or method is found, this value will be returned,
67
        otherwise the lookup will return the default value
68
        """
69
70
        # check if the current context defines an attribute or method for the
71
        # given property
72
        key = "{}_{}".format(field.getName(), name)
73
        if base_hasattr(context, key):
74
            attr = getattr(context, key, default)
75
            if callable(attr):
76
                # call the context method with additional information
77
                attr = attr(name=name,
78
                            widget=self,
79
                            field=field,
80
                            context=context,
81
                            default=default)
82
            return attr
83
84
        # return the widget attribute
85
        return getattr(self, name, default)
86
87
    def to_value(self, value):
88
        """Extract the value from the request or get it from the field
89
        """
90
        # the value might come from the request, e.g. on object creation
91
        if isinstance(value, six.string_types):
92
            value = filter(None, value.split("\r\n"))
93
        # we handle always lists in the templates
94
        if value is None:
95
            return []
96
        if not isinstance(value, (list, tuple)):
97
            value = [value]
98
        return value
99
100 View Code Duplication
    def get_input_widget_attributes(self, context, field, value):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
101
        """Return input widget attributes for the ReactJS component
102
        """
103
        attributes = {
104
            "data-id": field.getName(),
105
            "data-name": field.getName(),
106
            "data-values": self.to_value(value),
107
            "data-value_key": getattr(self, "value_key", "uid"),
108
            "data-api_url": self.get_api_url(),
109
            "data-query": getattr(self, "query", {}),
110
            "data-catalog": getattr(self, "catalog", "portal_catalog"),
111
            "data-search_index": getattr(self, "search_index", "Title"),
112
            "data-search_wildcard": getattr(self, "search_wildcard", True),
113
            "data-allow_user_value": getattr(self, "allow_user_value", False),
114
            "data-columns": getattr(self, "columns", []),
115
            "data-display_template": getattr(self, "display_template", None),
116
            "data-limit": getattr(self, "limit", 5),
117
            "data-multi_valued": getattr(self, "multi_valued", True),
118
            "data-disabled": getattr(self, "disabled", False),
119
            "data-readonly": getattr(self, "readonly", False),
120
            "data-hide_input_after_select": getattr(
121
                self, "hide_user_input_after_select", False),
122
        }
123
124
        for key, value in attributes.items():
125
            # lookup attributes for overrides
126
            value = self.lookup(key, field, context, default=value)
127
            # convert all attributes to JSON
128
            attributes[key] = json.dumps(value)
129
130
        return attributes
131
132
    def get_api_url(self):
133
        """JSON API URL to use for this widget
134
        """
135
        portal = api.get_portal()
136
        portal_url = api.get_url(portal)
137
        api_url = "{}/@@API/senaite/v1".format(portal_url)
138
        return api_url
139
140
141
registerWidget(QuerySelectWidget, title="QuerySelectWidget")
142