Put db in environment

This commit is contained in:
Ruud
2012-05-07 20:51:25 +02:00
parent 8832f692ac
commit cdaa2c03e4
3 changed files with 23 additions and 7 deletions
+1 -5
View File
@@ -24,11 +24,7 @@ web = Blueprint('web', __name__)
def get_session(engine = None):
engine = engine if engine else get_engine()
return scoped_session(sessionmaker(bind = engine))
def get_engine():
return create_engine(Env.get('db_path') + '?check_same_thread=False', echo = False)
return Env.getSession(engine)
def addView(route, func, static = False):
web.add_url_rule(route + ('' if static else '/'), endpoint = route if route else 'index', view_func = func)
+2 -2
View File
@@ -238,9 +238,9 @@ class Properties(Entity):
def setup():
"""Setup the database and create the tables that don't exists yet"""
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()
create_all(engine)
+20
View File
@@ -1,6 +1,9 @@
from couchpotato.core.event import fireEvent, addEvent
from couchpotato.core.loader import Loader
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
class Env(object):
@@ -20,6 +23,7 @@ class Env(object):
_quiet = False
_deamonize = False
_desktop = None
_session = None
''' Data paths and directories '''
_app_dir = ""
@@ -48,6 +52,22 @@ class Env(object):
def set(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
def setting(attr, section = 'core', value = None, default = '', type = None):