From 5acf92155df9e3596039439fe72191528d84df77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nut=CC=A6iu?= Date: Sat, 22 Apr 2017 13:56:34 +0300 Subject: [PATCH] Adding positive score constraint --- src/models/__init__.py | 7 ++++++- src/views/scoreboard.py | 8 +++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/models/__init__.py b/src/models/__init__.py index f309e56..c661b7d 100644 --- a/src/models/__init__.py +++ b/src/models/__init__.py @@ -32,7 +32,7 @@ class Result(db.Model): log = db.Column(db.String(10000)) score = db.Column(db.Integer) - def __init__(self, name="Anonymous", gpu=None, cpu=None, log=None, score=0): + def __init__(self, name="Anonymous", gpu=None, cpu=None, log=None, score=1): self.name = name self.gpu = gpu self.cpu = cpu @@ -41,3 +41,8 @@ class Result(db.Model): def __repr__(self): return self.gpu + + __table_args__ = ( + db.CheckConstraint(score > 0, name="positive_score_constraint"), + {} + ) diff --git a/src/views/scoreboard.py b/src/views/scoreboard.py index 5faadc9..44a29b2 100644 --- a/src/views/scoreboard.py +++ b/src/views/scoreboard.py @@ -54,10 +54,12 @@ def result_post(): cpu = content['cpu'] log = content['log'] score = int(content['score']) + if score <= 0: + error = "Score must be positive!" except KeyError: # Json doesn't contain the keys we need. - error = "invalid json keys: gpu, cpu, log, score name" + error = "Invalid json keys: gpu, cpu, log, score name!" except TypeError: # The types from the json object are not correct. - error = "invalid json object" + error = "Invalid json object!" if error: return flask.jsonify({'error': error, 'success': False}), 400 @@ -69,7 +71,7 @@ def result_post(): location = "/result/{}".format(entry.id) - return flask.jsonify({'success': True, 'location': location}), 201, {'location': location} + return flask.jsonify({'success': True}), 201, {'location': location} @utilities.cache.cached(timeout=60*5)