import os
import sys

from cmf.manage import *
install_traceback_email_notify()
APP.config.from_pyfile(cmf.__path__[0] + "/config_load.py")
start_views()
init_ds()
# delme compat patch
if hasattr(CMF_CACHE, 'flushdb'):
    CMF_CACHE.flushdb()
else:
    CMF_CACHE.flushall()

from cmf.include import *
from cmf.data_providers.base import commit_all_ds


def app_context(commit=False):
    def decorator(func):
        def wrapper(*args, **kwargs):
            with APP.test_request_context("/"):
                RAISE_LAZYLOAD = config.RAISE_LAZYLOAD
                config.RAISE_LAZYLOAD = False
                APP.preprocess_request()
                g.current_person = models.CmfPerson.get(id='CmfPerson:00000000-0000-0000-0000-000000000001')
                func(*args, **kwargs)
                if commit:
                    commit_all_ds()
                else:
                    rollback_all_ds()
                config.RAISE_LAZYLOAD = RAISE_LAZYLOAD
        return wrapper
    return decorator
