| Total Complexity | 4 |
| Total Lines | 33 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 |