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

@@ 91-103 (lines=13) @@
88
            }
89
        return safe_unicode(data)
90
91
    def to_utf8(self, data):
92
        """Encodes the data to utf-8
93
        """
94
        if isinstance(data, unicode):
95
            return data.encode("utf-8")
96
        if isinstance(data, list):
97
            return [self.to_utf8(item) for item in data]
98
        if isinstance(data, dict):
99
            return {
100
                self.to_utf8(key): self.to_utf8(value)
101
                for key, value in six.iteritems(data)
102
            }
103
        return data
104
105
106
@implementer(IAddressWidget)