pyramid_cubicweb/__init__.py
author Christophe de Vienne <christophe@unlish.com>
Fri, 19 Sep 2014 19:17:50 +0200
changeset 11511 13e0f569684c
parent 11503 ddf61aa73384
child 11522 568204930c85
permissions -rw-r--r--
Use 'wsgicors' for CORS handling. The CW CORS handling (in web/cors.py) is only able to work on cubicweb requests. When a request is not handled by bwcompat, we need a proper solution. The `wsgicors` library provides what we need as a wsgi middleware.
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
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    35
def wsgi_application(instance_name=None, debug=None):
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    36
    if instance_name is None:
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    37
        instance_name = os.environ.get('CW_INSTANCE')
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    38
    if debug is None:
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    39
        debug = 'CW_DEBUG' in os.environ
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    40
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    41
    cwconfig = cwcfg.config_for(instance_name, debugmode=debug)
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    42
    config = make_cubicweb_application(cwconfig)
11511
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    43
    app = config.make_wsgi_app()
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    44
    # 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
    45
    # pyramid_cubicweb anymore
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    46
    app = wsgicors.CORS(
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    47
        app,
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    48
        origin=cwconfig['access-control-allow-origin'],
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    49
        headers=cwconfig['access-control-allow-headers'],
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    50
        methods=cwconfig['access-control-allow-methods'],
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    51
        credentials='true')
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    52
    return app