# HG changeset patch # User Thierry Florac # Date 1346331510 -7200 # Node ID 9615e7d1a4ee088e38633a2024f7b7a2509ea514 # Parent b09c98b285cc99d8e73311fccf1ccd8d08f3721f Added "TextLine list" schema field and widget diff -r b09c98b285cc -r 9615e7d1a4ee docs/HISTORY.txt --- a/docs/HISTORY.txt Mon Aug 20 18:54:13 2012 +0200 +++ b/docs/HISTORY.txt Thu Aug 30 14:58:30 2012 +0200 @@ -4,6 +4,7 @@ 0.3.13 ------ - added "ztfy.utils.container" utility module + - added "TextLine list" schema field and widget - added Python 2.7 compatibility code and timeout parameter to XML-RPC protocol helper diff -r b09c98b285cc -r 9615e7d1a4ee src/ztfy/utils/browser/configure.zcml --- a/src/ztfy/utils/browser/configure.zcml Mon Aug 20 18:54:13 2012 +0200 +++ b/src/ztfy/utils/browser/configure.zcml Thu Aug 30 14:58:30 2012 +0200 @@ -35,4 +35,29 @@ widget=".color.ColorWidget" layer="z3c.form.interfaces.IFormLayer" /> + + + + + + + + + + + + + \ No newline at end of file diff -r b09c98b285cc -r 9615e7d1a4ee src/ztfy/utils/browser/templates/textlinelist_display.pt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ztfy/utils/browser/templates/textlinelist_display.pt Thu Aug 30 14:58:30 2012 +0200 @@ -0,0 +1,34 @@ + + \ No newline at end of file diff -r b09c98b285cc -r 9615e7d1a4ee src/ztfy/utils/browser/templates/textlinelist_input.pt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ztfy/utils/browser/templates/textlinelist_input.pt Thu Aug 30 14:58:30 2012 +0200 @@ -0,0 +1,39 @@ + + + \ No newline at end of file diff -r b09c98b285cc -r 9615e7d1a4ee src/ztfy/utils/browser/textlinelist.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ztfy/utils/browser/textlinelist.py Thu Aug 30 14:58:30 2012 +0200 @@ -0,0 +1,69 @@ +### -*- coding: utf-8 -*- #################################################### +############################################################################## +# +# Copyright (c) 2012 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## + + +# import standard packages + +# import Zope3 interfaces +from z3c.form.interfaces import IWidget, IFieldWidget, ITextWidget, IFormLayer + +# import local interfaces +from ztfy.utils.schema import ITextLineListField + +# import Zope3 packages +from z3c.form.browser.text import TextWidget +from z3c.form.converter import SequenceDataConverter +from z3c.form.widget import FieldWidget +from zope.component import adapter, adapts +from zope.interface import implementer, implementsOnly + +# import local packages +from ztfy.jqueryui import jquery_multiselect + + +class ITextLineListWidget(ITextWidget): + """TextLineList widget interface""" + + +class TextLineListDataConverter(SequenceDataConverter): + """TextLineList field data converter""" + + adapts(ITextLineListField, IWidget) + + def toWidgetValue(self, value): + if value is self.field.missing_value: + return [] + return '|'.join(value) + + def toFieldValue(self, value): + if not value: + return self.field.missing_value + return value.split('|') + + +class TextLineListWidget(TextWidget): + """TextLineList field widget""" + + implementsOnly(ITextLineListWidget) + + def render(self): + jquery_multiselect.need() + return super(TextLineListWidget, self).render() + + +@adapter(ITextLineListField, IFormLayer) +@implementer(IFieldWidget) +def TextLineListFieldWidgetFactory(field, request): + return FieldWidget(field, TextLineListWidget(request)) diff -r b09c98b285cc -r 9615e7d1a4ee src/ztfy/utils/schema.py --- a/src/ztfy/utils/schema.py Mon Aug 20 18:54:13 2012 +0200 +++ b/src/ztfy/utils/schema.py Thu Aug 30 14:58:30 2012 +0200 @@ -20,7 +20,7 @@ # import Zope3 interfaces from z3c.form.interfaces import IWidget -from zope.schema.interfaces import ITextLine, IDecimal +from zope.schema.interfaces import ITextLine, IDecimal, IList # import local interfaces @@ -28,7 +28,7 @@ from z3c.form.converter import BaseDataConverter, FormatterValidationError from zope.component import adapts from zope.interface import implements -from zope.schema import TextLine, Decimal +from zope.schema import TextLine, Decimal, List from zope.schema._bootstrapfields import InvalidValue # import local packages @@ -108,3 +108,20 @@ return decimal.Decimal(value) except decimal.InvalidOperation: raise FormatterValidationError(self.errorMessage, value) + + +# +# TextLine list field +# + +class ITextLineListField(IList): + """Marker interface for textline list field""" + + +class TextLineListField(List): + """TextLine list field""" + + implements(ITextLineListField) + + def __init__(self, value_type=None, unique=False, **kw): + super(TextLineListField, self).__init__(value_type=TextLine(), unique=True, **kw)