[Checkins] SVN: megrok.z3cform.base/trunk/src/megrok/z3cform/base/ Correcting imports, changing the behavior of apply_data_event, adding tests
Souheil CHELFOUH
souheil at chelfouh.com
Wed Oct 7 07:04:15 EDT 2009
Log message for revision 104850:
Correcting imports, changing the behavior of apply_data_event, adding tests
Changed:
U megrok.z3cform.base/trunk/src/megrok/z3cform/base/__init__.py
U megrok.z3cform.base/trunk/src/megrok/z3cform/base/components.py
U megrok.z3cform.base/trunk/src/megrok/z3cform/base/directives.py
U megrok.z3cform.base/trunk/src/megrok/z3cform/base/interfaces.py
U megrok.z3cform.base/trunk/src/megrok/z3cform/base/tests/test_utils.py
U megrok.z3cform.base/trunk/src/megrok/z3cform/base/utils.py
-=-
Modified: megrok.z3cform.base/trunk/src/megrok/z3cform/base/__init__.py
===================================================================
--- megrok.z3cform.base/trunk/src/megrok/z3cform/base/__init__.py 2009-10-07 09:05:57 UTC (rev 104849)
+++ megrok.z3cform.base/trunk/src/megrok/z3cform/base/__init__.py 2009-10-07 11:04:15 UTC (rev 104850)
@@ -8,8 +8,7 @@
from z3c.form.interfaces import DISPLAY_MODE, INPUT_MODE
# Public interface
-from utils import apply_data_event
+from components import *
+from directives import *
from interfaces import IGrokForm, ICancelButton
-from directives import field, cancellable
-from components import Form, AddForm, EditForm, DisplayForm, WidgetTemplate
-from components import PageForm, PageAddForm, PageEditForm, PageDisplayForm
+from utils import apply_data_event, changes_notification, set_fields_data
Modified: megrok.z3cform.base/trunk/src/megrok/z3cform/base/components.py
===================================================================
--- megrok.z3cform.base/trunk/src/megrok/z3cform/base/components.py 2009-10-07 09:05:57 UTC (rev 104849)
+++ megrok.z3cform.base/trunk/src/megrok/z3cform/base/components.py 2009-10-07 11:04:15 UTC (rev 104850)
@@ -124,7 +124,6 @@
grok.baseclass()
-
class DisplayForm(GrokForm, form.DisplayForm, grok.View):
"""z3c display form.
"""
@@ -166,3 +165,8 @@
class WidgetTemplate(object):
pass
+
+
+__all__ = ("Form", "AddForm", "EditForm", "DisplayForm",
+ "WidgetTemplate", "PageForm", "PageAddForm",
+ "PageEditForm", "PageDisplayForm")
Modified: megrok.z3cform.base/trunk/src/megrok/z3cform/base/directives.py
===================================================================
--- megrok.z3cform.base/trunk/src/megrok/z3cform/base/directives.py 2009-10-07 09:05:57 UTC (rev 104849)
+++ megrok.z3cform.base/trunk/src/megrok/z3cform/base/directives.py 2009-10-07 11:04:15 UTC (rev 104850)
@@ -31,3 +31,6 @@
scope = martian.CLASS
store = martian.ONCE
default = False
+
+
+__all__ = ("field", "mode", "widget", "cancellable")
Modified: megrok.z3cform.base/trunk/src/megrok/z3cform/base/interfaces.py
===================================================================
--- megrok.z3cform.base/trunk/src/megrok/z3cform/base/interfaces.py 2009-10-07 09:05:57 UTC (rev 104849)
+++ megrok.z3cform.base/trunk/src/megrok/z3cform/base/interfaces.py 2009-10-07 11:04:15 UTC (rev 104850)
@@ -12,3 +12,6 @@
class ICancelButton(IButton):
"""A button to cancel a form.
"""
+
+
+__all__ = ("IGrokForm", "ICancelButton")
Modified: megrok.z3cform.base/trunk/src/megrok/z3cform/base/tests/test_utils.py
===================================================================
--- megrok.z3cform.base/trunk/src/megrok/z3cform/base/tests/test_utils.py 2009-10-07 09:05:57 UTC (rev 104849)
+++ megrok.z3cform.base/trunk/src/megrok/z3cform/base/tests/test_utils.py 2009-10-07 11:04:15 UTC (rev 104850)
@@ -2,7 +2,9 @@
>>> person = Person()
>>> IPerson.providedBy(person)
True
- >>> person.name = u"christian"
+ >>> changes = set_fields_data(Fields(IPerson), person, {'name': u'james'})
+ >>> changes
+
>>> from zope.event import notify
>>> from zope.lifecycleevent import Attributes
>>> notify(ObjectModifiedEvent(person, *[Attributes(IPerson, 'name')]))
@@ -14,11 +16,13 @@
from zope.schema import TextLine
from zope.interface import Interface
from zope.lifecycleevent import ObjectModifiedEvent
+from from z3c.form.field import Fields
+from megrok.z3cform.base.utils import *
-
class IPerson(Interface):
name = TextLine(title=u"Name")
+
class Person(grok.Context):
grok.implements(IPerson)
Modified: megrok.z3cform.base/trunk/src/megrok/z3cform/base/utils.py
===================================================================
--- megrok.z3cform.base/trunk/src/megrok/z3cform/base/utils.py 2009-10-07 09:05:57 UTC (rev 104849)
+++ megrok.z3cform.base/trunk/src/megrok/z3cform/base/utils.py 2009-10-07 11:04:15 UTC (rev 104850)
@@ -1,17 +1,53 @@
# -*- coding: utf-8 -*-
from zope.event import notify
-from z3c.form.form import applyChanges
+from z3c.form import interfaces
+from zope.event import notify
+from zope.schema import IObject
from zope.lifecycleevent import Attributes, ObjectModifiedEvent
-def apply_data_event(form, context, data):
- """ Updates the object with the data and sends an IObjectModifiedEvent
+def set_fields_data(fields_manager, content, data):
+ """Applies the values to the fields, if a change has been made and
+ if the field is present in the given fields manager. It returns a
+ dictionnary describing the changes applied with the name of the field
+ and the interface from where it's from.
"""
- changes = applyChanges(form, context, data)
+ changes = {}
+ for name, field in fields_manager.items():
+
+ if name not in data or data[name] is interfaces.NOT_CHANGED:
+ continue
+
+ dm = zope.component.getMultiAdapter(
+ (content, field.field), interfaces.IDataManager)
+
+ if dm.get() != data[name] or IObject.providedBy(field.field):
+ dm.set(data[name])
+ changes.setdefault(dm.field.interface, []).append(name)
+
+ return changes
+
+
+def changes_notification(content, changes):
+ """Builds a list of descriptions, made of Attributes objects, defining
+ the changes made on the content and the related interface.
+ """
if changes:
descriptions = []
for interface, names in changes.items():
descriptions.append(Attributes(interface, *names))
- notify(ObjectModifiedEvent(context, *descriptions))
+ notify(ObjectModifiedEvent(content, *descriptions))
+ return description
+ return None
+
+
+def apply_data_event(fields, content, data):
+ """ Updates the object with the data and sends an IObjectModifiedEvent
+ """
+ changes = set_fields_data(fields, content, data)
+ changes and changes_notification(content, changes)
return changes
+
+
+__all__ = ("set_fields_data", "changes_notification", "apply_data_event")
More information about the checkins
mailing list