You've already forked KemoFureApi
							
							queries updated to use dictionaries
This commit is contained in:
		| @@ -9,18 +9,6 @@ from .endpoints.set_action import SetAction, SetActionOld | |||||||
| from .endpoints.posts_count import GetPostsCount, GetPostsCountOld | from .endpoints.posts_count import GetPostsCount, GetPostsCountOld | ||||||
| from .endpoints.account_stats import AccountStats, AccountStatsOld | 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: | class Database: | ||||||
|     db : DatabaseController = None |     db : DatabaseController = None | ||||||
|     app : Flask = None |     app : Flask = None | ||||||
| @@ -55,11 +43,7 @@ class Database: | |||||||
|         SELECT x_handle, id FROM accounts  |         SELECT x_handle, id FROM accounts  | ||||||
|         ORDER BY x_handle ASC''' |         ORDER BY x_handle ASC''' | ||||||
|  |  | ||||||
|         results = self.db.run_query(query) |         return 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 |  | ||||||
|  |  | ||||||
|     def get_account_stats(self): |     def get_account_stats(self): | ||||||
|         query = '''SELECT x_handle, |         query = '''SELECT x_handle, | ||||||
| @@ -73,22 +57,21 @@ FROM x_posts | |||||||
| 	LEFT JOIN x_post_details on x_post_details.id = x_posts.id | 	LEFT JOIN x_post_details on x_post_details.id = x_posts.id | ||||||
| GROUP BY account_id  | GROUP BY account_id  | ||||||
| ORDER BY x_handle''' | ORDER BY x_handle''' | ||||||
|         results = self.db.run_query(query) |         return 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] |  | ||||||
|  |  | ||||||
|     def get_post(self, id): |     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 |         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 |         LEFT JOIN x_post_details | ||||||
|             ON x_posts.id = x_post_details.id |             ON x_posts.id = x_post_details.id | ||||||
|         LEFT JOIN accounts |         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) |         result = self.db.run_query(query) | ||||||
|         if len(result) == 0: |         if len(result) == 0: | ||||||
|             return None |             return None | ||||||
|         else: |         else: | ||||||
|             #return most recent post |             #return most recent post | ||||||
|             result = result[-1] |             return result[-1] | ||||||
|             return get_post_dictionary(result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7]) |  | ||||||
|  |  | ||||||
|     def build_where_query(self, artist, actions_taken = [0, 1, 2, 3], last_id = -1, include_ratings = ["SFW", "NSFW"]): |     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" |         where_query = "WHERE x_post_details.id NOT NULL AND x_posts.error_id = 0" | ||||||
| @@ -106,7 +89,7 @@ ORDER BY x_handle''' | |||||||
|         where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings) |         where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings) | ||||||
|  |  | ||||||
|         query = f''' |         query = f''' | ||||||
|         SELECT count(*) FROM x_posts  |         SELECT count(*) as count FROM x_posts  | ||||||
|         LEFT JOIN x_post_details |         LEFT JOIN x_post_details | ||||||
|             ON x_posts.id = x_post_details.id |             ON x_posts.id = x_post_details.id | ||||||
|         LEFT JOIN accounts |         LEFT JOIN accounts | ||||||
| @@ -114,7 +97,7 @@ ORDER BY x_handle''' | |||||||
|         {where_query}''' |         {where_query}''' | ||||||
|  |  | ||||||
|         result = self.db.run_query(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"): |     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)) |         num_posts = max(1, min(num_posts, 100)) | ||||||
| @@ -133,12 +116,7 @@ ORDER BY x_handle''' | |||||||
|         ORDER BY {order_by} |         ORDER BY {order_by} | ||||||
|         LIMIT {num_posts} OFFSET {offset}''' |         LIMIT {num_posts} OFFSET {offset}''' | ||||||
|  |  | ||||||
|         results = self.db.run_query(query) |         return 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 |  | ||||||
|  |  | ||||||
|     def wrap_query_response(self, result, mode = "json"): |     def wrap_query_response(self, result, mode = "json"): | ||||||
|         if result is None: |         if result is None: | ||||||
|   | |||||||
| @@ -7,16 +7,17 @@ TABLE_X         = "x_posts" | |||||||
| class DatabaseController: | class DatabaseController: | ||||||
|     def __init__(self, db_name): |     def __init__(self, db_name): | ||||||
|         self.conn = sqlite3.connect(db_name) |         self.conn = sqlite3.connect(db_name) | ||||||
|  |         self.conn.row_factory = sqlite3.Row | ||||||
|         self.cursor = self.conn.cursor() |         self.cursor = self.conn.cursor() | ||||||
|  |  | ||||||
|     def run_query(self, query): |     def run_query(self, query): | ||||||
|         try: |         try: | ||||||
|             self.cursor.execute(query) |             self.cursor.execute(query) | ||||||
|             results = self.cursor.fetchall() |             results = [dict(result) for result in self.cursor.fetchall()] | ||||||
|             return results |             return results | ||||||
|         except Exception as e: |         except Exception as e: | ||||||
|             print(e) |             print(e) | ||||||
|             return None |             return [] | ||||||
|          |          | ||||||
|     def run_command(self, command): |     def run_command(self, command): | ||||||
|         try: |         try: | ||||||
|   | |||||||
							
								
								
									
										27
									
								
								modules/Archive/endpoints/new_query.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								modules/Archive/endpoints/new_query.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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"] |         db : Database = app.databases["Archive"] | ||||||
|  |  | ||||||
|         result = db.db.run_query(query) |         result = db.db.run_query(query) | ||||||
|  |         result = [list(d.values()) for d in result] | ||||||
|  |  | ||||||
|         return db.wrap_query_response(result) |         return db.wrap_query_response(result) | ||||||
|      |      | ||||||
| @@ -23,5 +24,6 @@ class QueryOld(Resource): | |||||||
|         db : Database = app.databases["ArchiveOld"] |         db : Database = app.databases["ArchiveOld"] | ||||||
|  |  | ||||||
|         result = db.db.run_query(query) |         result = db.db.run_query(query) | ||||||
|  |         result = [list(d.values()) for d in result] | ||||||
|  |  | ||||||
|         return db.wrap_query_response(result) |         return db.wrap_query_response(result) | ||||||
		Reference in New Issue
	
	Block a user