From 8fa87c0d1f7fd4199acf05e620d26329d38d36bf Mon Sep 17 00:00:00 2001 From: Ben Busby <33362396+benbusby@users.noreply.github.com> Date: Thu, 28 May 2020 19:03:12 -0600 Subject: [PATCH] Added ability to save/load configs by name - New PUT method for config allows changing config with specified name - New methods in js controller to handle loading/saving of configs --- app/routes.py | 14 +++++++++++++- app/static/js/controller.js | 35 +++++++++++++++++++++++++++++++++++ app/templates/index.html | 6 ++++-- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index 43b6d08..26dcd1d 100644 --- a/app/routes.py +++ b/app/routes.py @@ -150,16 +150,28 @@ def search(): mobile=g.user_request.mobile) if 'isch' not in search_util.search_type else '') -@app.route('/config', methods=['GET', 'POST']) +@app.route('/config', methods=['GET', 'POST', 'PUT']) @auth_required def config(): if request.method == 'GET': return json.dumps(g.user_config.__dict__) + elif request.method == 'PUT': + if 'name' in request.args: + config_path = os.path.join(app.config['CONFIG_PATH'], request.args.get('name')) + session['config'] = json.load(open(config_path)) if os.path.exists(config_path) else session['config'] + return json.dumps(session['config']) + else: + return json.dumps({}) else: config_data = request.form.to_dict() if 'url' not in config_data or not config_data['url']: config_data['url'] = g.user_config.url + if 'name' in request.args: + with open(os.path.join(app.config['CONFIG_PATH'], request.args.get('name')), 'w') as config_file: + config_file.write(json.dumps(config_data, indent=4)) + config_file.close() + session['config'] = config_data return redirect(config_data['url']) diff --git a/app/static/js/controller.js b/app/static/js/controller.js index 4817195..15d18fd 100644 --- a/app/static/js/controller.js +++ b/app/static/js/controller.js @@ -71,6 +71,41 @@ const setupConfigLayout = () => { fillConfigValues(); }; +const loadConfig = event => { + event.preventDefault(); + let config = prompt("Enter name of config:"); + if (!config) { + alert("Must specify a name for the config to load"); + return; + } + + let xhrPUT = new XMLHttpRequest(); + xhrPUT.open("PUT", "/config?name=" + config + '.json'); + xhrPUT.onload = function() { + if (xhrPUT.readyState === 4 && xhrPUT.status !== 200) { + alert("Error loading Whoogle config"); + return; + } + + location.reload(true); + }; + + xhrPUT.send(); +}; + +const saveConfig = event => { + event.preventDefault(); + let config = prompt("Enter name for this config:"); + if (!config) { + alert("Must specify a name for the config to save"); + return; + } + + let configForm = document.getElementById("config-form"); + configForm.action = '/config?name=' + config + '.json'; + configForm.submit(); +}; + document.addEventListener("DOMContentLoaded", function() { setTimeout(function() { document.getElementById("main").style.display = "block"; diff --git a/app/templates/index.html b/app/templates/index.html index cf40a82..e85d796 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -40,7 +40,7 @@