queries updated to use dictionaries
This commit is contained in:
parent
16c7792b7f
commit
742b0d0d49
|
@ -1,26 +1,16 @@
|
|||
import json
|
||||
from flask import Flask
|
||||
|
||||
|
||||
from .databaseController import DatabaseController
|
||||
from .endpoints.query import Query, QueryOld
|
||||
from .endpoints.post import GetPost, GetPostOld
|
||||
from .endpoints.posts import GetPosts, GetPostsOld
|
||||
from .endpoints.new_query import NewQuery, NewQueryOld
|
||||
from .endpoints.set_action import SetAction, SetActionOld
|
||||
from .endpoints.posts_count import GetPostsCount, GetPostsCountOld
|
||||
from .endpoints.account_stats import AccountStats, AccountStatsOld
|
||||
|
||||
def get_post_dictionary(id, error_id, action_taken, text, files, date, x_handle, x_rating):
|
||||
if files == "" or files is None:
|
||||
files = []
|
||||
else:
|
||||
files = files.split(',')
|
||||
dict = {"id": id, "id_str": str(id), "error_id": error_id, "action_taken": action_taken, "text": text, "files": files, "date": date, "handle": x_handle, "rating": x_rating}
|
||||
if error_id == -1:
|
||||
del(dict["error_id"])
|
||||
if action_taken == -1:
|
||||
del(dict["action_taken"])
|
||||
return dict
|
||||
|
||||
class Database:
|
||||
db : DatabaseController = None
|
||||
app : Flask = None
|
||||
|
@ -37,6 +27,7 @@ class Database:
|
|||
|
||||
if database_name == "Archive":
|
||||
api.add_resource(Query, "/Archive/Query")
|
||||
api.add_resource(NewQuery, "/Archive/NewQuery")
|
||||
api.add_resource(GetPost, "/Archive/GetPost/<id>")
|
||||
api.add_resource(GetPosts, "/Archive/GetPosts")
|
||||
api.add_resource(GetPostsCount, "/Archive/GetPosts/Count")
|
||||
|
@ -44,6 +35,7 @@ class Database:
|
|||
api.add_resource(SetAction, "/Archive/SetAction")
|
||||
elif database_name == "ArchiveOld":
|
||||
api.add_resource(QueryOld, "/ArchiveOld/Query")
|
||||
api.add_resource(NewQueryOld, "/ArchiveOld/NewQuery")
|
||||
api.add_resource(GetPostOld, "/ArchiveOld/GetPost/<id>")
|
||||
api.add_resource(GetPostsOld, "/ArchiveOld/GetPosts")
|
||||
api.add_resource(GetPostsCountOld, "/ArchiveOld/GetPosts/Count")
|
||||
|
@ -55,11 +47,7 @@ class Database:
|
|||
SELECT x_handle, id FROM accounts
|
||||
ORDER BY x_handle ASC'''
|
||||
|
||||
results = self.db.run_query(query)
|
||||
accounts = []
|
||||
if results is not None:
|
||||
accounts = [{"x_handle": result[0], "id": result[1]} for result in results]
|
||||
return accounts
|
||||
return self.db.run_query(query)
|
||||
|
||||
def get_account_stats(self):
|
||||
query = '''SELECT x_handle,
|
||||
|
@ -73,22 +61,21 @@ FROM x_posts
|
|||
LEFT JOIN x_post_details on x_post_details.id = x_posts.id
|
||||
GROUP BY account_id
|
||||
ORDER BY x_handle'''
|
||||
results = self.db.run_query(query)
|
||||
return [{"x_handle": result[0], "undecided": result[1], "approved": result[2], "deleted": result[3], "hidden": result[4], "invalid": result[5]} for result in results]
|
||||
return self.db.run_query(query)
|
||||
|
||||
def get_post(self, id):
|
||||
query = f'''SELECT x_posts.id, x_posts.error_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
|
||||
LEFT JOIN x_post_details
|
||||
ON x_posts.id = x_post_details.id
|
||||
LEFT JOIN accounts
|
||||
ON x_posts.account_id = accounts.id WHERE x_posts.id = {id}'''
|
||||
ON x_posts.account_id = accounts.id WHERE x_posts.id = {id}
|
||||
LIMIT 1'''
|
||||
result = self.db.run_query(query)
|
||||
if len(result) == 0:
|
||||
return None
|
||||
else:
|
||||
#return most recent post
|
||||
result = result[-1]
|
||||
return get_post_dictionary(result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7])
|
||||
return result[-1]
|
||||
|
||||
def build_where_query(self, artist, actions_taken = [0, 1, 2, 3], last_id = -1, include_ratings = ["SFW", "NSFW"]):
|
||||
where_query = "WHERE x_post_details.id NOT NULL AND x_posts.error_id = 0"
|
||||
|
@ -106,7 +93,7 @@ ORDER BY x_handle'''
|
|||
where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings)
|
||||
|
||||
query = f'''
|
||||
SELECT count(*) FROM x_posts
|
||||
SELECT count(*) as count FROM x_posts
|
||||
LEFT JOIN x_post_details
|
||||
ON x_posts.id = x_post_details.id
|
||||
LEFT JOIN accounts
|
||||
|
@ -114,7 +101,7 @@ ORDER BY x_handle'''
|
|||
{where_query}'''
|
||||
|
||||
result = self.db.run_query(query)
|
||||
return result[0][0]
|
||||
return result[0]["count"]
|
||||
|
||||
def get_posts(self, num_posts, artist, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0, include_ratings = ["SFW", "NSFW"], order = "DESC"):
|
||||
num_posts = max(1, min(num_posts, 100))
|
||||
|
@ -133,12 +120,7 @@ ORDER BY x_handle'''
|
|||
ORDER BY {order_by}
|
||||
LIMIT {num_posts} OFFSET {offset}'''
|
||||
|
||||
results = self.db.run_query(query)
|
||||
posts = []
|
||||
if results is not None:
|
||||
for result in results:
|
||||
posts.append(get_post_dictionary(result[0], -1, result[1], result[2], result[3], result[4], result[5], result[6]))
|
||||
return posts
|
||||
return self.db.run_query(query)
|
||||
|
||||
def wrap_query_response(self, result, mode = "json"):
|
||||
if result is None:
|
||||
|
|
|
@ -7,16 +7,17 @@ TABLE_X = "x_posts"
|
|||
class DatabaseController:
|
||||
def __init__(self, db_name):
|
||||
self.conn = sqlite3.connect(db_name)
|
||||
self.conn.row_factory = sqlite3.Row
|
||||
self.cursor = self.conn.cursor()
|
||||
|
||||
def run_query(self, query):
|
||||
try:
|
||||
self.cursor.execute(query)
|
||||
results = self.cursor.fetchall()
|
||||
results = [dict(result) for result in self.cursor.fetchall()]
|
||||
return results
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return None
|
||||
return []
|
||||
|
||||
def run_command(self, command):
|
||||
try:
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
from __future__ import annotations
|
||||
from flask_restful import Resource
|
||||
from flask import current_app as app, request
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from modules.Archive.database import Database
|
||||
|
||||
class NewQuery(Resource):
|
||||
def post(self):
|
||||
query = request.data.decode("utf-8")
|
||||
|
||||
db : Database = app.databases["Archive"]
|
||||
|
||||
result = db.db.run_query(query)
|
||||
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
class NewQueryOld(Resource):
|
||||
def post(self):
|
||||
query = request.data.decode("utf-8")
|
||||
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
|
||||
result = db.db.run_query(query)
|
||||
|
||||
return db.wrap_query_response(result)
|
|
@ -13,6 +13,7 @@ class Query(Resource):
|
|||
db : Database = app.databases["Archive"]
|
||||
|
||||
result = db.db.run_query(query)
|
||||
result = [list(d.values()) for d in result]
|
||||
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
|
@ -23,5 +24,6 @@ class QueryOld(Resource):
|
|||
db : Database = app.databases["ArchiveOld"]
|
||||
|
||||
result = db.db.run_query(query)
|
||||
result = [list(d.values()) for d in result]
|
||||
|
||||
return db.wrap_query_response(result)
|
Loading…
Reference in New Issue