d175a1fc9cd877359aa954fe8e6c710bbd526208
[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     if hasattr(aq_base(ob), 'at_post_create_script'):
20         ob.at_post_create_script()
21     if hasattr(aq_base(ob), 'manage_afterPortalFactoryCreate'):
22         ob.manage_afterPortalFactoryCreate()
23     wf = getToolByName(container, 'portal_workflow')
24     if wf.getInfoFor(ob,'review_state','') != 'published':
25         wf.doActionFor(ob,'publish',comment='site setup')
26
27
28 def siteSetup(context):
29
30     if context.readDataFile('koehsel.policy_setup.txt') is None:
31         return
32
33     site = context.getSite()
34     qi = getToolByName(site, 'portal_quickinstaller')
35     wf = getToolByName(site, 'portal_workflow')    
36
37     # Install EasyShop product
38
39     for p in INSTALL_PRODUCTS:
40         if not qi.isProductInstalled(p):
41             qi.installProduct(p)
42
43     # Add and publish shop instance
44
45     try: site.shop
46     except AttributeError:
47         makePublicObject(container=site, type_name='EasyShop', 
48                          id='shop', title='Shop', shopOwner='admin')
49
50     # Hide Users, News and Events
51
52     for id in ('Members', 'news', 'events'):
53         ob = getattr(site,id)
54         if wf.getInfoFor(ob,'review_state','') != 'private':
55             wf.doActionFor(ob,'retract',comment='site setup')
56
57
58 def contentSetup(app):
59     app.manage_addProduct['CMFPlone'].addPloneSite(id='site',
60                                                    extension_ids=['koehsel.policy:default'])
61
62
63 # Called via hackery in __init__.py on every site start
64 def appInit(app):
65     try: app.site
66     except AttributeError:
67
68         from Testing.makerequest import makerequest
69         from AccessControl.SecurityManagement import newSecurityManager
70         import transaction, AccessControl
71
72         # Call 'contentSetup' as SITE_OWNER user with a valid REQUEST
73         req = makerequest(app)
74         newSecurityManager(req.REQUEST, app.acl_users.getUserById(SITE_OWNER).__of__(app.acl_users))
75         contentSetup(req)
76         transaction.commit()
77         newSecurityManager(None, AccessControl.User.system)