Esempio n. 1
0
    def decorator(*args, **kwargs):
        from pony.orm import db_session
        from pony.orm.core import local
        from flask import after_this_request, current_app, has_app_context, \
            has_request_context
        from flask.signals import appcontext_popped

        register = local.db_context_counter == 0
        if register and (has_app_context() or has_request_context()):
            db_session.__enter__()

        result = f(*args, **kwargs)

        if register:
            if has_request_context():
                @after_this_request
                def pop(request):
                    db_session.__exit__()
                    return request
            elif has_app_context():
                @appcontext_popped.connect_via(
                    current_app._get_current_object()
                )
                def pop(sender, *args, **kwargs):
                    while local.db_context_counter:
                        db_session.__exit__()
            else:
                raise RuntimeError('Needs app or request context')
        return result
Esempio n. 2
0
    def decorator(*args, **kwargs):
        from pony.orm import db_session
        from pony.orm.core import local
        from flask import (
            after_this_request,
            current_app,
            has_app_context,
            has_request_context,
        )
        from flask.signals import appcontext_popped

        register = local.db_context_counter == 0
        if register and (has_app_context() or has_request_context()):
            db_session.__enter__()

        result = f(*args, **kwargs)

        if register:
            if has_request_context():

                @after_this_request
                def pop(request):
                    db_session.__exit__()
                    return request

            elif has_app_context():

                @appcontext_popped.connect_via(current_app._get_current_object())
                def pop(sender, *args, **kwargs):
                    while local.db_context_counter:
                        db_session.__exit__()

            else:
                raise RuntimeError("Needs app or request context")
        return result
Esempio n. 3
0
def start_db_session():
    """Starts a new db_session if it does not exists"""
    # print('==> Start session')

    if has_db_session():
        return

    if not has_app_context() or not has_request_context():
        raise RuntimeError('You need app_context or request_context')

    db_session.__enter__()
Esempio n. 4
0
def start_db_session():
    """Starts a new db_session if it does not exists"""
    # print('==> Start session')

    if has_db_session():
        return

    if not has_app_context() or not has_request_context():
        raise RuntimeError('You need app_context or request_context')

    db_session.__enter__()
Esempio n. 5
0
def ddb(ddbr):
    """database reset with ddb_sesion"""
    db_session.__enter__()
    yield ddbr
    db_session.__exit__()
Esempio n. 6
0
 def on_request(self):
     db_session.__enter__()