Setting global
This commit is contained in:
@@ -16,12 +16,14 @@ app = Flask(__name__)
|
|||||||
log = CPLog(__name__)
|
log = CPLog(__name__)
|
||||||
web = Module(__name__, 'web')
|
web = Module(__name__, 'web')
|
||||||
|
|
||||||
|
|
||||||
def get_session(engine):
|
def get_session(engine):
|
||||||
engine = engine if engine else get_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():
|
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('/')
|
@web.route('/')
|
||||||
@requires_auth
|
@requires_auth
|
||||||
|
|||||||
@@ -28,8 +28,7 @@ def setting_save_view():
|
|||||||
option = a.get('name')
|
option = a.get('name')
|
||||||
value = a.get('value')
|
value = a.get('value')
|
||||||
|
|
||||||
Env.get('settings').set(section, option, value)
|
Env.setting(option, section, value).save()
|
||||||
Env.get('settings').save()
|
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': True,
|
'success': True,
|
||||||
|
|||||||
+8
-5
@@ -48,9 +48,12 @@ def cmd_couchpotato(base_path, args):
|
|||||||
Env.set('app_dir', base_path)
|
Env.set('app_dir', base_path)
|
||||||
Env.set('data_dir', options.data_dir)
|
Env.set('data_dir', options.data_dir)
|
||||||
Env.set('db_path', os.path.join(options.data_dir, 'couchpotato.db'))
|
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
|
# 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)
|
Env.set('debug', debug)
|
||||||
|
|
||||||
|
|
||||||
@@ -96,13 +99,13 @@ def cmd_couchpotato(base_path, args):
|
|||||||
|
|
||||||
# Create app
|
# Create app
|
||||||
from couchpotato import app
|
from couchpotato import app
|
||||||
api_key = Env.get('settings').get('api_key')
|
api_key = Env.setting('api_key')
|
||||||
url_base = '/' + Env.get('settings').get('url_base') if Env.get('settings').get('url_base') else ''
|
url_base = '/' + Env.setting('url_base') if Env.setting('url_base') else ''
|
||||||
reloader = debug and not options.daemonize
|
reloader = debug and not options.daemonize
|
||||||
|
|
||||||
# Basic config
|
# Basic config
|
||||||
app.host = Env.get('settings').get('host', default = '0.0.0.0')
|
app.host = Env.setting('host', default = '0.0.0.0')
|
||||||
app.port = Env.get('settings').get('port', default = 5000)
|
app.port = Env.setting('port', default = 5000)
|
||||||
app.debug = debug
|
app.debug = debug
|
||||||
app.secret_key = api_key
|
app.secret_key = api_key
|
||||||
app.static_path = url_base + '/static'
|
app.static_path = url_base + '/static'
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from flask import request, Response
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
def check_auth(username, password):
|
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():
|
def authenticate():
|
||||||
return Response(
|
return Response(
|
||||||
@@ -16,7 +16,7 @@ def requires_auth(f):
|
|||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
auth = request.authorization
|
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 authenticate()
|
||||||
|
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
from couchpotato.core.settings import Settings
|
from couchpotato.core.settings import Settings
|
||||||
|
|
||||||
class Env:
|
class Env:
|
||||||
|
|
||||||
|
''' Environment variables '''
|
||||||
_debug = False
|
_debug = False
|
||||||
_settings = Settings()
|
_settings = Settings()
|
||||||
_options = None
|
_options = None
|
||||||
_args = None
|
_args = None
|
||||||
_quiet = False
|
_quiet = False
|
||||||
|
_deamonize = False
|
||||||
|
|
||||||
|
''' Data paths and directories '''
|
||||||
_app_dir = ""
|
_app_dir = ""
|
||||||
_data_dir = ""
|
_data_dir = ""
|
||||||
_db_path = ""
|
_db_path = ""
|
||||||
@@ -22,3 +26,15 @@ class Env:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def set(attr, value):
|
def set(attr, value):
|
||||||
return setattr(Env, '_' + 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
|
||||||
|
|||||||
Reference in New Issue
Block a user