add error handling for incorrect time format in search

master
Aevann1 2022-08-31 03:48:20 +02:00
parent 09d95e3e63
commit 60709a7253
1 changed files with 12 additions and 4 deletions

View File

@ -137,13 +137,17 @@ def searchposts(v):
if 'after' in criteria: if 'after' in criteria:
after = criteria['after'] after = criteria['after']
try: after = int(after) try: after = int(after)
except: after = timegm(time.strptime(after, "%Y-%m-%d")) except:
try: after = timegm(time.strptime(after, "%Y-%m-%d"))
except: abort(400)
posts = posts.filter(Submission.created_utc > after) posts = posts.filter(Submission.created_utc > after)
if 'before' in criteria: if 'before' in criteria:
before = criteria['before'] before = criteria['before']
try: before = int(before) try: before = int(before)
except: before = timegm(time.strptime(before, "%Y-%m-%d")) except:
try: before = timegm(time.strptime(before, "%Y-%m-%d"))
except: abort(400)
posts = posts.filter(Submission.created_utc < before) posts = posts.filter(Submission.created_utc < before)
posts = apply_time_filter(t, posts, Submission) posts = apply_time_filter(t, posts, Submission)
@ -243,13 +247,17 @@ def searchcomments(v):
if 'after' in criteria: if 'after' in criteria:
after = criteria['after'] after = criteria['after']
try: after = int(after) try: after = int(after)
except: after = timegm(time.strptime(after, "%Y-%m-%d")) except:
try: after = timegm(time.strptime(after, "%Y-%m-%d"))
except: abort(400)
comments = comments.filter(Comment.created_utc > after) comments = comments.filter(Comment.created_utc > after)
if 'before' in criteria: if 'before' in criteria:
before = criteria['before'] before = criteria['before']
try: before = int(before) try: before = int(before)
except: before = timegm(time.strptime(before, "%Y-%m-%d")) except:
try: before = timegm(time.strptime(before, "%Y-%m-%d"))
except: abort(400)
comments = comments.filter(Comment.created_utc < before) comments = comments.filter(Comment.created_utc < before)
comments = sort_comments(sort, comments) comments = sort_comments(sort, comments)