71cd55894273da2655fb6b41c4206331d0d94a21
[zope-bootstrap.git] / src / koehsel.policy / koehsel / policy / setuphandlers.py
1 from Products.CMFCore.utils import getToolByName
2 from Acquisition import aq_base
3 from zope import event
4 from Products.Archetypes.event import ObjectInitializedEvent
5
6 INSTALL_PRODUCTS = [
7     'easyshop.core', 
8     'koehsel.theme',
9 ]
10
11 SITE_OWNER = 'admin'
12
13 def makePublicObject(container, type_name, id, **kw):
14     """Create an object in CONTAINER with type TYPE_NAME and id ID. Additional keyword args
15 specify additional factory arguments. After creation, the object is published if needed."""
16     id = container.invokeFactory(id=id, type_name=type_name, **kw)
17     ob = getattr(container,id)
18     event.notify(ObjectInitializedEvent(ob))
19     ob.at_post_create_script()
20 #    if hasattr(aq_base(ob), 'manage_afterPortalFactoryCreate'):
21 #        ob.manage_afterPortalFactoryCreate()
22     wf = getToolByName(container, 'portal_workflow')
23     if wf.getInfoFor(ob,'review_state','') != 'published':
24         wf.doActionFor(ob,'publish',comment='site setup')
25
26
27 def siteSetup(context):
28
29     if context.readDataFile('koehsel.policy_setup.txt') is None:
30         return
31
32     site = context.getSite()
33     qi = getToolByName(site, 'portal_quickinstaller')
34     wf = getToolByName(site, 'portal_workflow')    
35
36     # Install EasyShop product
37
38     for p in INSTALL_PRODUCTS:
39         if not qi.isProductInstalled(p):
40             qi.installProduct(p)
41
42     # Add and publish shop instance
43
44     try: site.shop
45     except AttributeError:
46         makePublicObject(container=site, type_name='EasyShop', 
47                          id='shop', title='Shop', shopOwner='admin')
48
49     # Hide Users, News and Events
50
51     for id in ('Members', 'news', 'events'):
52         ob = getattr(site,id)
53         if wf.getInfoFor(ob,'review_state','') != 'private':
54             wf.doActionFor(ob,'retract',comment='site setup')
55
56
57 def contentSetup(app):
58     app.manage_addProduct['CMFPlone'].addPloneSite(id='site',
59                                                    extension_ids=['koehsel.policy:default'])
60
61
62 # Called via hackery in __init__.py on every site start
63 def appInit(app):
64     try: app.site
65     except AttributeError:
66
67         from Testing.makerequest import makerequest
68         from AccessControl.SecurityManagement import newSecurityManager
69         import transaction, AccessControl
70
71         # Call 'contentSetup' as SITE_OWNER user with a valid REQUEST
72         req = makerequest(app)
73         newSecurityManager(req.REQUEST, app.acl_users.getUser(SITE_OWNER).__of__(app.acl_users))
74         contentSetup(req)
75         transaction.commit()
76         newSecurityManager(None, AccessControl.User.system)