49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
from __future__ import annotations
|
|
import json
|
|
from flask_restful import Resource
|
|
from flask import current_app as app, jsonify, request
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from modules.Archive.database import Database
|
|
|
|
class Archive_GetPosts(Resource):
|
|
def get(self):
|
|
db : Database = app.databases["Archive"]
|
|
try:
|
|
count = int(request.args["count"]) if "count" in request.args else 20
|
|
page = int(request.args["page"]) if "page" in request.args else 1
|
|
last_id = int(request.args["last_id"]) if "last_id" in request.args else -1
|
|
except:
|
|
response = app.response_class(status=400)
|
|
response.headers.add("Access-Control-Allow-Origin", "*")
|
|
return response
|
|
|
|
param = []
|
|
if "undecided" in request.args:
|
|
param.append(0)
|
|
if "approved" in request.args:
|
|
param.append(1)
|
|
if "denied" in request.args:
|
|
param.append(2)
|
|
if "hidden" in request.args:
|
|
param.append(3)
|
|
if param == []:
|
|
param = [0,1,2,3]
|
|
|
|
real_page = page-1
|
|
offset = real_page * count
|
|
|
|
result = db.get_posts(count, param, last_id, offset)
|
|
|
|
if result is None:
|
|
response = app.response_class(status=400)
|
|
else:
|
|
response = app.response_class(
|
|
response=json.dumps(result, ensure_ascii=False, indent=1),
|
|
status=200,
|
|
mimetype='application/json'
|
|
)
|
|
|
|
response.headers.add("Access-Control-Allow-Origin", "*")
|
|
return response |