[7196] | 1 | ## $Id: objectwidget.py 12316 2014-12-25 08:27:45Z henrik $ |
---|
[5275] | 2 | ## |
---|
[7196] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[5275] | 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. |
---|
[7196] | 8 | ## |
---|
[5275] | 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. |
---|
[7196] | 13 | ## |
---|
[5275] | 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 | ## |
---|
[5299] | 18 | """A widget to display IObject instances in forms. |
---|
[5275] | 19 | """ |
---|
| 20 | from zope.browserpage import ViewPageTemplateFile |
---|
| 21 | from zope.formlib.interfaces import IDisplayWidget |
---|
| 22 | from zope.formlib.objectwidget import ObjectWidget, ObjectWidgetView |
---|
| 23 | from zope.formlib.utility import setUpWidgets |
---|
[5310] | 24 | from zope.interface import implements, implementsOnly |
---|
[5275] | 25 | from zope.schema import getFieldNamesInOrder |
---|
| 26 | |
---|
[11949] | 27 | class IkobaObjectWidgetView(ObjectWidgetView): |
---|
[5299] | 28 | template = ViewPageTemplateFile('objectwidget.pt') |
---|
[5275] | 29 | |
---|
[12316] | 30 | class IProductOptionWidgetDisplayView(ObjectWidgetView): |
---|
| 31 | template = ViewPageTemplateFile('productoptiondisplaywidget.pt') |
---|
[12314] | 32 | |
---|
[11949] | 33 | class IkobaObjectWidget(ObjectWidget): |
---|
[5275] | 34 | |
---|
| 35 | def __init__(self, context, request, factory, **kw): |
---|
| 36 | super(ObjectWidget, self).__init__(context, request) |
---|
| 37 | |
---|
| 38 | # define view that renders the widget |
---|
| 39 | self.view = self._getView(request) |
---|
| 40 | |
---|
| 41 | # factory used to create content that this widget (field) |
---|
| 42 | # represents |
---|
| 43 | self.factory = factory |
---|
| 44 | |
---|
| 45 | # handle foo_widget specs being passed in |
---|
| 46 | self.names = getFieldNamesInOrder(self.context.schema) |
---|
| 47 | for k, v in kw.items(): |
---|
| 48 | if k.endswith('_widget'): |
---|
| 49 | setattr(self, k, v) |
---|
| 50 | |
---|
| 51 | # set up my subwidgets |
---|
| 52 | self._setUpWidgets() |
---|
[12314] | 53 | |
---|
[5275] | 54 | def subwidgets(self): |
---|
| 55 | result = [self.getSubWidget(name) for name in self.names] |
---|
| 56 | return result |
---|
| 57 | |
---|
| 58 | def _setUpWidgets(self): |
---|
[12314] | 59 | self._setUpEditWidgets() |
---|
| 60 | # enlarge title widgets |
---|
| 61 | for subwidget in self.subwidgets(): |
---|
| 62 | if 'title' in subwidget.name: |
---|
| 63 | subwidget.displayWidth = '50%' |
---|
| 64 | return |
---|
[5275] | 65 | |
---|
| 66 | def _getView(self, request): |
---|
[11949] | 67 | return IkobaObjectWidgetView(self, request) |
---|
[5275] | 68 | |
---|
[7795] | 69 | |
---|
[11949] | 70 | class IkobaObjectDisplayWidget(IkobaObjectWidget): |
---|
[5275] | 71 | |
---|
[5310] | 72 | implementsOnly(IDisplayWidget) |
---|
| 73 | |
---|
[5275] | 74 | def _setUpDisplayWidgets(self): |
---|
| 75 | # subwidgets need a new name |
---|
| 76 | setUpWidgets(self, self.context.schema, IDisplayWidget, |
---|
| 77 | prefix=self.name, names=self.names, |
---|
| 78 | context=self.context) |
---|
| 79 | |
---|
| 80 | def _setUpWidgets(self): |
---|
| 81 | return self._setUpDisplayWidgets() |
---|
[12314] | 82 | |
---|
| 83 | def _getView(self, request): |
---|
[12316] | 84 | if self.context.schema.__name__ == 'IProductOptionEntry': |
---|
| 85 | return IProductOptionWidgetDisplayView(self, request) |
---|
| 86 | return IkobaObjectWidgetView(self, request) |
---|