Skip to content

Commit

Permalink
Forms now notify the ObjectEditedEvent instead of the ObjectModifiedE…
Browse files Browse the repository at this point in the history
…vent.
  • Loading branch information
thefunny42 committed Mar 20, 2015
1 parent 2663e73 commit 761f8e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGES.txt
Expand Up @@ -4,7 +4,7 @@ Changes
1.10a2 (unreleased)
-------------------

- Nothing changed yet.
- Forms now notify the ObjectEditedEvent instead of the ObjectModifiedEvent.


1.10a1 (2013-11-22)
Expand Down
6 changes: 4 additions & 2 deletions setup.py
@@ -1,6 +1,7 @@
from setuptools import setup, find_packages
import os


def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()

Expand Down Expand Up @@ -34,13 +35,14 @@ def read(*rnames):
'Framework :: Zope3',
],
packages=find_packages('src'),
package_dir = {'': 'src'},
package_dir={'': 'src'},
namespace_packages=['grokcore'],
include_package_data = True,
include_package_data=True,
zip_safe=False,
install_requires=[
'setuptools',
'grokcore.component >= 2.1',
'grokcore.content',
'grokcore.security >= 1.5',
'grokcore.view >= 2.0',
'martian >= 0.13',
Expand Down
34 changes: 23 additions & 11 deletions src/grokcore/formlib/formlib.py
Expand Up @@ -14,24 +14,30 @@
"""Custom implementations of formlib helpers
"""

from zope import interface, event, lifecycleevent
from zope.interface.interfaces import IInterface
from zope.formlib import form
from zope.schema.interfaces import IField
from grokcore.content import ObjectEditedEvent
import zope.event
import zope.formlib.form
import zope.interface
import zope.lifecycleevent

class action(form.action):

class action(zope.formlib.form.action):
"""We override the action decorator we pass in our custom Action.
"""
def __call__(self, success):
action = Action(self.label, success=success, **self.options)
self.actions.append(action)
return action

class Action(form.Action):

class Action(zope.formlib.form.Action):
def success(self, data):
if self.success_handler is not None:
return self.success_handler(self.form, **data)


def Fields(*args, **kw):
fields = []
for key, value in kw.items():
Expand All @@ -40,23 +46,25 @@ def Fields(*args, **kw):
fields.append(value)
del kw[key]
fields.sort(key=lambda field: field.order)
return form.Fields(*(args + tuple(fields)), **kw)
return zope.formlib.form.Fields(*(args + tuple(fields)), **kw)


def get_auto_fields(context):
"""Get the form fields for context.
"""
# for an interface context, we generate them from that interface
if IInterface.providedBy(context):
return form.Fields(context)
return zope.formlib.form.Fields(context)
# if we have a non-interface context, we're autogenerating them
# from any schemas defined by the context
fields = form.Fields(*most_specialized_interfaces(context))
fields = zope.formlib.form.Fields(*most_specialized_interfaces(context))
# we pull in this field by default, but we don't want it in our form
fields = fields.omit('__name__')
return fields

AutoFields = get_auto_fields


def most_specialized_interfaces(context):
"""Get interfaces for an object without any duplicates.
Expand All @@ -65,14 +73,15 @@ def most_specialized_interfaces(context):
interface twice, as that would result in duplicate names when creating
the form.
"""
declaration = interface.implementedBy(context)
declaration = zope.interface.implementedBy(context)
seen = []
for iface in declaration.flattened():
if interface_seen(seen, iface):
continue
seen.append(iface)
return seen


def interface_seen(seen, iface):
"""Return True if interface already is seen.
"""
Expand All @@ -81,6 +90,7 @@ def interface_seen(seen, iface):
return True
return False


def apply_data(context, form_fields, data, adapters=None, update=False):
"""Save form data (``data`` dict) on a ``context`` object.
Expand Down Expand Up @@ -111,7 +121,7 @@ def apply_data(context, form_fields, data, adapters=None, update=False):
adapters[interface] = adapter

name = form_field.__name__
newvalue = data.get(name, form_field) # using form_field as marker
newvalue = data.get(name, form_field) # using form_field as marker

if update:
if ((newvalue is not form_field) and
Expand All @@ -125,6 +135,7 @@ def apply_data(context, form_fields, data, adapters=None, update=False):

return changes


def apply_data_event(context, form_fields, data, adapters=None, update=False):
"""Like apply_data, but also sends an IObjectModifiedEvent.
"""
Expand All @@ -133,7 +144,8 @@ def apply_data_event(context, form_fields, data, adapters=None, update=False):
if changes:
descriptions = []
for interface, names in changes.items():
descriptions.append(lifecycleevent.Attributes(interface, *names))
event.notify(lifecycleevent.ObjectModifiedEvent(context, *descriptions))
descriptions.append(
zope.lifecycleevent.Attributes(interface, *names))
zope.event.notify(ObjectEditedEvent(context, *descriptions))

return changes

0 comments on commit 761f8e6

Please sign in to comment.