added GetPosts/Count endpoint
This commit is contained in:
parent
794bf9d30b
commit
155394ab75
|
@ -1,10 +1,9 @@
|
||||||
import json
|
|
||||||
|
|
||||||
from .databaseController import DatabaseController
|
from .databaseController import DatabaseController
|
||||||
from .endpoints.query import Archive_Query
|
from .endpoints.query import Archive_Query
|
||||||
from .endpoints.get_post import Archive_GetPost
|
from .endpoints.get_post import Archive_GetPost
|
||||||
from .endpoints.get_posts import Archive_GetPosts
|
from .endpoints.get_posts import Archive_GetPosts
|
||||||
from .endpoints.set_action import Archive_SetAction
|
from .endpoints.set_action import Archive_SetAction
|
||||||
|
from .endpoints.get_posts_count import Archive_GetPostsCount
|
||||||
|
|
||||||
def get_post_dictionary(id, error_id, action_taken, text, files, date, x_handle, x_rating):
|
def get_post_dictionary(id, error_id, action_taken, text, files, date, x_handle, x_rating):
|
||||||
if files == "" or files is None:
|
if files == "" or files is None:
|
||||||
|
@ -33,6 +32,7 @@ class Database:
|
||||||
api.add_resource(Archive_Query, "/Archive/Query")
|
api.add_resource(Archive_Query, "/Archive/Query")
|
||||||
api.add_resource(Archive_GetPost, "/Archive/GetPost/<id>")
|
api.add_resource(Archive_GetPost, "/Archive/GetPost/<id>")
|
||||||
api.add_resource(Archive_GetPosts, "/Archive/GetPosts")
|
api.add_resource(Archive_GetPosts, "/Archive/GetPosts")
|
||||||
|
api.add_resource(Archive_GetPostsCount, "/Archive/GetPosts/Count")
|
||||||
api.add_resource(Archive_SetAction, "/Archive/SetAction")
|
api.add_resource(Archive_SetAction, "/Archive/SetAction")
|
||||||
|
|
||||||
def get_post(self, id):
|
def get_post(self, id):
|
||||||
|
@ -49,9 +49,7 @@ class Database:
|
||||||
result = result[-1]
|
result = result[-1]
|
||||||
return get_post_dictionary(result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7])
|
return get_post_dictionary(result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7])
|
||||||
|
|
||||||
def get_posts(self, num_posts, artist, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0, include_ratings = ["SFW", "NSFW"]):
|
def build_where_query(self, artist, actions_taken = [0, 1, 2, 3], last_id = -1, include_ratings = ["SFW", "NSFW"]):
|
||||||
num_posts = max(1, min(num_posts, 100))
|
|
||||||
|
|
||||||
where_query = "WHERE x_post_details.id NOT NULL AND x_posts.error_id = 0"
|
where_query = "WHERE x_post_details.id NOT NULL AND x_posts.error_id = 0"
|
||||||
if last_id != -1:
|
if last_id != -1:
|
||||||
where_query += f" AND x_posts.id < {last_id}"
|
where_query += f" AND x_posts.id < {last_id}"
|
||||||
|
@ -61,6 +59,26 @@ class Database:
|
||||||
where_query += " AND (" + " OR ".join([f'accounts.rating = "{rating}"' for rating in include_ratings]) + ")"
|
where_query += " AND (" + " OR ".join([f'accounts.rating = "{rating}"' for rating in include_ratings]) + ")"
|
||||||
if artist is not None and artist != "":
|
if artist is not None and artist != "":
|
||||||
where_query += f' AND accounts.x_handle = "{artist}"'
|
where_query += f' AND accounts.x_handle = "{artist}"'
|
||||||
|
return where_query
|
||||||
|
|
||||||
|
def get_posts_count(self, artist, actions_taken = [0, 1, 2, 3], last_id = -1, include_ratings = ["SFW", "NSFW"]):
|
||||||
|
where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings)
|
||||||
|
|
||||||
|
query = f'''
|
||||||
|
SELECT count(*) FROM x_posts
|
||||||
|
LEFT JOIN x_post_details
|
||||||
|
ON x_posts.id = x_post_details.id
|
||||||
|
LEFT JOIN accounts
|
||||||
|
ON x_posts.account_id = accounts.id
|
||||||
|
{where_query}'''
|
||||||
|
|
||||||
|
result = self.db.run_query(query)
|
||||||
|
return result[0][0]
|
||||||
|
|
||||||
|
def get_posts(self, num_posts, artist, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0, include_ratings = ["SFW", "NSFW"]):
|
||||||
|
num_posts = max(1, min(num_posts, 100))
|
||||||
|
|
||||||
|
where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings)
|
||||||
|
|
||||||
query = f'''
|
query = f'''
|
||||||
SELECT x_posts.id, x_posts.action_taken, x_post_details.text, x_post_details.files, x_post_details.date, accounts.x_handle, accounts.rating FROM x_posts
|
SELECT x_posts.id, x_posts.action_taken, x_post_details.text, x_post_details.files, x_post_details.date, accounts.x_handle, accounts.rating FROM x_posts
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
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_GetPostsCount(Resource):
|
||||||
|
def get(self):
|
||||||
|
db : Database = app.databases["Archive"]
|
||||||
|
try:
|
||||||
|
artist = request.args["artist"] if "artist" in request.args else None
|
||||||
|
except:
|
||||||
|
response = app.response_class(status=400)
|
||||||
|
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||||
|
return response
|
||||||
|
|
||||||
|
actions = []
|
||||||
|
if "undecided" in request.args:
|
||||||
|
actions.append(0)
|
||||||
|
if "approved" in request.args:
|
||||||
|
actions.append(1)
|
||||||
|
if "denied" in request.args:
|
||||||
|
actions.append(2)
|
||||||
|
if "hidden" in request.args:
|
||||||
|
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"]
|
||||||
|
|
||||||
|
result = db.get_posts_count(artist, actions_taken=actions, last_id= -1, include_ratings= ratings)
|
||||||
|
|
||||||
|
if result is None:
|
||||||
|
response = app.response_class(status=400)
|
||||||
|
else:
|
||||||
|
response = app.response_class(
|
||||||
|
response=str(result),
|
||||||
|
status=200,
|
||||||
|
mimetype='text/plain'
|
||||||
|
)
|
||||||
|
|
||||||
|
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||||
|
return response
|
Loading…
Reference in New Issue