|
|
@ -61,6 +61,7 @@ video_fields = {
|
|
|
|
comment_fields = {
|
|
|
|
comment_fields = {
|
|
|
|
'id': flask_restful.fields.String(attribute='comment.id'),
|
|
|
|
'id': flask_restful.fields.String(attribute='comment.id'),
|
|
|
|
'video_id': flask_restful.fields.Integer(),
|
|
|
|
'video_id': flask_restful.fields.Integer(),
|
|
|
|
|
|
|
|
'video_recorded_at': flask_restful.fields.DateTime(dt_format='iso8601', attribute='video.recorded_at'),
|
|
|
|
'offset': flask_restful.fields.Float(),
|
|
|
|
'offset': flask_restful.fields.Float(),
|
|
|
|
'commenter_id': flask_restful.fields.Integer(attribute='comment.commenter_id'),
|
|
|
|
'commenter_id': flask_restful.fields.Integer(attribute='comment.commenter_id'),
|
|
|
|
'commenter_name': flask_restful.fields.String(attribute='comment.commenter_name'),
|
|
|
|
'commenter_name': flask_restful.fields.String(attribute='comment.commenter_name'),
|
|
|
@ -74,6 +75,16 @@ comment_fields = {
|
|
|
|
'updated_at': flask_restful.fields.DateTime(dt_format='iso8601', attribute='comment.updated_at'),
|
|
|
|
'updated_at': flask_restful.fields.DateTime(dt_format='iso8601', attribute='comment.updated_at'),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
search_fields = {
|
|
|
|
|
|
|
|
**comment_fields,
|
|
|
|
|
|
|
|
'video_title': flask_restful.fields.String(attribute='video.title'),
|
|
|
|
|
|
|
|
'video_game': flask_restful.fields.String(attribute='video.game'),
|
|
|
|
|
|
|
|
'video_length': flask_restful.fields.Integer(attribute='video.length'),
|
|
|
|
|
|
|
|
'video_thumbnail_small': flask_restful.fields.String(attribute='video.thumbnail_small'),
|
|
|
|
|
|
|
|
'video_thumbnail_medium': flask_restful.fields.String(attribute='video.thumbnail_medium'),
|
|
|
|
|
|
|
|
'video_thumbnail_large': flask_restful.fields.String(attribute='video.thumbnail_large'),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
emote_fields = {
|
|
|
|
emote_fields = {
|
|
|
|
'id': flask_restful.fields.Integer(),
|
|
|
|
'id': flask_restful.fields.Integer(),
|
|
|
|
'code': flask_restful.fields.String(),
|
|
|
|
'code': flask_restful.fields.String(),
|
|
|
@ -87,6 +98,14 @@ filter_parser.add_argument('sort_order', type=str)
|
|
|
|
filter_parser.add_argument('page_number', type=int)
|
|
|
|
filter_parser.add_argument('page_number', type=int)
|
|
|
|
filter_parser.add_argument('page_size', type=int)
|
|
|
|
filter_parser.add_argument('page_size', type=int)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
search_parser = flask_restful.reqparse.RequestParser()
|
|
|
|
|
|
|
|
search_parser.add_argument('commenter', type=str)
|
|
|
|
|
|
|
|
search_parser.add_argument('term', type=str)
|
|
|
|
|
|
|
|
search_parser.add_argument('sort_by', type=str)
|
|
|
|
|
|
|
|
search_parser.add_argument('sort_order', type=str)
|
|
|
|
|
|
|
|
search_parser.add_argument('page_number', type=int)
|
|
|
|
|
|
|
|
search_parser.add_argument('page_size', type=int)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VideoResource(flask_restful.Resource):
|
|
|
|
class VideoResource(flask_restful.Resource):
|
|
|
|
@flask_restful.marshal_with(video_fields)
|
|
|
|
@flask_restful.marshal_with(video_fields)
|
|
|
@ -132,8 +151,9 @@ class CommentResource(flask_restful.Resource):
|
|
|
|
video = q.first()
|
|
|
|
video = q.first()
|
|
|
|
if not video:
|
|
|
|
if not video:
|
|
|
|
flask_restful.abort(404, message='Video {0} does not exist'.format(video_id))
|
|
|
|
flask_restful.abort(404, message='Video {0} does not exist'.format(video_id))
|
|
|
|
q = db.session.query(Association).join(Comment).filter(
|
|
|
|
q = db.session.query(Association).join(Comment)
|
|
|
|
Association.video_id == video_id, Association.comment_id == comment_id)
|
|
|
|
q = q.filter((Association.video_id == video_id) &\
|
|
|
|
|
|
|
|
(Association.comment_id == comment_id))
|
|
|
|
assoc = q.first()
|
|
|
|
assoc = q.first()
|
|
|
|
if not assoc:
|
|
|
|
if not assoc:
|
|
|
|
flask_restful.abort(404,
|
|
|
|
flask_restful.abort(404,
|
|
|
@ -149,7 +169,8 @@ class CommentsResource(flask_restful.Resource):
|
|
|
|
video = q.first()
|
|
|
|
video = q.first()
|
|
|
|
if not video:
|
|
|
|
if not video:
|
|
|
|
flask_restful.abort(404, message='Video {0} does not exist'.format(id))
|
|
|
|
flask_restful.abort(404, message='Video {0} does not exist'.format(id))
|
|
|
|
q = db.session.query(Association).join(Comment).filter(Association.video_id == id)
|
|
|
|
q = db.session.query(Association).join(Comment).join(Video)
|
|
|
|
|
|
|
|
q = q.filter(Association.video_id == id)
|
|
|
|
if args['filter']:
|
|
|
|
if args['filter']:
|
|
|
|
q = q.filter(Comment.message_body.ilike('%{}%'.format(args['filter'])))
|
|
|
|
q = q.filter(Comment.message_body.ilike('%{}%'.format(args['filter'])))
|
|
|
|
count = q.count()
|
|
|
|
count = q.count()
|
|
|
@ -159,6 +180,44 @@ class CommentsResource(flask_restful.Resource):
|
|
|
|
col = getattr(Association, args['sort_by'], None)
|
|
|
|
col = getattr(Association, args['sort_by'], None)
|
|
|
|
if not col:
|
|
|
|
if not col:
|
|
|
|
col = getattr(Comment, args['sort_by'], None)
|
|
|
|
col = getattr(Comment, args['sort_by'], None)
|
|
|
|
|
|
|
|
if not col:
|
|
|
|
|
|
|
|
col = getattr(Video, args['sort_by'].split('video_', 1).pop(), None)
|
|
|
|
|
|
|
|
if col:
|
|
|
|
|
|
|
|
if args['sort_order']:
|
|
|
|
|
|
|
|
order_by = getattr(col, args['sort_order'], None)
|
|
|
|
|
|
|
|
if order_by:
|
|
|
|
|
|
|
|
q = q.order_by(order_by())
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
q = q.order_by(col)
|
|
|
|
|
|
|
|
if args['page_size']:
|
|
|
|
|
|
|
|
q = q.limit(args['page_size'])
|
|
|
|
|
|
|
|
if args['page_number'] and args['page_size']:
|
|
|
|
|
|
|
|
q = q.offset(args['page_number'] * args['page_size'])
|
|
|
|
|
|
|
|
assocs = q.all()
|
|
|
|
|
|
|
|
return assocs, 200, {'X-Total-Count': count}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SearchResource(flask_restful.Resource):
|
|
|
|
|
|
|
|
@flask_restful.marshal_with(search_fields)
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
|
|
|
|
args = search_parser.parse_args()
|
|
|
|
|
|
|
|
if not args['commenter'] and not args['term']:
|
|
|
|
|
|
|
|
return [], 200, {'X-Total-Count': 0}
|
|
|
|
|
|
|
|
q = db.session.query(Association).join(Comment).join(Video)
|
|
|
|
|
|
|
|
if args['commenter']:
|
|
|
|
|
|
|
|
q = q.filter(Comment.commenter_name.ilike('%{}%'.format(args['commenter'])) |\
|
|
|
|
|
|
|
|
Comment.commenter_display_name.ilike('%{}%'.format(args['commenter'])))
|
|
|
|
|
|
|
|
if args['term']:
|
|
|
|
|
|
|
|
q = q.filter(Comment.message_body.ilike('%{}%'.format(args['term'])))
|
|
|
|
|
|
|
|
count = q.count()
|
|
|
|
|
|
|
|
if args['sort_order'] == 'random':
|
|
|
|
|
|
|
|
q = q.order_by(sqlalchemy.func.random())
|
|
|
|
|
|
|
|
elif args['sort_by']:
|
|
|
|
|
|
|
|
col = getattr(Association, args['sort_by'], None)
|
|
|
|
|
|
|
|
if not col:
|
|
|
|
|
|
|
|
col = getattr(Comment, args['sort_by'], None)
|
|
|
|
|
|
|
|
if not col:
|
|
|
|
|
|
|
|
col = getattr(Video, args['sort_by'].split('video_', 1).pop(), None)
|
|
|
|
if col:
|
|
|
|
if col:
|
|
|
|
if args['sort_order']:
|
|
|
|
if args['sort_order']:
|
|
|
|
order_by = getattr(col, args['sort_order'], None)
|
|
|
|
order_by = getattr(col, args['sort_order'], None)
|
|
|
@ -196,6 +255,7 @@ api.add_resource(VideoResource, '/videos/<int:id>')
|
|
|
|
api.add_resource(VideosResource, '/videos')
|
|
|
|
api.add_resource(VideosResource, '/videos')
|
|
|
|
api.add_resource(CommentResource, '/videos/<int:video_id>/comments/<string:comment_id>')
|
|
|
|
api.add_resource(CommentResource, '/videos/<int:video_id>/comments/<string:comment_id>')
|
|
|
|
api.add_resource(CommentsResource, '/videos/<int:id>/comments')
|
|
|
|
api.add_resource(CommentsResource, '/videos/<int:id>/comments')
|
|
|
|
|
|
|
|
api.add_resource(SearchResource, '/search')
|
|
|
|
api.add_resource(EmoteResource, '/emotes/<int:id>')
|
|
|
|
api.add_resource(EmoteResource, '/emotes/<int:id>')
|
|
|
|
api.add_resource(EmotesResource, '/emotes')
|
|
|
|
api.add_resource(EmotesResource, '/emotes')
|
|
|
|
|
|
|
|
|
|
|
|