Skip to content

Commit 5fe22e5

Browse files
authored
Merge pull request #367 from metabrainz/validate-int
Validate integer query params
2 parents eade73c + 40996b1 commit 5fe22e5

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

metabrainz/admin/views.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import json
2121
import socket
2222

23+
from metabrainz.utils import get_int_query_param
24+
2325

2426
class HomeView(AdminIndexView):
2527

@@ -193,7 +195,7 @@ class CommercialUsersView(AdminBaseView):
193195

194196
@expose('/')
195197
def index(self):
196-
page = int(request.args.get('page', default=1))
198+
page = get_int_query_param('page', default=1)
197199
if page < 1:
198200
return redirect(url_for('.index'))
199201
limit = 20
@@ -207,7 +209,7 @@ class PaymentsView(AdminBaseView):
207209

208210
@expose('/')
209211
def list(self):
210-
page = int(request.args.get('page', default=1))
212+
page = get_int_query_param('page', default=1)
211213
is_donation_arg = request.args.get('is_donation')
212214
if is_donation_arg == "True":
213215
is_donation = True
@@ -283,12 +285,7 @@ def lookup_ips(users):
283285

284286
@expose('/top-ips/')
285287
def top_ips(self):
286-
287-
days = 7
288-
try:
289-
days = int(request.args.get('days'))
290-
except:
291-
pass
288+
days = get_int_query_param('days', default=7)
292289

293290
non_commercial, commercial = AccessLog.top_ips(limit=100, days=days)
294291

@@ -302,15 +299,9 @@ def top_ips(self):
302299
days=days
303300
)
304301

305-
306302
@expose('/top-tokens/')
307303
def top_tokens(self):
308-
309-
days = 7
310-
try:
311-
days = int(request.args.get('days'))
312-
except:
313-
pass
304+
days = get_int_query_param('days', default=7)
314305

315306
non_commercial, commercial = AccessLog.top_tokens(limit=100, days=days)
316307
return self.render(
@@ -323,7 +314,7 @@ def top_tokens(self):
323314

324315
@expose('/token-log')
325316
def token_log(self):
326-
page = int(request.args.get('page', default=1))
317+
page = get_int_query_param('page', default=1)
327318
if page < 1:
328319
return redirect(url_for('.token_log'))
329320
limit = 20

metabrainz/payments/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from requests.exceptions import RequestException
1111
from werkzeug.exceptions import BadRequest
1212

13+
from metabrainz.utils import get_int_query_param
14+
1315
payments_bp = Blueprint('payments', __name__)
1416

1517

@@ -47,7 +49,7 @@ def payment(currency):
4749

4850
@payments_bp.route('/donors')
4951
def donors():
50-
page = int(request.args.get('page', default=1))
52+
page = get_int_query_param('page', default=1)
5153
if page < 1:
5254
return redirect(url_for('.donors'))
5355
limit = 30

metabrainz/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import string
55
import subprocess
66

7+
from flask import request
8+
79

810
def reformat_datetime(value, format='%x %X %Z'):
911
return value.strftime(format)
@@ -29,3 +31,19 @@ def build_url(base, additional_params=None):
2931
(url.scheme, url.netloc, url.path, url.params,
3032
urlencode(query_params), url.fragment)
3133
)
34+
35+
36+
def get_int_query_param(key: str, default: int):
37+
""" Get an integer query parameter from the current request
38+
Args:
39+
key: the key whose value to retrieve
40+
default: the value to return in case the param is missing
41+
or not a valid integer
42+
Returns:
43+
the value of query param if its available and a valid integer,
44+
else the default value
45+
"""
46+
try:
47+
return int(request.args.get(key, default=default))
48+
except ValueError:
49+
return default

0 commit comments

Comments
 (0)