Example #1
0
def getApplication():
    """Return the nearest enclosing :class:`grokcore.site.Application`.

    Raises :exc:`ValueError` if no application can be found.
    """
    site = getSite()
    if IApplication.providedBy(site):
        return site
    # Another sub-site is within the application. Walk up the object
    # tree until we get to the an application.
    obj = site
    while obj is not None:
        if IApplication.providedBy(obj):
            return obj
        obj = obj.__parent__
    raise ValueError("No application found.")
Example #2
0
def create_application(factory, container, name):
    """Creates an application and triggers the events from
    the application lifecycle.
    """
    # Check the factory.
    assert IApplication.implementedBy(factory)

    # Check the availability of the name in the container.
    if name in container:
        raise KeyError(name)

    # Instanciate the application
    application = factory()

    # Trigger the creation event.
    notify(ObjectCreatedEvent(application))

    # Persist the application.
    # This may raise a KeyError.
    container[name] = application

    # Trigger the initialization event.
    notify(ApplicationInitializedEvent(application))

    return application
Example #3
0
def create_application(factory, container, name):
    """Creates an application and triggers the events from
    the application lifecycle.
    """
    # Check the factory.
    if not IApplication.implementedBy(factory):
        raise WrongType(factory)

    # Check the availability of the name in the container.
    if name in container:
        raise KeyError(name)

    # Instanciate the application
    application = factory()

    # Trigger the creation event.
    notify(ObjectCreatedEvent(application))

    # Persist the application.
    # This may raise a KeyError.
    container[name] = 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