vendor/Five/1.2b-r20590

view form/metaconfigure.py @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2004, 2005 Zope Corporation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 """Edit form directives
16 $Id: metaconfigure.py 12884 2005-05-30 13:10:41Z philikon $
17 """
18 import ExtensionClass
20 from zope.component import getGlobalService
21 from zope.component.servicenames import Presentation
22 from zope.publisher.interfaces.browser import IBrowserRequest
23 from zope.app.publisher.browser.globalbrowsermenuservice import \
24 menuItemDirective
25 from zope.app.form.browser.metaconfigure import BaseFormDirective
26 from zope.app.container.interfaces import IAdding
28 from Products.Five.form import EditView, AddView
29 from Products.Five.metaclass import makeClass
30 from Products.Five.security import protectClass, initializeClass
31 from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile
32 from Products.Five.browser.metaconfigure import makeClassForTemplate
34 def EditViewFactory(name, schema, label, permission, layer,
35 template, default_template, bases, for_, fields,
36 fulledit_path=None, fulledit_label=None, menu=u''):
37 s = getGlobalService(Presentation)
38 class_ = makeClassForTemplate(template, globals(), used_for=schema,
39 bases=bases)
40 class_.schema = schema
41 class_.label = label
42 class_.fieldNames = fields
44 class_.fulledit_path = fulledit_path
45 if fulledit_path and (fulledit_label is None):
46 fulledit_label = "Full edit"
48 class_.fulledit_label = fulledit_label
50 class_.generated_form = ZopeTwoPageTemplateFile(default_template)
53 s.provideView(for_, name, IBrowserRequest, class_, layer)
54 protectClass(class_, permission)
55 initializeClass(class_)
57 class FiveFormDirective(BaseFormDirective):
59 def _processWidgets(self):
60 if self._widgets:
61 customWidgetsObject = makeClass('CustomWidgetsMixin', (ExtensionClass.Base,), self._widgets)
62 self.bases = self.bases + (customWidgetsObject,)
64 class EditFormDirective(FiveFormDirective):
66 view = EditView
67 default_template = 'edit.pt'
68 title = 'Edit'
70 def _handle_menu(self):
71 if self.menu:
72 menuItemDirective(
73 self._context, self.menu, self.for_ or self.schema,
74 '@@' + self.name, self.title, permission=self.permission)
76 def __call__(self):
77 self._processWidgets()
78 self._handle_menu()
79 self._context.action(
80 discriminator=self._discriminator(),
81 callable=EditViewFactory,
82 args=self._args(),
83 kw={'menu': self.menu},
84 )
87 def AddViewFactory(name, schema, label, permission, layer,
88 template, default_template, bases, for_,
89 fields, content_factory, arguments,
90 keyword_arguments, set_before_add, set_after_add,
91 menu=u''):
93 s = getGlobalService(Presentation)
94 class_ = makeClassForTemplate(template, globals(), used_for=schema,
95 bases=bases)
97 class_.schema = schema
98 class_.label = label
99 class_.fieldNames = fields
100 class_._factory = content_factory
101 class_._arguments = arguments
102 class_._keyword_arguments = keyword_arguments
103 class_._set_before_add = set_before_add
104 class_._set_after_add = set_after_add
106 class_.generated_form = ZopeTwoPageTemplateFile(default_template)
108 s.provideView(for_, name, IBrowserRequest, class_, layer)
109 protectClass(class_, permission)
110 initializeClass(class_)
112 class AddFormDirective(FiveFormDirective):
114 view = AddView
115 default_template = 'add.pt'
116 for_ = IAdding
118 # default add form information
119 description = None
120 content_factory = None
121 arguments = None
122 keyword_arguments = None
123 set_before_add = None
124 set_after_add = None
126 def _handle_menu(self):
127 if self.menu or self.title:
128 if (not self.menu) or (not self.title):
129 raise ValueError("If either menu or title are specified, "
130 "they must both be specified")
131 # Add forms are really for IAdding components, so do not use
132 # for=self.schema.
133 menuItemDirective(
134 self._context, self.menu, self.for_, '@@' + self.name,
135 self.title, permission=self.permission,
136 description=self.description)
138 def _handle_arguments(self, leftover=None):
139 schema = self.schema
140 fields = self.fields
141 arguments = self.arguments
142 keyword_arguments = self.keyword_arguments
143 set_before_add = self.set_before_add
144 set_after_add = self.set_after_add
146 if leftover is None:
147 leftover = fields
149 if arguments:
150 missing = [n for n in arguments if n not in fields]
151 if missing:
152 raise ValueError("Some arguments are not included in the form",
153 missing)
154 optional = [n for n in arguments if not schema[n].required]
155 if optional:
156 raise ValueError("Some arguments are optional, use"
157 " keyword_arguments for them",
158 optional)
159 leftover = [n for n in leftover if n not in arguments]
161 if keyword_arguments:
162 missing = [n for n in keyword_arguments if n not in fields]
163 if missing:
164 raise ValueError(
165 "Some keyword_arguments are not included in the form",
166 missing)
167 leftover = [n for n in leftover if n not in keyword_arguments]
169 if set_before_add:
170 missing = [n for n in set_before_add if n not in fields]
171 if missing:
172 raise ValueError(
173 "Some set_before_add are not included in the form",
174 missing)
175 leftover = [n for n in leftover if n not in set_before_add]
177 if set_after_add:
178 missing = [n for n in set_after_add if n not in fields]
179 if missing:
180 raise ValueError(
181 "Some set_after_add are not included in the form",
182 missing)
183 leftover = [n for n in leftover if n not in set_after_add]
185 self.set_after_add += leftover
187 else:
188 self.set_after_add = leftover
190 def __call__(self):
191 self._processWidgets()
192 self._handle_menu()
193 self._handle_arguments()
195 self._context.action(
196 discriminator=self._discriminator(),
197 callable=AddViewFactory,
198 args=self._args()+(self.content_factory, self.arguments,
199 self.keyword_arguments,
200 self.set_before_add, self.set_after_add),
201 kw={'menu': self.menu},
202 )