Add additional magic after invokeFactory call
[zope-bootstrap.git] / src / koehsel.policy / koehsel / policy / setuphandlers.py
index 5140789..71cd558 100644 (file)
@@ -1,11 +1,76 @@
-def setupVarious(context):
+from Products.CMFCore.utils import getToolByName
+from Acquisition import aq_base
+from zope import event
+from Products.Archetypes.event import ObjectInitializedEvent
 
-    # Ordinarily, GenericSetup handlers check for the existence of XML files.
-    # Here, we are not parsing an XML file, but we use this text file as a
-    # flag to check that we actually meant for this import step to be run.
-    # The file is found in profiles/default.
+INSTALL_PRODUCTS = [
+    'easyshop.core', 
+    'koehsel.theme',
+]
 
-    if context.readDataFile('koehsel.theme_various.txt') is None:
+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:
         return
 
-    # Add additional setup code here
+    site = context.getSite()
+    qi = getToolByName(site, 'portal_quickinstaller')
+    wf = getToolByName(site, 'portal_workflow')    
+
+    # Install EasyShop product
+
+    for p in INSTALL_PRODUCTS:
+        if not qi.isProductInstalled(p):
+            qi.installProduct(p)
+
+    # Add and publish shop instance
+
+    try: site.shop
+    except AttributeError:
+        makePublicObject(container=site, type_name='EasyShop', 
+                         id='shop', title='Shop', shopOwner='admin')
+
+    # Hide Users, News and Events
+
+    for id in ('Members', 'news', 'events'):
+        ob = getattr(site,id)
+        if wf.getInfoFor(ob,'review_state','') != 'private':
+            wf.doActionFor(ob,'retract',comment='site setup')
+
+
+def contentSetup(app):
+    app.manage_addProduct['CMFPlone'].addPloneSite(id='site',
+                                                   extension_ids=['koehsel.policy:default'])
+
+
+# Called via hackery in __init__.py on every site start
+def appInit(app):
+    try: app.site
+    except AttributeError:
+
+        from Testing.makerequest import makerequest
+        from AccessControl.SecurityManagement import newSecurityManager
+        import transaction, AccessControl
+
+        # Call 'contentSetup' as SITE_OWNER user with a valid REQUEST
+        req = makerequest(app)
+        newSecurityManager(req.REQUEST, app.acl_users.getUser(SITE_OWNER).__of__(app.acl_users))
+        contentSetup(req)
+        transaction.commit()
+        newSecurityManager(None, AccessControl.User.system)