source: main/waeup.kofa/branches/henrik-transcript-workflow/src/waeup/kofa/widgets/sequencewidget.py @ 15189

Last change on this file since 15189 was 11495, checked in by uli, 11 years ago

Make sequence length change a view attribute.

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1## $Id: sequencewidget.py 11495 2014-03-13 15:39:27Z uli $
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
32
33class KofaSequenceWidget(ListSequenceWidget):
34    """A sequence widget for lists.
35
36    This is basically a plain copy from zope.formlib. We have,
37    however, the possibility to tweak the attached template.
38    """
39    template = ViewPageTemplateFile('sequencewidget.pt')
40    _kofa_seq_len_changed = False
41
42    def _generateSequence(self):
43        result = super(KofaSequenceWidget, self)._generateSequence()
44        if ((self.name + '.add' in self.request.form) or
45            (self.name + '.remove' in self.request.form)):
46            self._kofa_seq_len_changed = True
47        return result
48
49
50class KofaSequenceDisplayWidget(SequenceDisplayWidget):
51    """A sequence widget for lists.
52
53    This is basically a plain copy from zope.formlib. We have,
54    however, the possibility to tweak html tags.
55    """
56
57    tag = None
58    itemTag = 'div'
59
60    def __call__(self):
61        """Patch for the orginal __call__ method.
62
63        The orginal __call__ method doesn't call
64        the translate function properly.
65        """
66
67        # get the data to display:
68        if self._renderedValueSet():
69            data = self._data
70        else:
71            data = self.context.get(self.context.context)
72
73        # deal with special cases:
74        if data == self.context.missing_value:
75            return translate(self._missingValueMessage, context=self.request)
76        data = list(data)
77        if not data:
78            return translate(self._emptySequenceMessage, context=self.request)
79
80        parts = []
81        for i, item in enumerate(data):
82            widget = self._getWidget(i)
83            widget.setRenderedValue(item)
84            s = widget()
85            if self.itemTag:
86                s = "<%s>%s</%s>" % (self.itemTag, s, self.itemTag)
87            parts.append(s)
88        contents = "\n".join(parts)
89        if self.tag:
90            contents = "\n%s\n" % contents
91            contents = renderElement(self.tag,
92                                     cssClass=self.cssClass,
93                                     extra=self.extra,
94                                     contents=contents)
95        return contents
96
97
98# Register our sequence widgets as default for lists.
99@grok.adapter(IList, IField, IBrowserRequest)
100@grok.implementer(IInputWidget)
101def seq_input_widget(obj, field, req, *args, **kw):
102    return KofaSequenceWidget(obj, field, req, *args, **kw)
103
104
105@grok.adapter(IList, IField, IBrowserRequest)
106@grok.implementer(IDisplayWidget)
107def seq_display_widget(obj, field, req, *args, **kw):
108    return KofaSequenceDisplayWidget(obj, field, req, *args, **kw)
Note: See TracBrowser for help on using the repository browser.