Private GIT

Skip to content
Snippets Groups Projects
Commit 261f6be7 authored by Ruud's avatar Ruud
Browse files

Setting global

parent cdde9865
No related branches found
No related tags found
No related merge requests found
......@@ -16,12 +16,14 @@ app = Flask(__name__)
log = CPLog(__name__)
web = Module(__name__, 'web')
def get_session(engine):
engine = engine if engine else get_engine()
return scoped_session(sessionmaker(autoflush = True, transactional = True, bind = engine))
return scoped_session(sessionmaker(transactional = True, bind = engine))
def get_engine():
return create_engine('sqlite:///' + Env.get('db_path'), echo = Env.get('debug'))
return create_engine('sqlite:///' + Env.get('db_path'), echo = False)
@web.route('/')
@requires_auth
......
......@@ -28,8 +28,7 @@ def setting_save_view():
option = a.get('name')
value = a.get('value')
Env.get('settings').set(section, option, value)
Env.get('settings').save()
Env.setting(option, section, value).save()
return jsonify({
'success': True,
......
......@@ -48,9 +48,12 @@ def cmd_couchpotato(base_path, args):
Env.set('app_dir', base_path)
Env.set('data_dir', options.data_dir)
Env.set('db_path', os.path.join(options.data_dir, 'couchpotato.db'))
Env.set('quiet', options.quiet)
Env.set('daemonize', options.daemonize)
Env.set('args', args)
# Determine debug
debug = options.debug or Env.get('settings').get('debug', default = False)
debug = options.debug or Env.setting('debug', default = False)
Env.set('debug', debug)
......@@ -96,13 +99,13 @@ def cmd_couchpotato(base_path, args):
# Create app
from couchpotato import app
api_key = Env.get('settings').get('api_key')
url_base = '/' + Env.get('settings').get('url_base') if Env.get('settings').get('url_base') else ''
api_key = Env.setting('api_key')
url_base = '/' + Env.setting('url_base') if Env.setting('url_base') else ''
reloader = debug and not options.daemonize
# Basic config
app.host = Env.get('settings').get('host', default = '0.0.0.0')
app.port = Env.get('settings').get('port', default = 5000)
app.host = Env.setting('host', default = '0.0.0.0')
app.port = Env.setting('port', default = 5000)
app.debug = debug
app.secret_key = api_key
app.static_path = url_base + '/static'
......
......@@ -3,7 +3,7 @@ from flask import request, Response
from functools import wraps
def check_auth(username, password):
return username == Env.get('settings').get('username') and password == Env.get('settings').get('password')
return username == Env.setting('username') and password == Env.setting('password')
def authenticate():
return Response(
......@@ -16,7 +16,7 @@ def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if Env.get('settings').get('username') and (not auth or not check_auth(auth.username, auth.password)):
if Env.setting('username') and (not auth or not check_auth(auth.username, auth.password)):
return authenticate()
return f(*args, **kwargs)
......
from couchpotato.core.settings import Settings
class Env:
''' Environment variables '''
_debug = False
_settings = Settings()
_options = None
_args = None
_quiet = False
_deamonize = False
''' Data paths and directories '''
_app_dir = ""
_data_dir = ""
_db_path = ""
......@@ -22,3 +26,15 @@ class Env:
@staticmethod
def set(attr, value):
return setattr(Env, '_' + attr, value)
@staticmethod
def setting(attr, section = 'global', value = None, default = ''):
# Return setting
if value == None:
return Env.get('settings').get(attr, default = default, section = section)
# Set setting
s = Env.get('settings')
s.set(section, attr, value)
return s
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment