Check for persistence support in add-form output result
authorThierry Florac <thierry.florac@onf.fr>
Mon, 24 Sep 2018 15:48:59 +0200
changeset 142 5a6990958cb3
parent 141 d83e8f9a9885
child 143 b80f490dee13
Check for persistence support in add-form output result
src/pyams_form/form.py
--- a/src/pyams_form/form.py	Fri Sep 21 16:28:25 2018 +0200
+++ b/src/pyams_form/form.py	Mon Sep 24 15:48:59 2018 +0200
@@ -12,49 +12,45 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-import json
 import logging
 logger = logging.getLogger('PyAMS (form)')
 
+import json
 import transaction
 import venusian
-
-# import interfaces
-from pyams_form.interfaces.form import IFormLayer, IForm, IAJAXForm, IInnerSubForm, IInnerTabForm, \
-    ICustomUpdateSubForm, IFormCreatedEvent, FormCreatedEvent, IInnerForm, IFormContextPermissionChecker
-from pyams_form.interfaces.form import IAddFormButtons, IModalAddFormButtons, IEditFormButtons, \
-    IModalEditFormButtons, IModalDisplayFormButtons
-from pyams_form.interfaces import get_form_weight
-from pyams_form.interfaces.form import FormObjectCreatedEvent, FormObjectModifiedEvent
-from pyams_i18n.interfaces import II18n
-from pyams_skin.interfaces import IDialog, ISkinnable, IContentTitle
-from pyams_template.interfaces import IContentTemplate, ILayoutTemplate
-from pyams_utils.interfaces import FORBIDDEN_PERMISSION, ICacheKeyValue
-from pyramid_chameleon.interfaces import IChameleonTranslate
-from z3c.form.interfaces import DISPLAY_MODE, IErrorViewSnippet, IMultipleErrors
-from zope.publisher.interfaces.browser import IBrowserRequest
-from zope.schema.interfaces import ValidationError
-
-# import packages
-from pyams_form.group import GroupsBasedForm
-from pyams_pagelet.interfaces import PageletCreatedEvent
-from pyams_skin.skin import apply_skin
-from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
-from pyams_utils.url import absolute_url
+from persistent import IPersistent
 from pyramid.decorator import reify
 from pyramid.events import subscriber
 from pyramid.response import Response
-from pyramid_zope_request import PyramidToPublisher, PyramidPublisherRequest
+from pyramid_chameleon.interfaces import IChameleonTranslate
+from pyramid_zope_request import PyramidPublisherRequest, PyramidToPublisher
 from z3c.form.button import Buttons
-from z3c.form.form import applyChanges, \
-    Form, AddForm as BaseAddForm, EditForm as BaseEditForm, DisplayForm as BaseDisplayForm
+from z3c.form.form import AddForm as BaseAddForm, DisplayForm as BaseDisplayForm, EditForm as BaseEditForm, Form, \
+    applyChanges
+from z3c.form.interfaces import DISPLAY_MODE, IErrorViewSnippet, IMultipleErrors
 from zope.component import queryUtility
-from zope.interface import implementer, classImplements, alsoProvides, Interface
+from zope.interface import Interface, alsoProvides, classImplements, implementer
 from zope.lifecycleevent import Attributes, ObjectCreatedEvent
 from zope.location import locate
+from zope.publisher.interfaces.browser import IBrowserRequest
 from zope.schema.fieldproperty import FieldProperty
+from zope.schema.interfaces import ValidationError
+
+from pyams_form.group import GroupsBasedForm
+from pyams_form.interfaces import get_form_weight
+from pyams_form.interfaces.form import FormCreatedEvent, IAJAXForm, ICustomUpdateSubForm, IForm, \
+    IFormContextPermissionChecker, IFormCreatedEvent, IFormLayer, IInnerForm, IInnerSubForm, IInnerTabForm
+from pyams_form.interfaces.form import FormObjectCreatedEvent, FormObjectModifiedEvent
+from pyams_form.interfaces.form import IAddFormButtons, IEditFormButtons, IModalAddFormButtons, \
+    IModalDisplayFormButtons, IModalEditFormButtons
+from pyams_i18n.interfaces import II18n
+from pyams_pagelet.interfaces import PageletCreatedEvent
+from pyams_skin.interfaces import IContentTitle, IDialog, ISkinnable
+from pyams_skin.skin import apply_skin
+from pyams_template.interfaces import IContentTemplate, ILayoutTemplate
+from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyams_utils.interfaces import FORBIDDEN_PERMISSION, ICacheKeyValue
+from pyams_utils.url import absolute_url
 
 from pyams_form import _
 
@@ -362,7 +358,7 @@
         registry = self.request.registry
         # create object
         object = self.create(data.get(self, data))
-        if object is not None:
+        if IPersistent.providedBy(object):
             registry.notify(ObjectCreatedEvent(object))
             # set parent temporarily to avoid NotYet exceptions
             locate(object, self.context)
@@ -746,7 +742,7 @@
             if 'implementer' in settings:
                 implementer = settings.pop('implementer')
                 if not isinstance(implementer, (list, tuple, set)):
-                    implementer = (implementer, )
+                    implementer = (implementer,)
                 classImplements(new_class, *implementer)
 
             logger.debug('Registering AJAX view "{0}" for {1} ({2})'.format(settings.get('name'),