-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed all calls to zope.app.event.publish to use zope.event.notify.
- Loading branch information
Jim Fulton
committed
May 26, 2004
1 parent
4b9ed5c
commit d2b7983
Showing
4 changed files
with
688 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
############################################################################## | ||
# | ||
# Copyright (c) 2001, 2002 Zope Corporation and Contributors. | ||
# All Rights Reserved. | ||
# | ||
# This software is subject to the provisions of the Zope Public License, | ||
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | ||
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | ||
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | ||
# FOR A PARTICULAR PURPOSE. | ||
# | ||
############################################################################## | ||
"""Add Form View class | ||
$Id$ | ||
""" | ||
import sys | ||
|
||
from zope.app import zapi | ||
from zope.event import notify | ||
from zope.app.event.objectevent import ObjectCreatedEvent | ||
from zope.app.form.utility import setUpWidgets, getWidgetsData | ||
from zope.app.i18n import ZopeMessageIDFactory as _ | ||
from zope.app.form.interfaces import IInputWidget, WidgetsError | ||
from zope.app.pagetemplate.simpleviewclass import SimpleViewClass | ||
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile | ||
from zope.component import getAdapter | ||
from zope.publisher.interfaces.browser import IBrowserRequest | ||
from zope.schema.interfaces import ValidationError | ||
from zope.security.checker import defineChecker, NamesChecker | ||
from zope.app.publisher.browser.globalbrowsermenuservice import \ | ||
globalBrowserMenuService | ||
from editview import EditView | ||
from submit import Update | ||
|
||
class AddView(EditView): | ||
"""Simple edit-view base class. | ||
Subclasses should provide a schema attribute defining the schema | ||
to be edited. | ||
""" | ||
|
||
def _setUpWidgets(self): | ||
setUpWidgets(self, self.schema, IInputWidget, names=self.fieldNames) | ||
|
||
def update(self): | ||
|
||
if self.update_status is not None: | ||
# We've been called before. Just return the previous result. | ||
return self.update_status | ||
|
||
if Update in self.request: | ||
|
||
self.update_status = '' | ||
try: | ||
data = getWidgetsData(self, self.schema, names=self.fieldNames) | ||
self.createAndAdd(data) | ||
except WidgetsError, errors: | ||
self.errors = errors | ||
self.update_status = _("An error occured.") | ||
return self.update_status | ||
|
||
self.request.response.redirect(self.nextURL()) | ||
|
||
return self.update_status | ||
|
||
def create(self, *args, **kw): | ||
"""Do the actual instantiation.""" | ||
return self._factory(*args, **kw) | ||
|
||
def createAndAdd(self, data): | ||
"""Add the desired object using the data in the data argument. | ||
The data argument is a dictionary with the data entered in the form. | ||
""" | ||
|
||
args = [] | ||
if self._arguments: | ||
for name in self._arguments: | ||
args.append(data[name]) | ||
|
||
kw = {} | ||
if self._keyword_arguments: | ||
for name in self._keyword_arguments: | ||
if name in data: | ||
kw[str(name)] = data[name] | ||
|
||
content = self.create(*args, **kw) | ||
adapted = getAdapter(content, self.schema) | ||
|
||
errors = [] | ||
|
||
if self._set_before_add: | ||
for name in self._set_before_add: | ||
if name in data: | ||
field = self.schema[name] | ||
try: | ||
field.set(adapted, data[name]) | ||
except ValidationError: | ||
errors.append(sys.exc_info()[1]) | ||
|
||
if errors: | ||
raise WidgetsError(*errors) | ||
|
||
notify(ObjectCreatedEvent(content)) | ||
|
||
content = self.add(content) | ||
|
||
adapted = getAdapter(content, self.schema) | ||
|
||
if self._set_after_add: | ||
for name in self._set_after_add: | ||
if name in data: | ||
field = self.schema[name] | ||
try: | ||
field.set(adapted, data[name]) | ||
except ValidationError: | ||
errors.append(sys.exc_info()[1]) | ||
|
||
if errors: | ||
raise WidgetsError(*errors) | ||
|
||
return content | ||
|
||
def add(self, content): | ||
return self.context.add(content) | ||
|
||
def nextURL(self): | ||
return self.context.nextURL() | ||
|
||
|
||
def AddViewFactory(name, schema, label, permission, layer, | ||
template, default_template, bases, for_, | ||
fields, content_factory, arguments, | ||
keyword_arguments, set_before_add, set_after_add, | ||
menu=u''): | ||
|
||
s = zapi.getGlobalService(zapi.servicenames.Presentation) | ||
class_ = SimpleViewClass( | ||
template, | ||
used_for = schema, bases = bases | ||
) | ||
|
||
class_.schema = schema | ||
class_.label = label | ||
class_.fieldNames = fields | ||
class_._factory = content_factory | ||
class_._arguments = arguments | ||
class_._keyword_arguments = keyword_arguments | ||
class_._set_before_add = set_before_add | ||
class_._set_after_add = set_after_add | ||
|
||
class_.generated_form = ViewPageTemplateFile(default_template) | ||
|
||
defineChecker(class_, | ||
NamesChecker( | ||
("__call__", "__getitem__", | ||
"browserDefault", "publishTraverse"), | ||
permission, | ||
) | ||
) | ||
|
||
s.provideView(for_, name, IBrowserRequest, class_, layer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
############################################################################## | ||
# | ||
# Copyright (c) 2001, 2002 Zope Corporation and Contributors. | ||
# All Rights Reserved. | ||
# | ||
# This software is subject to the provisions of the Zope Public License, | ||
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | ||
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | ||
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | ||
# FOR A PARTICULAR PURPOSE. | ||
# | ||
############################################################################## | ||
"""Add Wizard View Classes | ||
$Id$ | ||
""" | ||
import sys | ||
|
||
from zope.app import zapi | ||
from zope.event import notify | ||
from zope.app.event.objectevent import ObjectCreatedEvent | ||
from zope.app.form.utility import setUpWidgets | ||
from zope.app.form.interfaces import WidgetsError, IInputWidget | ||
from zope.app.pagetemplate.simpleviewclass import SimpleViewClass | ||
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile | ||
from zope.component import getAdapter | ||
from zope.publisher.interfaces.browser import IBrowserRequest | ||
from zope.schema.interfaces import ValidationError | ||
from zope.security.checker import defineChecker, NamesChecker | ||
from editwizard import EditWizardView, WizardStorage | ||
|
||
class AddWizardView(EditWizardView): | ||
"""Multi-page add-view base class. | ||
Subclasses should provide a schema attribute defining the schema | ||
to be edited. | ||
""" | ||
|
||
def _setUpWidgets(self): | ||
if self.use_session: | ||
# Need session for File upload fields | ||
raise NotImplementedError, 'Need a working ISessionDataManager' | ||
else: | ||
self.storage = WizardStorage(self.fieldNames, None) | ||
|
||
setUpWidgets(self, self.schema, IInputWidget, names=self.fieldNames) | ||
|
||
def create(self, *args, **kw): | ||
"""Do the actual instantiation.""" | ||
return self._factory(*args, **kw) | ||
|
||
def apply_update(self, data): | ||
"""Add the desired object using the data in the data argument. | ||
The data argument is a dictionary with the data entered in the form. | ||
Issues a redirect to context.nextURL() | ||
Returns False, as per editview.apply_update | ||
""" | ||
|
||
# This code originally from add.py's createAndAdd method | ||
|
||
args = [] | ||
for name in self._arguments: | ||
args.append(data[name]) | ||
|
||
kw = {} | ||
for name in self._keyword_arguments: | ||
if name in data: | ||
kw[str(name)] = data[name] | ||
|
||
content = self.create(*args, **kw) | ||
adapted = getAdapter(content, self.schema, context=self.context) | ||
|
||
errors = [] | ||
|
||
for name in self._set_before_add: | ||
if name in data: | ||
field = self.schema[name] | ||
try: | ||
field.set(adapted, data[name]) | ||
except ValidationError: | ||
errors.append(sys.exc_info()[1]) | ||
|
||
if errors: | ||
raise WidgetsError(*errors) | ||
|
||
notify(ObjectCreatedEvent(content)) | ||
|
||
content = self.context.add(content) | ||
|
||
adapted = getAdapter(content, self.schema) | ||
|
||
for name in self._set_after_add: | ||
if name in data: | ||
field = self.schema[name] | ||
try: | ||
field.set(adapted, data[name]) | ||
except ValidationError: | ||
errors.append(sys.exc_info()[1]) | ||
|
||
if errors: | ||
raise WidgetsError(*errors) | ||
|
||
self.request.response.redirect(self.context.nextURL()) | ||
return False | ||
|
||
|
||
def AddWizardViewFactory( | ||
name, schema, permission, layer, panes, fields, | ||
template, default_template, bases, for_, content_factory, arguments, | ||
keyword_arguments, set_before_add, set_after_add, use_session=True): | ||
|
||
class_ = SimpleViewClass(template, used_for = schema, bases = bases) | ||
|
||
class_.schema = schema | ||
class_.panes = panes | ||
class_.fieldNames = fields | ||
class_._factory = content_factory | ||
class_._arguments = arguments or [] | ||
class_._keyword_arguments = keyword_arguments or [] | ||
class_._set_before_add = set_before_add or [] | ||
class_._set_after_add = set_after_add or [] | ||
class_.use_session = use_session | ||
|
||
class_.generated_form = ViewPageTemplateFile(default_template) | ||
|
||
defineChecker(class_, | ||
NamesChecker( | ||
("__call__", "__getitem__", "browserDefault"), | ||
permission, | ||
) | ||
) | ||
|
||
s = zapi.getGlobalService(zapi.servicenames.Presentation) | ||
s.provideView(for_, name, IBrowserRequest, class_, layer) |
Oops, something went wrong.