suchwow/suchwow/routes/api.py

45 lines
1.2 KiB
Python
Raw Permalink Normal View History

from flask import jsonify, Blueprint, url_for, request, abort
2022-09-01 07:33:10 +01:00
2021-01-05 22:01:51 +00:00
from suchwow.models import Post
from suchwow import wownero
bp = Blueprint("api", "api")
@bp.route("/api/list")
def api_list():
limit = request.args.get('limit', 30)
offset = request.args.get('offset', 0)
2021-04-25 21:17:28 +01:00
# Hacky way to convert query str value to int
try:
limit = int(limit)
offset = int(offset)
except:
abort(500, "Bleep bleep")
2021-04-25 21:17:28 +01:00
if limit > 30:
limit = 30
2021-01-05 22:01:51 +00:00
all_posts = []
posts = Post.select().where(Post.approved==True).order_by(Post.timestamp.desc()).limit(limit).offset(offset)
2021-01-05 22:01:51 +00:00
for post in posts:
wallet = wownero.Wallet()
if wallet.connected:
address = wallet.get_address(account=post.account_index)
else:
address = ''
payload = {
'image': url_for('post.uploaded_file', filename=post.image_name, _external=True),
'submitter': post.submitter,
'address': address,
'title': post.title,
2021-01-05 22:04:31 +00:00
'text': post.text,
'href': url_for('post.read', id=post.id, _external=True),
'id': post.id,
'timestamp': post.timestamp
2021-01-05 22:01:51 +00:00
}
all_posts.append(payload)
return jsonify(all_posts)