Code Duplication    Length = 13-23 lines in 2 locations

src/bika/lims/adapters/referencewidgetvocabulary.py 1 location

@@ 103-125 (lines=23) @@
100
        """
101
        return self.get_query_from_request("search_query")
102
103
    def to_utf8(self, data):
104
        """
105
        Convert unicode values to strings even if they belong to lists or dicts.
106
        :param data: an object.
107
        :return: The object with all unicode values converted to string.
108
        """
109
        # if this is a unicode string, return its string representation
110
        if isinstance(data, unicode):
111
            return data.encode('utf-8')
112
113
        # if this is a list of values, return list of string values
114
        if isinstance(data, list):
115
            return [self.to_utf8(item) for item in data]
116
117
        # if this is a dictionary, return dictionary of string keys and values
118
        if isinstance(data, dict):
119
            return {
120
                self.to_utf8(key): self.to_utf8(value)
121
                for key, value in six.iteritems(data)
122
            }
123
124
        # if it's anything else, return it in its original form
125
        return data
126
127
    def get_query_from_request(self, name):
128
        """Returns the query inferred from the request

src/senaite/core/z3cform/widgets/address.py 1 location

@@ 83-95 (lines=13) @@
80
            }
81
        return safe_unicode(data)
82
83
    def to_utf8(self, data):
84
        """Encodes the data to utf-8
85
        """
86
        if isinstance(data, unicode):
87
            return data.encode("utf-8")
88
        if isinstance(data, list):
89
            return [self.to_utf8(item) for item in data]
90
        if isinstance(data, dict):
91
            return {
92
                self.to_utf8(key): self.to_utf8(value)
93
                for key, value in six.iteritems(data)
94
            }
95
        return data
96
97
98
@implementer(IAddressWidget)