Refactoring /result and making errors more explicit
This commit is contained in:
parent
0513695586
commit
5265ff8e14
1 changed files with 19 additions and 20 deletions
|
@ -45,26 +45,23 @@ def result_post():
|
||||||
"""
|
"""
|
||||||
Allows the upload of resources via POST.
|
Allows the upload of resources via POST.
|
||||||
"""
|
"""
|
||||||
content = flask.request.get_json()
|
|
||||||
error = None
|
|
||||||
gpu = cpu = log = score = name = None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
content = flask.request.get_json()
|
||||||
|
|
||||||
name = content['name']
|
name = content['name']
|
||||||
gpu = content['gpu']
|
gpu = content['gpu']
|
||||||
cpu = content['cpu']
|
cpu = content['cpu']
|
||||||
log = content['log']
|
log = content['log']
|
||||||
score = int(content['score'])
|
score = int(content['score'])
|
||||||
if score <= 0:
|
if score <= 0:
|
||||||
error = "Score must be positive!"
|
raise ValueError("Score must be positive!")
|
||||||
except KeyError: # Json doesn't contain the keys we need.
|
except KeyError as e: # Json doesn't contain the keys we need.
|
||||||
error = "Invalid json keys: gpu, cpu, log, score name!"
|
error = "Key not found: {}.".format(str(e))
|
||||||
except TypeError: # The types from the json object are not correct.
|
except TypeError as e: # If we don't get a string
|
||||||
error = "Invalid json object!"
|
error = str(e)
|
||||||
|
except ValueError as e:
|
||||||
if error:
|
error = str(e)
|
||||||
return flask.jsonify({'error': error, 'success': False}), 400
|
else:
|
||||||
|
|
||||||
# Add data to the database
|
# Add data to the database
|
||||||
entry = Result(name=name, gpu=gpu, cpu=cpu, log=log, score=score)
|
entry = Result(name=name, gpu=gpu, cpu=cpu, log=log, score=score)
|
||||||
db.session.add(entry)
|
db.session.add(entry)
|
||||||
|
@ -75,6 +72,8 @@ def result_post():
|
||||||
location = "/result/{}".format(entry.id)
|
location = "/result/{}".format(entry.id)
|
||||||
|
|
||||||
return flask.jsonify({'success': True}), 201, {'location': location}
|
return flask.jsonify({'success': True}), 201, {'location': location}
|
||||||
|
# Exception occurred
|
||||||
|
return flask.jsonify({'error': error, 'success': False}), 400
|
||||||
|
|
||||||
|
|
||||||
@utilities.cache.cached(timeout=60*5)
|
@utilities.cache.cached(timeout=60*5)
|
||||||
|
|
Loading…
Reference in a new issue