server/comments: catch bad IDs

This commit is contained in:
rr- 2016-08-14 11:43:19 +02:00
parent 56b3eb9674
commit 65efc309a8
1 changed files with 5 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import datetime
from szurubooru import db, errors from szurubooru import db, errors
from szurubooru.func import users, scores, util from szurubooru.func import users, scores, util
class InvalidCommentIdError(errors.ValidationError): pass
class CommentNotFoundError(errors.NotFoundError): pass class CommentNotFoundError(errors.NotFoundError): pass
class EmptyCommentTextError(errors.ValidationError): pass class EmptyCommentTextError(errors.ValidationError): pass
@ -22,6 +23,10 @@ def serialize_comment(comment, auth_user, options=None):
options) options)
def try_get_comment_by_id(comment_id): def try_get_comment_by_id(comment_id):
try:
comment_id = int(comment_id)
except ValueError:
raise InvalidCommentIdError('Invalid comment ID: %r.' % comment_id)
return db.session \ return db.session \
.query(db.Comment) \ .query(db.Comment) \
.filter(db.Comment.comment_id == comment_id) \ .filter(db.Comment.comment_id == comment_id) \