Back to index

plone3  3.1.7
__init__.py
Go to the documentation of this file.
00001 from zope.event import notify
00002 from wicked.fieldevent.interfaces import IFieldEvent, IFieldRenderEvent, IFieldStorageEvent
00003 from wicked.fieldevent.interfaces import IFieldValue, IFieldValueSetter, IField
00004 from wicked.fieldevent.interfaces import FieldRenderEvent, FieldStorageEvent
00005 from zope.component import subscribers, getMultiAdapter, handle
00006 from zope.component import adapter, adapts
00007 from zope.interface import implementer, implements
00008 
00009 import logging
00010 logger = logging.getLogger('wicked.fieldevent')
00011 
00012 @adapter(IFieldEvent)
00013 def notifyFieldEvent(event):
00014     field = event.field
00015     if IFieldRenderEvent.providedBy(event):
00016         event.value = getMultiAdapter((field, event), IFieldValue)
00017         if event.kwargs.get('raw', False) or getattr(event, 'raw', False):
00018             # bail out
00019             return 
00020 
00021     handle(event.field, event.instance, event)
00022     
00023     if IFieldStorageEvent.providedBy(event):
00024         for ignore in subscribers((field, event), IFieldValueSetter):
00025             pass
00026 
00027 def render(field_, instance, **kwargs):
00028     renderer=FieldRenderEvent(field_, instance, **kwargs)
00029     notify(renderer)
00030     return renderer.value
00031 
00032 def store(field_, instance, value, **kwargs):
00033     store = FieldStorageEvent(field_, instance, value, **kwargs)
00034     notify(store)
00035 
00036 # @@ separate IATField interfaces?
00037 @implementer(IFieldValue)
00038 @adapter(IField, IFieldRenderEvent)
00039 def at_field_retrieval(field, event):
00040     return field.get(event.instance, **event.kwargs)
00041 
00042 # @@ separate IATField interfaces?
00043 @implementer(IFieldValueSetter)
00044 @adapter(IField, IFieldStorageEvent)
00045 def at_field_storage(field, event):
00046     field.set(event.instance, event.value, **event.kwargs)
00047 
00048 ## testing! ##
00049 
00050 def test_suite():
00051     import unittest
00052     from zope.testing import doctest
00053     from zope.component import provideAdapter, provideHandler
00054     from zope.component import provideSubscriptionAdapter
00055     from zope.testing.cleanup import cleanUp
00056     
00057     def setUp(tc):
00058         # clean slate!
00059         cleanUp()
00060 
00061         # init event system
00062         from zope.component import event
00063         
00064         # register components
00065         provideHandler(notifyFieldEvent)
00066         provideAdapter(at_field_retrieval)
00067         provideSubscriptionAdapter(at_field_storage)
00068 
00069     def tearDown(tc):
00070         cleanUp()
00071         
00072     globs=globals()
00073     globs.update(locals())
00074     
00075     optflags = doctest.REPORT_ONLY_FIRST_FAILURE | doctest.ELLIPSIS
00076     rm = doctest.DocFileTest("README.txt",
00077                              package="wicked.fieldevent",
00078                              globs=globs,
00079                              setUp=setUp,
00080                              tearDown=tearDown,
00081                              optionflags=optflags)
00082     
00083     return unittest.TestSuite((rm,))