Fix config changing crash

This commit is contained in:
notangelmario 2021-04-22 11:33:09 +03:00
parent e2380a5c74
commit b1a1bd6c31

View File

@ -238,40 +238,41 @@ def search():
@app.route('/config', methods=['GET', 'POST', 'PUT']) @app.route('/config', methods=['GET', 'POST', 'PUT'])
@auth_required @auth_required
def config(): def config():
if os.getenv('WHOOGLE_CONFIG_DISABLE_CHANGE', '') != '': if request.method == 'GET':
if request.method == 'GET': return json.dumps(g.user_config.__dict__)
return json.dumps(g.user_config.__dict__) elif request.method == 'PUT' and os.getenv('WHOOGLE_CONFIG_DISABLE_CHANGE', '') == '':
elif request.method == 'PUT': if 'name' in request.args:
if 'name' in request.args: config_pkl = os.path.join(
config_pkl = os.path.join( app.config['CONFIG_PATH'],
app.config['CONFIG_PATH'], request.args.get('name'))
request.args.get('name')) session['config'] = (pickle.load(open(config_pkl, 'rb'))
session['config'] = (pickle.load(open(config_pkl, 'rb')) if os.path.exists(config_pkl)
if os.path.exists(config_pkl) else session['config'])
else session['config']) return json.dumps(session['config'])
return json.dumps(session['config'])
else:
return json.dumps({})
else: else:
config_data = request.form.to_dict() return json.dumps({})
if 'url' not in config_data or not config_data['url']: elif os.getenv('WHOOGLE_CONFIG_DISABLE_CHANGE', '') == '':
config_data['url'] = g.user_config.url config_data = request.form.to_dict()
if 'url' not in config_data or not config_data['url']:
config_data['url'] = g.user_config.url
# Save config by name to allow a user to easily load later # Save config by name to allow a user to easily load later
if 'name' in request.args: if 'name' in request.args:
pickle.dump( pickle.dump(
config_data, config_data,
open(os.path.join( open(os.path.join(
app.config['CONFIG_PATH'], app.config['CONFIG_PATH'],
request.args.get('name')), 'wb')) request.args.get('name')), 'wb'))
# Overwrite default config if user has cookies disabled # Overwrite default config if user has cookies disabled
if g.cookies_disabled: if g.cookies_disabled:
open(app.config['DEFAULT_CONFIG'], 'w').write( open(app.config['DEFAULT_CONFIG'], 'w').write(
json.dumps(config_data, indent=4)) json.dumps(config_data, indent=4))
session['config'] = config_data session['config'] = config_data
return redirect(config_data['url']) return redirect(config_data['url'])
else:
return json.dumps({})
@app.route('/url', methods=['GET']) @app.route('/url', methods=['GET'])