Skip to content

Commit 78fd40a

Browse files
Created validators.py and moved validate_results_request in validators.py
1 parent 7affd83 commit 78fd40a

2 files changed

Lines changed: 41 additions & 39 deletions

File tree

codespeed/validators.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from django.core.exceptions import ValidationError
2+
3+
4+
def validate_results_request(data):
5+
"""
6+
Validates that a result request dictionary has all needed parameters
7+
and their type is correct.
8+
9+
Throws ValidationError on error
10+
"""
11+
mandatory_data = [
12+
'env',
13+
'proj',
14+
'branch',
15+
'exe',
16+
'ben',
17+
]
18+
19+
for key in mandatory_data:
20+
if key not in data:
21+
raise ValidationError('Key "' + key +
22+
'" missing from GET request!')
23+
elif data[key] == '':
24+
raise ValidationError('Value for key "' + key +
25+
'" empty in GET request!')
26+
27+
# Check that 'revs' is the correct format (if it exists)
28+
if 'revs' in data:
29+
try:
30+
rev_value = int(data['revs'])
31+
except ValueError:
32+
raise ValidationError('Value for key "revs" is not an integer!')
33+
if rev_value <= 0:
34+
raise ValidationError('Value for key "revs" should be a'
35+
' strictly positive integer!', True)

codespeed/views.py

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import django
77

88
from django.core.urlresolvers import reverse
9+
from django.core.exceptions import ValidationError
910
from django.http import HttpResponse, Http404, HttpResponseBadRequest,\
1011
HttpResponseNotFound
1112
from django.shortcuts import get_object_or_404, render_to_response
@@ -20,6 +21,7 @@
2021
getdefaultexecutable, getcomparisonexes)
2122
from .results import save_result, create_report_if_enough_data
2223
from . import commits
24+
from .validators import validate_results_request
2325

2426
import cStringIO
2527
from matplotlib.figure import Figure
@@ -746,41 +748,6 @@ def django_has_content_type():
746748
(django.VERSION[0] == 1 and django.VERSION[1] >= 6))
747749

748750

749-
def validate_results_request(data):
750-
"""
751-
Validates that a result request dictionary has all needed parameters
752-
753-
It returns a tuple
754-
"", False when no errors where found
755-
Error_message, True when there is an error
756-
"""
757-
mandatory_data = [
758-
'env',
759-
'proj',
760-
'branch',
761-
'exe',
762-
'ben',
763-
]
764-
765-
for key in mandatory_data:
766-
if key not in data:
767-
return 'Key "' + key + '" missing from GET request', True
768-
elif data[key] == '':
769-
return 'Value for key "' + key + '" empty in GET request', True
770-
771-
# Check that 'revs' is the correct format (if it exists)
772-
if 'revs' in data:
773-
try:
774-
rev_value = int(data['revs'])
775-
except:
776-
return 'Value for key "revs" is not an integer', True
777-
if rev_value <= 0:
778-
return 'Value for key "revs" should be a strictly positive '\
779-
'integer', True
780-
781-
return '', False
782-
783-
784751
def get_benchmark_results(data):
785752
try:
786753
environment = Environment.objects.get(name=data['env'])
@@ -903,10 +870,10 @@ def get_benchmark_results(data):
903870
def makeimage(request):
904871
data = request.GET
905872

906-
err_msg, err = validate_results_request(data)
907-
908-
if err:
909-
return HttpResponseBadRequest(err_msg)
873+
try:
874+
validate_results_request(data)
875+
except ValidationError as err:
876+
return HttpResponseBadRequest(str(err))
910877

911878
result_data, error = get_benchmark_results(data)
912879

0 commit comments

Comments
 (0)