KemoFureApi/modules/Archive/endpoints/set_action.py

45 lines
1.4 KiB
Python

from __future__ import annotations
import json
from flask_cors import cross_origin
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_SetAction(Resource):
@cross_origin(supports_credentials=True)
def post(self):
data = request.get_json()
try:
bypass = ("allow_override" in data) and data["allow_override"]
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)
response.headers.add("Access-Control-Allow-Origin", "*")
return response
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)
response = app.response_class(
response=result,
status=200,
mimetype='application/json'
)
response.headers.add("Access-Control-Allow-Origin", "*")
return response