add file upload to api
This commit is contained in:
parent
3eca405595
commit
99dc16895e
|
@ -28,7 +28,7 @@ def mysterycheck():
|
||||||
if type(options) == str:
|
if type(options) == str:
|
||||||
flash(options)
|
flash(options)
|
||||||
else:
|
else:
|
||||||
results, _ = roll_yamls(options)
|
results, _ = roll_options(options)
|
||||||
return render_template("checkresult.html", results=results)
|
return render_template("checkresult.html", results=results)
|
||||||
|
|
||||||
return render_template("check.html")
|
return render_template("check.html")
|
||||||
|
@ -60,11 +60,14 @@ def get_yaml_data(file) -> Union[Dict[str, str], str]:
|
||||||
return options
|
return options
|
||||||
|
|
||||||
|
|
||||||
def roll_yamls(options: Dict[str, Union[str, str]]) -> Tuple[Dict[str, Union[str, bool]], Dict[str, dict]]:
|
def roll_options(options: Dict[str, Union[dict, str]]) -> Tuple[Dict[str, Union[str, bool]], Dict[str, dict]]:
|
||||||
results = {}
|
results = {}
|
||||||
rolled_results = {}
|
rolled_results = {}
|
||||||
for filename, text in options.items():
|
for filename, text in options.items():
|
||||||
try:
|
try:
|
||||||
|
if type(text) is dict:
|
||||||
|
yaml_data = text
|
||||||
|
else:
|
||||||
yaml_data = parse_yaml(text)
|
yaml_data = parse_yaml(text)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
results[filename] = f"Failed to parse YAML data in {filename}: {e}"
|
results[filename] = f"Failed to parse YAML data in {filename}: {e}"
|
||||||
|
|
|
@ -13,7 +13,7 @@ import pickle
|
||||||
|
|
||||||
from .models import *
|
from .models import *
|
||||||
from WebHostLib import app
|
from WebHostLib import app
|
||||||
from .check import get_yaml_data, roll_yamls
|
from .check import get_yaml_data, roll_options
|
||||||
|
|
||||||
|
|
||||||
@app.route('/generate', methods=['GET', 'POST'])
|
@app.route('/generate', methods=['GET', 'POST'])
|
||||||
|
@ -29,7 +29,7 @@ def generate(race=False):
|
||||||
if type(options) == str:
|
if type(options) == str:
|
||||||
flash(options)
|
flash(options)
|
||||||
else:
|
else:
|
||||||
results, gen_options = roll_yamls(options)
|
results, gen_options = roll_options(options)
|
||||||
if any(type(result) == str for result in results.values()):
|
if any(type(result) == str for result in results.values()):
|
||||||
return render_template("checkresult.html", results=results)
|
return render_template("checkresult.html", results=results)
|
||||||
elif len(gen_options) > app.config["MAX_ROLL"]:
|
elif len(gen_options) > app.config["MAX_ROLL"]:
|
||||||
|
@ -55,20 +55,28 @@ def generate(race=False):
|
||||||
@app.route('/api/generate', methods=['POST'])
|
@app.route('/api/generate', methods=['POST'])
|
||||||
def generate_api():
|
def generate_api():
|
||||||
try:
|
try:
|
||||||
|
options = {}
|
||||||
|
if 'file' in request.files:
|
||||||
|
file = request.files['file']
|
||||||
|
options = get_yaml_data(file)
|
||||||
|
|
||||||
json_data = request.get_json()
|
json_data = request.get_json()
|
||||||
if json_data:
|
if 'weights' in json_data:
|
||||||
# example: options = {"player1weights" : {<weightsdata>}}
|
# example: options = {"player1weights" : {<weightsdata>}}
|
||||||
options = json_data["weights"]
|
options = json_data["weights"]
|
||||||
|
if not options:
|
||||||
|
return {"text": "No options found. Expected file attachment or json weights."
|
||||||
|
}, 400
|
||||||
|
|
||||||
if len(options) > app.config["MAX_ROLL"]:
|
if len(options) > app.config["MAX_ROLL"]:
|
||||||
return {"text": "Max size of multiworld exceeded",
|
return {"text": "Max size of multiworld exceeded",
|
||||||
"detail": app.config["MAX_ROLL"]}, 409
|
"detail": app.config["MAX_ROLL"]}, 409
|
||||||
|
|
||||||
race = bool(json_data["race"])
|
race = bool(json_data["race"])
|
||||||
results, gen_options = roll_yamls(options)
|
results, gen_options = roll_options(options)
|
||||||
if any(type(result) == str for result in results.values()):
|
if any(type(result) == str for result in results.values()):
|
||||||
return {"text": str(results),
|
return {"text": str(results),
|
||||||
"detail": results}, 400
|
"detail": results}, 400
|
||||||
|
|
||||||
else:
|
else:
|
||||||
gen = Generation(
|
gen = Generation(
|
||||||
options=pickle.dumps({name: vars(options) for name, options in gen_options.items()}),
|
options=pickle.dumps({name: vars(options) for name, options in gen_options.items()}),
|
||||||
|
@ -81,8 +89,6 @@ def generate_api():
|
||||||
"encoded": app.url_map.converters["suuid"].to_url(gen.id),
|
"encoded": app.url_map.converters["suuid"].to_url(gen.id),
|
||||||
"wait_api_url": url_for(wait_seed_api, seed=gen.id),
|
"wait_api_url": url_for(wait_seed_api, seed=gen.id),
|
||||||
"url": url_for(wait_seed, seed=gen.id)}, 201
|
"url": url_for(wait_seed, seed=gen.id)}, 201
|
||||||
else:
|
|
||||||
return {"text": "POST data empty or incorrectly headered."}, 400
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"text": "Uncaught Exception:" + str(e)}, 500
|
return {"text": "Uncaught Exception:" + str(e)}, 500
|
||||||
|
|
||||||
|
@ -151,6 +157,7 @@ def wait_seed(seed: UUID):
|
||||||
return "Generation failed, please retry."
|
return "Generation failed, please retry."
|
||||||
return render_template("wait_seed.html", seed_id=seed_id)
|
return render_template("wait_seed.html", seed_id=seed_id)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/status/<suuid:seed>')
|
@app.route('/api/status/<suuid:seed>')
|
||||||
def wait_seed_api(seed: UUID):
|
def wait_seed_api(seed: UUID):
|
||||||
seed_id = seed
|
seed_id = seed
|
||||||
|
@ -165,6 +172,7 @@ def wait_seed_api(seed: UUID):
|
||||||
return {"text": "Generation failed"}, 500
|
return {"text": "Generation failed"}, 500
|
||||||
return {"text": "Generation running"}, 202
|
return {"text": "Generation running"}, 202
|
||||||
|
|
||||||
|
|
||||||
def upload_to_db(folder, owner, sid):
|
def upload_to_db(folder, owner, sid):
|
||||||
patches = set()
|
patches = set()
|
||||||
spoiler = ""
|
spoiler = ""
|
||||||
|
|
Loading…
Reference in New Issue