Browse Source

Setting global

pull/1/merge
Ruud 14 years ago
parent
commit
261f6be7e1
  1. 6
      couchpotato/__init__.py
  2. 3
      couchpotato/api/__init__.py
  3. 13
      couchpotato/cli.py
  4. 4
      couchpotato/core/auth.py
  5. 16
      couchpotato/environment.py

6
couchpotato/__init__.py

@ -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

3
couchpotato/api/__init__.py

@ -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,

13
couchpotato/cli.py

@ -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'

4
couchpotato/core/auth.py

@ -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)

16
couchpotato/environment.py

@ -1,12 +1,16 @@
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

Loading…
Cancel
Save