Skip to content

Commit

Permalink
Rename ApplicationInitializedEvent to ApplicationAddedEvent.
Browse files Browse the repository at this point in the history
When the IApplicationAddedEvent is triggered the new application
will be current Zope local site. The site is restored after the
event.
  • Loading branch information
thefunny42 committed Jun 4, 2015
1 parent 753ab59 commit 743efb5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Expand Up @@ -8,6 +8,12 @@ Changes
directive let you customize when (namely the event) to install the
configured local sites.

- Rename ``ApplicationInitializedEvent`` to ``ApplicationAddedEvent``.

- When the ``IApplicationAddedEvent`` is triggered the new application
will be current Zope local site. The site is restored after the
event.


1.6.1 (2012-05-02)
------------------
Expand Down
4 changes: 2 additions & 2 deletions src/grokcore/site/__init__.py
Expand Up @@ -21,8 +21,8 @@
import grokcore.site.testing

from grokcore.site.interfaces import IApplication
from grokcore.site.interfaces import IApplicationInitializedEvent
from grokcore.site.interfaces import ApplicationInitializedEvent
from grokcore.site.interfaces import IApplicationAddedEvent
from grokcore.site.interfaces import ApplicationAddedEvent

from grokcore.site.interfaces import IGrokcoreSiteAPI
__all__ = list(IGrokcoreSiteAPI)
9 changes: 9 additions & 0 deletions src/grokcore/site/ftests/application/application.py
Expand Up @@ -12,6 +12,7 @@
>>> import grokcore.site
>>> import zope.site.hooks
>>> root = getRootFolder()
>>> app = grokcore.site.util.create_application(Cave, root, 'mycave')
>>> root['cave'] = app
>>> zope.site.hooks.setSite(app)
Expand All @@ -37,6 +38,14 @@
>>> grokcore.site.getApplication()
<grokcore.site.ftests.application.application.Cave object at ...>
If you try to create an application that is not valid it will fail:
>>> grokcore.site.util.create_application(object(), root, 'myobject')
Traceback (most recent call last):
...
WrongType: ...
"""
import grokcore.content
import grokcore.site
Expand Down
17 changes: 8 additions & 9 deletions src/grokcore/site/interfaces.py
Expand Up @@ -22,19 +22,18 @@ class IApplication(Interface):
"""


class IApplicationInitializedEvent(IObjectEvent):
"""A Grok Application has been created with success and is now ready
to be used.
class IApplicationAddedEvent(IObjectEvent):
"""A Grok Application has been added with success.
This event can be used to trigger the creation of contents or other tasks
that require the application to be fully operational : utilities installed
that require the application to be fully there : utilities installed
and indexes created in the catalog."""


class ApplicationInitializedEvent(object):
"""A Grok Application has been created and is now ready to be used.
class ApplicationAddedEvent(object):
"""A Grok Application has been added.
"""
implements(IApplicationInitializedEvent)
implements(IApplicationAddedEvent)

def __init__(self, app):
assert IApplication.providedBy(app)
Expand Down Expand Up @@ -90,10 +89,10 @@ class IGrokcoreSiteAPI(IGrokcoreComponentAPI, IBaseClasses, IDirectives):

IApplication = Attribute('The application model interface')

IApplicationInitializedEvent = Attribute(
IApplicationAddedEvent = Attribute(
'The application initialized event interface')

ApplicationInitializedEvent = Attribute(
ApplicationAddedEvent = Attribute(
'The application initialized event factory')

def getSite():
Expand Down
19 changes: 13 additions & 6 deletions src/grokcore/site/util.py
Expand Up @@ -12,11 +12,11 @@
#
##############################################################################

import grokcore.site
from grokcore.site.interfaces import IApplication, ApplicationInitializedEvent
from zope.component.hooks import getSite
from grokcore.site.interfaces import IApplication, ApplicationAddedEvent
from zope.component.hooks import getSite, setSite
from zope.event import notify
from zope.lifecycleevent import ObjectCreatedEvent
from zope.schema.interfaces import WrongType


def getApplication():
Expand All @@ -42,7 +42,8 @@ def create_application(factory, container, name):
the application lifecycle.
"""
# Check the factory.
assert IApplication.implementedBy(factory)
if not IApplication.implementedBy(factory):
raise WrongType(factory)

# Check the availability of the name in the container.
if name in container:
Expand All @@ -58,7 +59,13 @@ def create_application(factory, container, name):
# This may raise a KeyError.
container[name] = application

# Trigger the initialization event.
notify(ApplicationInitializedEvent(application))
# Trigger the initialization event with the new application as a
# current site.
current = getSite()
setSite(application)
try:
notify(ApplicationAddedEvent(application))
finally:
setSite(current)

return application

0 comments on commit 743efb5

Please sign in to comment.