1 | ## $Id: objectwidget.py 7769 2012-03-07 08:27:35Z uli $ |
---|
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 SIRPObjectWidgetView(ObjectWidgetView): |
---|
28 | template = ViewPageTemplateFile('objectwidget.pt') |
---|
29 | |
---|
30 | class SIRPObjectWidget(ObjectWidget): |
---|
31 | |
---|
32 | def __init__(self, context, request, factory, **kw): |
---|
33 | #super(ResultsEntryWidget, self).__init__(context, request) |
---|
34 | super(ObjectWidget, self).__init__(context, request) |
---|
35 | |
---|
36 | # define view that renders the widget |
---|
37 | self.view = self._getView(request) |
---|
38 | |
---|
39 | # factory used to create content that this widget (field) |
---|
40 | # represents |
---|
41 | self.factory = factory |
---|
42 | |
---|
43 | # handle foo_widget specs being passed in |
---|
44 | self.names = getFieldNamesInOrder(self.context.schema) |
---|
45 | for k, v in kw.items(): |
---|
46 | if k.endswith('_widget'): |
---|
47 | setattr(self, k, v) |
---|
48 | |
---|
49 | # set up my subwidgets |
---|
50 | self._setUpWidgets() |
---|
51 | |
---|
52 | def subwidgets(self): |
---|
53 | result = [self.getSubWidget(name) for name in self.names] |
---|
54 | return result |
---|
55 | |
---|
56 | def _setUpWidgets(self): |
---|
57 | return self._setUpEditWidgets() |
---|
58 | |
---|
59 | def _getView(self, request): |
---|
60 | return SIRPObjectWidgetView(self, request) |
---|
61 | |
---|
62 | |
---|
63 | class SIRPObjectDisplayWidget(SIRPObjectWidget): |
---|
64 | |
---|
65 | implementsOnly(IDisplayWidget) |
---|
66 | |
---|
67 | def _setUpDisplayWidgets(self): |
---|
68 | # subwidgets need a new name |
---|
69 | setUpWidgets(self, self.context.schema, IDisplayWidget, |
---|
70 | prefix=self.name, names=self.names, |
---|
71 | context=self.context) |
---|
72 | |
---|
73 | def _setUpWidgets(self): |
---|
74 | return self._setUpDisplayWidgets() |
---|
75 | |
---|
76 | import grok |
---|
77 | from zope.schema.interfaces import IObject |
---|
78 | from zope.formlib.interfaces import IInputWidget |
---|
79 | from zope.publisher.interfaces.browser import IBrowserRequest |
---|
80 | from waeup.sirp.applicants.applicant import ResultEntry |
---|
81 | from waeup.sirp.schema.interfaces import IMultiValObject |
---|
82 | |
---|
83 | #@grok.adapter(IObject, IBrowserRequest) |
---|
84 | @grok.adapter(IMultiValObject, IBrowserRequest) |
---|
85 | @grok.implementer(IInputWidget) |
---|
86 | def obj_input_widget(obj, req): |
---|
87 | return SIRPObjectWidget(obj, req, ResultEntry) |
---|
88 | |
---|
89 | #@grok.adapter(IObject, IBrowserRequest) |
---|
90 | @grok.adapter(IMultiValObject, IBrowserRequest) |
---|
91 | @grok.implementer(IDisplayWidget) |
---|
92 | def obj_display_widget(obj, req): |
---|
93 | return SIRPObjectDisplayWidget(obj, req, ResultEntry) |
---|
94 | #grok.adapts(IObject, IBrowserRequest) |
---|