Beispiel #1
0
def add_session_to_request(event):
    event.request.time = time.time()
    event.request.now = time.time
    event.request.db = Session()
    event.request.queries = 0
    # The listener is not yet deleted after the request completes,
    # as this is not implemented in sqlalchemy. Therefore, the
    # closure would contain a reference to the event as long
    # as the connection to the database is active, which
    # creates a memory leak. As long as the listener cannot be
    # removed, we have to use a weak reference.
    weak_event = weakref.ref(event)

    def before_execute(conn, clauseelement, multiparams, params):
        wevent = weak_event()
        if wevent:
            wevent.request.queries += 1

    saevent.listen(event.request.db.get_bind(), "before_execute",
                   before_execute)

    def callback(request):
        request.db.rollback()

    event.request.add_finished_callback(callback)

    user_id = security.authenticated_userid(event.request)
    if user_id is not None:
        event.request.user = event.request.db.query(User).get(user_id)
    else:
        event.request.user = None
    event.request.userInfo = utils.UserInfo(event.request.user)
    event.request.permissionInfo = utils.PermissionInfo(event.request)
Beispiel #2
0
def add_request_attributes(event):
    # Add database session
    event.request.db = event.request.registry.db_maker()

    def callback(request):
        request.db.rollback()

    event.request.add_finished_callback(callback)

    # Add user objects
    event.request.user = None
    if event.request.identity is not None:
        event.request.user = event.request.identity['user']
    event.request.userInfo = utils.UserInfo(event.request.user)
    event.request.permissionInfo = utils.PermissionInfo(event.request)

    # Add Javascript and CSS
    event.request.javascript = list()
    event.request.css = list()

    # Add config
    event.request.config = muesli.config