def get(self):
"""Get a range of messages using a "limit"
and an "offset."
Returns:
list: list of dictionaries describing
messages, if successful.
None: If aborted.
"""
json_data = get_valid_json(self.SCHEMA_GET)
offset = int(json_data['offset'])
limit = int(json_data['limit'])
# Just make sure not requesting too many at once!
if limit > config.LIMITS_MESSAGES_GET_LIMIT:
message = ("You may only request %d messages at once."
% config.LIMITS_MESSAGES_GET_LIMIT)
flask_restful.abort(400, message=message)
# Now we're sure we have the right data to
# make a query, actually do that query and
# return said data or 404 if nothing matches.
query = db.session.query(models.Message)
results = query.limit(limit).offset(offset).all()
if results == []:
message = ("No messages found at offset %d limit %d"
% (offset, limit))
flask_restful.abort(404, message=message)
else:
return [r.to_dict() for r in results]
评论列表
文章目录