Put db in environment
This commit is contained in:
@@ -24,11 +24,7 @@ web = Blueprint('web', __name__)
|
|||||||
|
|
||||||
|
|
||||||
def get_session(engine = None):
|
def get_session(engine = None):
|
||||||
engine = engine if engine else get_engine()
|
return Env.getSession(engine)
|
||||||
return scoped_session(sessionmaker(bind = engine))
|
|
||||||
|
|
||||||
def get_engine():
|
|
||||||
return create_engine(Env.get('db_path') + '?check_same_thread=False', echo = False)
|
|
||||||
|
|
||||||
def addView(route, func, static = False):
|
def addView(route, func, static = False):
|
||||||
web.add_url_rule(route + ('' if static else '/'), endpoint = route if route else 'index', view_func = func)
|
web.add_url_rule(route + ('' if static else '/'), endpoint = route if route else 'index', view_func = func)
|
||||||
|
|||||||
@@ -238,9 +238,9 @@ class Properties(Entity):
|
|||||||
def setup():
|
def setup():
|
||||||
"""Setup the database and create the tables that don't exists yet"""
|
"""Setup the database and create the tables that don't exists yet"""
|
||||||
from elixir import setup_all, create_all
|
from elixir import setup_all, create_all
|
||||||
from couchpotato import get_engine
|
from couchpotato.environment import Env
|
||||||
|
|
||||||
engine = get_engine()
|
engine = Env.getEngine()
|
||||||
|
|
||||||
setup_all()
|
setup_all()
|
||||||
create_all(engine)
|
create_all(engine)
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
from couchpotato.core.event import fireEvent, addEvent
|
from couchpotato.core.event import fireEvent, addEvent
|
||||||
from couchpotato.core.loader import Loader
|
from couchpotato.core.loader import Loader
|
||||||
from couchpotato.core.settings import Settings
|
from couchpotato.core.settings import Settings
|
||||||
|
from sqlalchemy.engine import create_engine
|
||||||
|
from sqlalchemy.orm import scoped_session
|
||||||
|
from sqlalchemy.orm.session import sessionmaker
|
||||||
import os
|
import os
|
||||||
|
|
||||||
class Env(object):
|
class Env(object):
|
||||||
@@ -20,6 +23,7 @@ class Env(object):
|
|||||||
_quiet = False
|
_quiet = False
|
||||||
_deamonize = False
|
_deamonize = False
|
||||||
_desktop = None
|
_desktop = None
|
||||||
|
_session = None
|
||||||
|
|
||||||
''' Data paths and directories '''
|
''' Data paths and directories '''
|
||||||
_app_dir = ""
|
_app_dir = ""
|
||||||
@@ -48,6 +52,22 @@ class Env(object):
|
|||||||
def set(attr, value):
|
def set(attr, value):
|
||||||
return setattr(Env, '_' + attr, value)
|
return setattr(Env, '_' + attr, value)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getSession(engine = None):
|
||||||
|
existing_session = Env.get('session')
|
||||||
|
if existing_session:
|
||||||
|
return existing_session
|
||||||
|
|
||||||
|
engine = Env.getEngine()
|
||||||
|
session = scoped_session(sessionmaker(bind = engine))
|
||||||
|
Env.set('session', session)
|
||||||
|
|
||||||
|
return session
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getEngine():
|
||||||
|
return create_engine(Env.get('db_path'), echo = False)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def setting(attr, section = 'core', value = None, default = '', type = None):
|
def setting(attr, section = 'core', value = None, default = '', type = None):
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user