예제 #1
0
파일: site.py 프로젝트: Py-AMS/pyams-site
def site_factory(request):
    """Application site factory

    On application startup, this factory checks configuration to get application name and
    load it from the ZODB; if the application can't be found, configuration is scanned to
    get application factory, create a new one and create a local site manager.
    """
    conn = get_connection(request)
    root = conn.root()
    application_key = request.registry.settings.get(PYAMS_APPLICATION_SETTINGS_KEY,
                                                    PYAMS_APPLICATION_DEFAULT_NAME)
    application = root.get(application_key)
    if application is None:
        factory = request.registry.settings.get(PYAMS_APPLICATION_FACTORY_KEY)
        if factory:
            resolver = DottedNameResolver()
            factory = resolver.maybe_resolve(factory)
        else:
            factory = request.registry.queryUtility(ISiteRootFactory, default=BaseSiteRoot)
        application = root[application_key] = factory()
        if IPossibleSite.providedBy(application):
            lsm = LocalSiteManager(application, default_folder=False)
            application.setSiteManager(lsm)
        try:
            # if some components require a valid and complete registry
            # with all registered utilities, they can subscribe to
            # INewLocalSiteCreatedEvent event interface
            set_local_registry(application.getSiteManager())
            get_current_registry().notify(NewLocalSiteCreatedEvent(application))
        finally:
            set_local_registry(None)
        import transaction  # pylint: disable=import-outside-toplevel
        transaction.commit()
    return application
예제 #2
0
def importComponentRegistry(context):
    """Import local components.
    """
    site = context.getSite()
    sm = None
    if IPossibleSite.providedBy(site):
        # All object managers are an IPossibleSite, but this
        # defines the getSiteManager method to be available
        try:
            sm = site.getSiteManager()
        except ComponentLookupError:
            sm = None

    if sm is None or not IComponentRegistry.providedBy(sm):
        logger = context.getLogger('componentregistry')
        logger.info("Can not register components, as no registry was found.")
        return

    importer = queryMultiAdapter((sm, context), IBody)
    if importer:
        body = context.readDataFile('componentregistry.xml')
        if body is not None:
            importer.body = body
        else:
            logger = context.getLogger('componentregistry')
            logger.debug("Nothing to import")
def importComponentRegistry(context):
    """Import local components.
    """
    site = context.getSite()
    sm = None
    if IPossibleSite.providedBy(site):
        # All object managers are an IPossibleSite, but this
        # defines the getSiteManager method to be available
        try:
            sm = site.getSiteManager()
        except ComponentLookupError:
            sm = None

    if sm is None or not IComponentRegistry.providedBy(sm):
        logger = context.getLogger('componentregistry')
        logger.info("Can not register components, as no registry was found.")
        return

    importer = queryMultiAdapter((sm, context), IBody)
    if importer:
        body = context.readDataFile('componentregistry.xml')
        if body is not None:
            importer.body = body
        else:
            logger = context.getLogger('componentregistry')
            logger.debug("Nothing to import")
예제 #4
0
파일: utils.py 프로젝트: novareto/ul.zodb
 def create_app(db):
     conn = db.open()
     try:
         root = conn.root()
         if not name in root:
             with transaction_manager:
                 app = root[name] = model()
                 if (not ISite.providedBy(app) and
                         IPossibleSite.providedBy(app)):
                     LocalSiteManager(app)
                 notify(events.ApplicationInitializedEvent(app))
     finally:
         conn.close()
예제 #5
0
 def create_app(db):
     conn = db.open()
     try:
         root = conn.root()
         if not name in root:
             with transaction_manager:
                 app = root[name] = model()
                 if (not ISite.providedBy(app)
                         and IPossibleSite.providedBy(app)):
                     LocalSiteManager(app)
                 notify(events.ApplicationInitializedEvent(app))
     finally:
         conn.close()
예제 #6
0
def enableSite(obj, iface=ISite):
    """Install __before_traverse__ hook for Local Site
    """
    # We want the original object, not stuff in between, and no acquisition
    obj = aq_base(obj)
    if not IPossibleSite.providedBy(obj):
        raise TypeError('Must provide IPossibleSite')
    hook = NameCaller(HOOK_NAME)
    registerBeforeTraverse(obj, hook, HOOK_NAME, 1)

    if not hasattr(obj, HOOK_NAME):
        setattr(obj, HOOK_NAME, LocalSiteHook())

    zope.interface.alsoProvides(obj, iface)
예제 #7
0
def exportComponentRegistry(context):
    """Export local components.
    """
    site = context.getSite()
    sm = None
    if IPossibleSite.providedBy(site):
        # All object managers are an IPossibleSite, but this
        # defines the getSiteManager method to be available
        try:
            sm = site.getSiteManager()
        except ComponentLookupError:
            sm = None

    if sm is None or not IComponentRegistry.providedBy(sm):
        logger = context.getLogger('componentregistry')
        logger.debug("Nothing to export.")
        return

    exporter = queryMultiAdapter((sm, context), IBody)
    if exporter:
        body = exporter.body
        if body is not None:
            context.writeDataFile('componentregistry.xml', body,
                                  exporter.mime_type)
예제 #8
0
def initialize_applications(db, list_applications=list_applications,
                            transaction_manager=transaction_manager):
    """
    This utility setup applications in a database.

    The list of application to create is given by py:fun:list_applications

    For each application to create:

    - it creates the application thanks to the factory
    - it eventually makes it a LocalSiteManager

    You may use it as the :py:fun:init_db initializer parameter,
    or cook your own.

    :param db: the ZODB database
    :param list_applications: a callable that return a dict of applications
        factory indexed by their name
    :param transaction_manager: if not provided
         use :py:mod:transaction default one
    """
    conn = db.open()

    try:
        root = conn.root()
        apps = list_applications()

        with transaction_manager:
            for name, factory in apps.items():
                if not name in root:
                    application = root[name] = factory()
                    if (not ISite.providedBy(application) and
                        IPossibleSite.providedBy(application)):
                        LocalSiteManager(application)
    finally:
        conn.close()
def exportComponentRegistry(context):
    """Export local components.
    """
    site = context.getSite()
    sm = None
    if IPossibleSite.providedBy(site):
        # All object managers are an IPossibleSite, but this
        # defines the getSiteManager method to be available
        try:
            sm = site.getSiteManager()
        except ComponentLookupError:
            sm = None

    if sm is None or not IComponentRegistry.providedBy(sm):
        logger = context.getLogger('componentregistry')
        logger.debug("Nothing to export.")
        return

    exporter = queryMultiAdapter((sm, context), IBody)
    if exporter:
        body = exporter.body
        if body is not None:
            context.writeDataFile('componentregistry.xml', body,
                                  exporter.mime_type)