Back to index

plone3  3.1.7
events.py
Go to the documentation of this file.
00001 from zope.component import adapter
00002 from zope.annotation.interfaces import IAnnotatable
00003 
00004 from Acquisition import aq_parent
00005 
00006 from Products.GenericSetup.interfaces import IBeforeProfileImportEvent
00007 from Products.GenericSetup.interfaces import IProfileImportedEvent
00008 from Products.CMFCore.utils import getToolByName
00009 
00010 
00011 def findProductForProfile(context, profile_id):
00012     qi = getToolByName(context, "portal_quickinstaller", None)
00013     if qi is None:
00014         return None
00015 
00016     if profile_id.startswith("profile-"):
00017         profile_id = profile_id[8:]
00018 
00019     for product in qi.listInstallableProducts(skipInstalled=False):
00020         profiles = qi.getInstallProfiles(product["id"])
00021         if profile_id in profiles:
00022             return product["id"]
00023 
00024     return None
00025 
00026 
00027 @adapter(IBeforeProfileImportEvent)
00028 def handleBeforeProfileImportEvent(event):
00029     if event.profile_id is None or not event.full_import:
00030         return
00031 
00032     context=event.tool
00033 
00034     # We need a request to scribble some data in
00035     request = getattr(context, "REQUEST", None)
00036     if request is None:
00037         return
00038 
00039     product = findProductForProfile(context, event.profile_id)
00040     if product is None:
00041         return
00042 
00043     qi = getToolByName(context, "portal_quickinstaller", None)
00044     if qi is None:
00045         return
00046 
00047     snapshot = qi.snapshotPortal(aq_parent(context))
00048 
00049     storage = IAnnotatable(request, None)
00050     if storage is None:
00051         return
00052 
00053     installing = storage.get("Products.CMFQuickInstaller.Installing", [])
00054     if product in installing:
00055         return
00056 
00057     if storage.has_key("Products.CMFQuickInstallerTool.Events"):
00058         data = storage["Products.CMFQuickInstallerTool.Events"]
00059     else:
00060         data = storage["Products.CMFQuickInstallerTool.Events"] = {}
00061     data[event.profile_id] = dict(product=product, snapshot=snapshot)
00062 
00063 
00064 @adapter(IProfileImportedEvent)
00065 def handleProfileImportedEvent(event):
00066     if event.profile_id is None or not event.full_import:
00067         return
00068 
00069     context = event.tool
00070 
00071     # We need a request to scribble some data in
00072     request = getattr(context, "REQUEST", None)
00073     if request is None:
00074         return
00075 
00076     storage = IAnnotatable(request, None)
00077     if storage is None:
00078         return
00079 
00080     data = storage.get("Products.CMFQuickInstallerTool.Events", [])
00081     if event.profile_id not in data:
00082         return
00083     info = data[event.profile_id]
00084 
00085     qi = getToolByName(context, "portal_quickinstaller", None)
00086     if qi is None:
00087         return
00088 
00089     after = qi.snapshotPortal(aq_parent(context))
00090 
00091     settings = qi.deriveSettingsFromSnapshots(info["snapshot"], after)
00092     version = qi.getProductVersion(info["product"])
00093     qi.notifyInstalled(
00094             info["product"],
00095             locked=False,
00096             logmsg="Installed via setup tool",
00097             settings=settings,
00098             installedversion=version,
00099             status='installed',
00100             error=False)