source: main/waeup.kofa/branches/0.2/src/waeup/kofa/widgets/sequencewidget.py @ 14292

Last change on this file since 14292 was 8057, checked in by Henrik Bettermann, 13 years ago

Adjust copyright header and propset svn:keywords.

  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1## $Id: sequencewidget.py 8057 2012-04-06 21:56:22Z henrik $
2##
3## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""An improved sequence widget.
19
20We provide a sequence widget that better suits our needs layout-wise
21than the default implementation from zope.formlib.
22"""
23import grok
24from zope.i18n import translate
25from zope.browserpage import ViewPageTemplateFile
26from zope.formlib.interfaces import IInputWidget, IDisplayWidget
27from zope.formlib.widgets import (
28    ListSequenceWidget, SequenceDisplayWidget)
29from zope.publisher.interfaces.browser import IBrowserRequest
30from zope.schema.interfaces import IField, IList
31
32class KofaSequenceWidget(ListSequenceWidget):
33    """A sequence widget for lists.
34
35    This is basically a plain copy from zope.formlib. We have,
36    however, the possibility to tweak the attached template.
37    """
38    template = ViewPageTemplateFile('sequencewidget.pt')
39
40class KofaSequenceDisplayWidget(SequenceDisplayWidget):
41    """A sequence widget for lists.
42
43    This is basically a plain copy from zope.formlib. We have,
44    however, the possibility to tweak html tags.
45    """
46
47    tag = None
48    itemTag = 'div'
49
50    def __call__(self):
51        """Patch for the orginal __call__ method.
52
53        The orginal __call__ method doesn't call
54        the translate function properly.
55        """
56
57        # get the data to display:
58        if self._renderedValueSet():
59            data = self._data
60        else:
61            data = self.context.get(self.context.context)
62
63        # deal with special cases:
64        if data == self.context.missing_value:
65            return translate(self._missingValueMessage, context=self.request)
66        data = list(data)
67        if not data:
68            return translate(self._emptySequenceMessage, context=self.request)
69
70        parts = []
71        for i, item in enumerate(data):
72            widget = self._getWidget(i)
73            widget.setRenderedValue(item)
74            s = widget()
75            if self.itemTag:
76                s = "<%s>%s</%s>" % (self.itemTag, s, self.itemTag)
77            parts.append(s)
78        contents = "\n".join(parts)
79        if self.tag:
80            contents = "\n%s\n" % contents
81            contents = renderElement(self.tag,
82                                     cssClass=self.cssClass,
83                                     extra=self.extra,
84                                     contents=contents)
85        return contents
86
87# Register our sequence widgets as default for lists.
88@grok.adapter(IList, IField, IBrowserRequest)
89@grok.implementer(IInputWidget)
90def seq_input_widget(obj, field, req, *args, **kw):
91    return KofaSequenceWidget(obj, field, req, *args, **kw)
92
93@grok.adapter(IList, IField, IBrowserRequest)
94@grok.implementer(IDisplayWidget)
95def seq_display_widget(obj, field, req, *args, **kw):
96    return KofaSequenceDisplayWidget(obj, field, req, *args, **kw)
Note: See TracBrowser for help on using the repository browser.