KemoFureApi/modules/Archive/endpoints/set_action.py

35 lines
1.2 KiB
Python
Raw Permalink Normal View History

2025-01-02 19:05:25 +08:00
from __future__ import annotations
import json
from flask_restful import Resource
2025-01-12 17:27:10 +08:00
from flask import current_app as app, request
2025-01-02 19:05:25 +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 SetAction(Resource):
2025-01-02 19:05:25 +08:00
def post(self):
2025-01-02 20:17:17 +08:00
data = json.loads(request.data)
2025-01-02 19:05:25 +08:00
try:
2025-01-02 20:17:17 +08:00
bypass = ("allow_override" in data) and data["allow_override"]
2025-01-02 19:05:25 +08:00
action = data["action_taken"]
if "id_str" in data:
id = int(data["id_str"])
elif "id" in data:
id = data["id"]
else:
raise Exception("no id (int) or id_str (str) specified")
except Exception as e:
print(e)
response = app.response_class(response=e, status=400)
2025-01-02 20:17:17 +08:00
response.headers.add("Access-Control-Allow-Origin", "*")
return response
2025-01-02 19:05:25 +08:00
db : Database = app.databases["Archive"]
query = f"UPDATE x_posts SET action_taken = {action} WHERE id = {id}"
if not bypass:
query += " AND action_taken = 0"
result = db.db.run_command(query)
2025-01-12 17:27:10 +08:00
return db.wrap_query_response(result, mode="text")