[7766] | 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 | |
---|
| 20 | We provide a sequence widget that better suits our needs layout-wise |
---|
| 21 | than the default implementation from zope.formlib. |
---|
| 22 | """ |
---|
| 23 | import grok |
---|
[8024] | 24 | from zope.i18n import translate |
---|
[7766] | 25 | from zope.browserpage import ViewPageTemplateFile |
---|
[7797] | 26 | from zope.formlib.interfaces import IInputWidget, IDisplayWidget |
---|
| 27 | from zope.formlib.widgets import ( |
---|
| 28 | ListSequenceWidget, SequenceDisplayWidget) |
---|
[7766] | 29 | from zope.publisher.interfaces.browser import IBrowserRequest |
---|
| 30 | from zope.schema.interfaces import IField, IList |
---|
| 31 | |
---|
[11495] | 32 | |
---|
[7819] | 33 | class KofaSequenceWidget(ListSequenceWidget): |
---|
[7766] | 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') |
---|
[11495] | 40 | _kofa_seq_len_changed = False |
---|
[7766] | 41 | |
---|
[11495] | 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 | |
---|
[7797] | 50 | class KofaSequenceDisplayWidget(SequenceDisplayWidget): |
---|
| 51 | """A sequence widget for lists. |
---|
[7766] | 52 | |
---|
[7797] | 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 |
---|
[7802] | 58 | itemTag = 'div' |
---|
[7797] | 59 | |
---|
[8024] | 60 | def __call__(self): |
---|
[8025] | 61 | """Patch for the orginal __call__ method. |
---|
[8024] | 62 | |
---|
[8025] | 63 | The orginal __call__ method doesn't call |
---|
| 64 | the translate function properly. |
---|
| 65 | """ |
---|
| 66 | |
---|
[8024] | 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 | |
---|
[11495] | 97 | |
---|
[7797] | 98 | # Register our sequence widgets as default for lists. |
---|
[7766] | 99 | @grok.adapter(IList, IField, IBrowserRequest) |
---|
| 100 | @grok.implementer(IInputWidget) |
---|
| 101 | def seq_input_widget(obj, field, req, *args, **kw): |
---|
[7819] | 102 | return KofaSequenceWidget(obj, field, req, *args, **kw) |
---|
[7797] | 103 | |
---|
[11495] | 104 | |
---|
[7797] | 105 | @grok.adapter(IList, IField, IBrowserRequest) |
---|
| 106 | @grok.implementer(IDisplayWidget) |
---|
| 107 | def seq_display_widget(obj, field, req, *args, **kw): |
---|
| 108 | return KofaSequenceDisplayWidget(obj, field, req, *args, **kw) |
---|