[Zope-CVS] SVN: zope.webdev/trunk/ Fixed up registration so reregister would get called properly and refactored tests.

Rocky Burt rocky at serverzen.com
Tue Jan 31 08:30:26 EST 2006


Log message for revision 41509:
  Fixed up registration so reregister would get called properly and refactored tests.

Changed:
  U   zope.webdev/trunk/page.py
  U   zope.webdev/trunk/page.txt
  U   zope.webdev/trunk/page.zcml
  U   zope.webdev/trunk/tests.py

-=-
Modified: zope.webdev/trunk/page.py
===================================================================
--- zope.webdev/trunk/page.py	2006-01-31 13:29:50 UTC (rev 41508)
+++ zope.webdev/trunk/page.py	2006-01-31 13:30:25 UTC (rev 41509)
@@ -18,6 +18,7 @@
 __docformat__ = "reStructuredText"
 import persistent
 import zope.interface
+from zope.app.component.interfaces.registration import IRegisterable, IRegistered
 import zope.security.checker
 import zope.app.component.site
 import zope.app.component.interfaces.registration
@@ -29,8 +30,8 @@
 from zope.app import publisher
 from zope.app.presentation import zpt, registration
 from zope.webdev import interfaces
+from zope.app.component.interfaces.registration import IComponentRegistration
 
-
 class Page(persistent.Persistent, zope.app.container.contained.Contained):
     """Local page."""
     zope.interface.implements(interfaces.IPage)
@@ -86,13 +87,11 @@
     def getTemplate(self):
         return self._template
 
-
 class PageRegistration(zope.app.component.site.AdapterRegistration):
 
     provided = zope.interface.Interface
 
     def __init__(self, page):
-
         self.page = page
 
     @property
@@ -111,10 +110,10 @@
     def component(self):
         makeViewClass = MakeViewClass(self.page)
 
-        return registration.TemplateViewFactory(
+        factory = registration.TemplateViewFactory(
             makeViewClass, self.page.getTemplate(), self.page.permission)
+        return factory
 
-
 class MakeViewClass(object):
 
     def __init__(self,page):
@@ -137,3 +136,28 @@
     reg = PageRegistration(page)
     package.registrationManager.addRegistration(reg)
     reg.status = zope.app.component.interfaces.registration.ActiveStatus
+
+def reregisterPage(page):
+    registered = IRegistered(page)
+    for reg in registered.registrations():
+        reg.status = zope.app.component.interfaces.registration.InactiveStatus
+        reg.status = zope.app.component.interfaces.registration.ActiveStatus
+        
+
+def handlePageModification(event, page):
+    reregisterPage(page)
+
+
+class PageRegistered(zope.app.component.registration.Registered):
+    """Default Registered adapter compares the component which would normally
+    be the TemplateViewFactory to self.registerable which is a Page instance.
+    Because we do magic to return the TemplateviewFactory dynamically because
+    of TTW code, we have changed to comparison in the adapter by overriding.
+    """
+    
+    def registrations(self):
+        rm = zapi.getParent(self.registerable).registrationManager
+        return [reg for reg in rm.values()
+                if (IComponentRegistration.providedBy(reg) and
+                    reg.component.cls.page is self.registerable)]    
+                    
\ No newline at end of file

Modified: zope.webdev/trunk/page.txt
===================================================================
--- zope.webdev/trunk/page.txt	2006-01-31 13:29:50 UTC (rev 41508)
+++ zope.webdev/trunk/page.txt	2006-01-31 13:30:25 UTC (rev 41509)
@@ -13,10 +13,14 @@
   >>> site['image.gif'] = object()
   >>> site['file.txt'] = object()
 
-  >>> from zope.webdev.page import Page,registerPage
-  >>> names_html = Page('names.html')
+  >>> from zope.webdev.page import Page,registerPage,reregisterPage
+  >>> from zope.webdev.interfaces import IPage
+  >>> from zope.app.container.interfaces import IReadContainer
+  >>> names_html = Page('names.html', for_=IPage)
   >>> site.getSiteManager()['default']['names.html'] = names_html
   >>> registerPage(names_html)
+  >>> names_html.for_ = IReadContainer
+  >>> reregisterPage(names_html)
 
 
 defaults are: for=Interface, layers=(IDefaultLayer,),
@@ -53,6 +57,7 @@
 
   >>> page = zope.component.getMultiAdapter(
   ...     (site, request), name='names.html')
+  
 
 Now render the page:
 
@@ -94,4 +99,19 @@
   >>> print page()
   abc
 
+  Test event callback to ensure view is reloaded.
   
+  >>> reregisterPage(names_html)
+
+  Make sure the event is being fired properly and the view no longer looks up
+  properly after we change for_.
+
+  >>> from zope.app.event.objectevent import ObjectModifiedEvent
+  >>> from zope.event import notify
+  >>> notify(ObjectModifiedEvent(names_html))
+  >>> names_html.for_ = IReadContainer
+  >>> reregisterPage(names_html)
+  >>> page = zope.component.getMultiAdapter(
+  ...     (site, request), name='names.html')
+  
+

Modified: zope.webdev/trunk/page.zcml
===================================================================
--- zope.webdev/trunk/page.zcml	2006-01-31 13:29:50 UTC (rev 41508)
+++ zope.webdev/trunk/page.zcml	2006-01-31 13:30:25 UTC (rev 41509)
@@ -56,5 +56,11 @@
   <class class="zodbcode.class_.PersistentMethod">
     <allow attributes="__call__" />
   </class>
-
+  
+  <adapter
+    factory=".page.PageRegistered"
+    provides="zope.app.component.interfaces.registration.IRegistered"
+    for=".interfaces.IPage"
+    /> 
+    
 </configure>

Modified: zope.webdev/trunk/tests.py
===================================================================
--- zope.webdev/trunk/tests.py	2006-01-31 13:29:50 UTC (rev 41508)
+++ zope.webdev/trunk/tests.py	2006-01-31 13:30:25 UTC (rev 41509)
@@ -22,7 +22,15 @@
 from zope.testing.doctestunit import DocTestSuite, DocFileSuite
 from zope.app.testing import setup
 
-
+from zope.app.testing import ztapi
+from zope.app.event.interfaces import IObjectModifiedEvent
+from zope.webdev.page import handlePageModification
+from zope.webdev.interfaces import IPage
+from zope.webdev.page import PageRegistered
+from zope.app.component.interfaces.registration import IRegisterable, IRegistered
+from zope.app.event.objectevent import ObjectModifiedEvent
+from zope.app.event.objectevent import objectEventNotify
+  
 def setUp(test):
     setup.placefulSetUp()
 
@@ -30,7 +38,14 @@
 def tearDown(test):
     setup.placefulTearDown()
 
+def pageTestSetUp(test):
+     setup.placefulSetUp()
 
+     ztapi.subscribe([IObjectModifiedEvent, IPage], None, handlePageModification)
+     ztapi.provideAdapter(IPage, IRegistered, PageRegistered)
+     ztapi.subscribe([IObjectModifiedEvent], None, objectEventNotify)
+    
+
 def test_suite():
     return unittest.TestSuite((
         DocFileSuite('package.txt',
@@ -42,7 +57,7 @@
                      optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
                      ),
         DocFileSuite('page.txt',
-                     setUp=setUp, tearDown=tearDown,
+                     setUp=pageTestSetUp, tearDown=tearDown,
                      optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
                      ),
         DocTestSuite('zope.webdev.vocabulary',



More information about the Zope-CVS mailing list