KemoFureApi/modules/Archive/endpoints/posts.py

55 lines
1.8 KiB
Python
Raw Normal View History

2024-12-30 16:35:57 +08:00
from __future__ import annotations
from flask_restful import Resource
2025-01-12 17:27:10 +08:00
from flask import current_app as app, request
2024-12-30 16:35:57 +08:00
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from modules.Archive.database import Database
2025-01-12 17:27:10 +08:00
class GetPosts(Resource):
2024-12-30 16:35:57 +08:00
def get(self):
db : Database = app.databases["Archive"]
try:
2025-01-02 19:05:25 +08:00
count = int(request.args["count"]) if "count" in request.args else 20
2024-12-30 16:35:57 +08:00
page = int(request.args["page"]) if "page" in request.args else 1
2025-01-03 06:19:52 +08:00
artist = request.args["artist"] if "artist" in request.args else None
2024-12-30 16:35:57 +08:00
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
2025-01-03 05:12:35 +08:00
actions = []
2024-12-30 16:35:57 +08:00
if "undecided" in request.args:
2025-01-03 05:12:35 +08:00
actions.append(0)
2024-12-30 16:35:57 +08:00
if "approved" in request.args:
2025-01-03 05:12:35 +08:00
actions.append(1)
2024-12-30 16:35:57 +08:00
if "denied" in request.args:
2025-01-03 05:12:35 +08:00
actions.append(2)
if "hidden" in request.args:
2025-01-03 05:12:35 +08:00
actions.append(3)
if actions == []:
actions = [0,1,2,3]
ratings = []
if "SFW" in request.args:
ratings.append("SFW")
if "NSFW" in request.args:
ratings.append("NSFW")
if "NSFL" in request.args:
ratings.append("NSFL")
if ratings == []:
ratings = ["SFW", "NSFW"]
2024-12-30 16:35:57 +08:00
2025-01-12 01:32:03 +08:00
if "random" in request.args:
order = "RAND"
elif "ascending" in request.args:
order = "ASC"
else:
order = "DESC"
2024-12-30 16:35:57 +08:00
real_page = page-1
offset = real_page * count
2025-01-12 01:32:03 +08:00
result = db.get_posts(count, artist, actions_taken=actions, last_id= -1, offset= offset, include_ratings= ratings, order=order)
2025-01-12 17:27:10 +08:00
return db.wrap_query_response(result)