1 | ## $Id: objectwidget.py 12316 2014-12-25 08:27:45Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 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 | """A widget to display IObject instances in forms. |
---|
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 |
---|
24 | from zope.interface import implements, implementsOnly |
---|
25 | from zope.schema import getFieldNamesInOrder |
---|
26 | |
---|
27 | class IkobaObjectWidgetView(ObjectWidgetView): |
---|
28 | template = ViewPageTemplateFile('objectwidget.pt') |
---|
29 | |
---|
30 | class IProductOptionWidgetDisplayView(ObjectWidgetView): |
---|
31 | template = ViewPageTemplateFile('productoptiondisplaywidget.pt') |
---|
32 | |
---|
33 | class IkobaObjectWidget(ObjectWidget): |
---|
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() |
---|
53 | |
---|
54 | def subwidgets(self): |
---|
55 | result = [self.getSubWidget(name) for name in self.names] |
---|
56 | return result |
---|
57 | |
---|
58 | def _setUpWidgets(self): |
---|
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 |
---|
65 | |
---|
66 | def _getView(self, request): |
---|
67 | return IkobaObjectWidgetView(self, request) |
---|
68 | |
---|
69 | |
---|
70 | class IkobaObjectDisplayWidget(IkobaObjectWidget): |
---|
71 | |
---|
72 | implementsOnly(IDisplayWidget) |
---|
73 | |
---|
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() |
---|
82 | |
---|
83 | def _getView(self, request): |
---|
84 | if self.context.schema.__name__ == 'IProductOptionEntry': |
---|
85 | return IProductOptionWidgetDisplayView(self, request) |
---|
86 | return IkobaObjectWidgetView(self, request) |
---|