Add additional magic after invokeFactory call
Stefan Bund [Wed, 21 Oct 2009 23:15:48 +0000 (01:15 +0200)]
src/koehsel.policy/koehsel/policy/setuphandlers.py

index dc45961..71cd558 100644 (file)
@@ -1,4 +1,7 @@
 from Products.CMFCore.utils import getToolByName
+from Acquisition import aq_base
+from zope import event
+from Products.Archetypes.event import ObjectInitializedEvent
 
 INSTALL_PRODUCTS = [
     'easyshop.core', 
@@ -7,6 +10,20 @@ INSTALL_PRODUCTS = [
 
 SITE_OWNER = 'admin'
 
+def makePublicObject(container, type_name, id, **kw):
+    """Create an object in CONTAINER with type TYPE_NAME and id ID. Additional keyword args
+specify additional factory arguments. After creation, the object is published if needed."""
+    id = container.invokeFactory(id=id, type_name=type_name, **kw)
+    ob = getattr(container,id)
+    event.notify(ObjectInitializedEvent(ob))
+    ob.at_post_create_script()
+#    if hasattr(aq_base(ob), 'manage_afterPortalFactoryCreate'):
+#        ob.manage_afterPortalFactoryCreate()
+    wf = getToolByName(container, 'portal_workflow')
+    if wf.getInfoFor(ob,'review_state','') != 'published':
+        wf.doActionFor(ob,'publish',comment='site setup')
+
+
 def siteSetup(context):
 
     if context.readDataFile('koehsel.policy_setup.txt') is None:
@@ -26,12 +43,8 @@ def siteSetup(context):
 
     try: site.shop
     except AttributeError:
-        site.invokeFactory(id='shop', type_name='EasyShop')
-        shop = site.shop
-        shop.title = 'Shop'
-        shop.shopOwner = 'admin'
-        if wf.getInfoFor(shop,'review_state','') != 'published':
-            wf.doActionFor(shop,'publish',comment='site setup')
+        makePublicObject(container=site, type_name='EasyShop', 
+                         id='shop', title='Shop', shopOwner='admin')
 
     # Hide Users, News and Events