Passed
Push — 2.x ( 19619a...408f73 )
by Ramon
09:28
created

TextLineField.get()   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
# -*- coding: utf-8 -*-
2
3
import six
4
5
from Products.CMFPlone.utils import safe_unicode
6
from zope import schema
7
8
9
class TextLineField(schema.TextLine):
10
    """A text field with no newlines and without leading and trailing spaces.
11
    """
12
13
    def set(self, object, value):
14
        """Set the field's value to the given object.
15
        The value is converted to a Unicode string to preserve compatibility
16
        with the legacy behavior of auto-generated setters in AT content types.
17
        Leading and trailing whitespaces are removed before assignment.
18
        """
19
        value = safe_unicode(value)
20
        if isinstance(value, six.string_types):
21
            value = value.strip()
22
        super(TextLineField, self).set(object, value)
23
24
    def get(self, object):
25
        """Sets the value of this field from the given object.
26
        The returned value is encoded as UTF-8 to maintain compatibility with
27
        the legacy behavior of auto-generated getters in AT content types.
28
        """
29
        value = super(TextLineField, self).get(object)
30
        if isinstance(value, six.string_types):
31
            value = value.encode("utf-8")
32
        return value
33