pyramid_cubicweb/__init__.py
author Christophe de Vienne <christophe@unlish.com>
Thu, 06 Nov 2014 22:08:57 +0100
changeset 11522 568204930c85
parent 11511 13e0f569684c
child 11532 6a1d0aa3ac85
permissions -rw-r--r--
Provides a full wsgi cubicweb application builder
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11503
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
     1
import os
11511
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
     2
import wsgicors
11503
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
     3
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
     4
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
11501
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     5
from pyramid.config import Configurator
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     6
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     7
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     8
def make_cubicweb_application(cwconfig):
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     9
    """
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    10
    Create a pyramid-based CubicWeb instance from a cubicweb configuration.
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    11
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    12
    It is initialy meant to be used by the 'pyramid' command of cubicweb-ctl.
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    13
    """
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    14
    settings = {
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    15
        'session.secret': '11',  # XXX
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    16
    }
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    17
    if cwconfig.debugmode:
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    18
        settings.update({
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    19
            'pyramid.debug_authorization': True,
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    20
            'pyramid.debug_notfound': True,
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    21
            'pyramid.debug_routematch': True,
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    22
        })
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    23
    config = Configurator(settings=settings)
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    24
    if cwconfig.debugmode:
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    25
        config.include('pyramid_debugtoolbar')
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    26
    config.registry['cubicweb.config'] = cwconfig
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    27
    config.registry['cubicweb.repository'] = repo = cwconfig.repository()
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    28
    config.registry['cubicweb.registry'] = repo.vreg
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    29
    config.include('pyramid_cubicweb.defaults')
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    30
    config.include('pyramid_cubicweb.core')
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    31
    config.include('pyramid_cubicweb.bwcompat')
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    32
    return config
11503
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    33
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    34
11522
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    35
def wsgi_application_from_cwconfig(cwconfig):
11503
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    36
    config = make_cubicweb_application(cwconfig)
11511
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    37
    app = config.make_wsgi_app()
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    38
    # This replaces completely web/cors.py, which is not used by
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    39
    # pyramid_cubicweb anymore
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    40
    app = wsgicors.CORS(
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    41
        app,
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    42
        origin=cwconfig['access-control-allow-origin'],
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    43
        headers=cwconfig['access-control-allow-headers'],
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    44
        methods=cwconfig['access-control-allow-methods'],
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    45
        credentials='true')
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    46
    return app
11522
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    47
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    48
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    49
def wsgi_application(instance_name=None, debug=None):
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    50
    if instance_name is None:
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    51
        instance_name = os.environ.get('CW_INSTANCE')
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    52
    if debug is None:
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    53
        debug = 'CW_DEBUG' in os.environ
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    54
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    55
    cwconfig = cwcfg.config_for(instance_name, debugmode=debug)
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    56
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
    57
    return wsgi_application_from_cwconfig(cwconfig)