This commit is contained in:
2025-02-14 16:07:24 -05:00
parent 00af36703a
commit 60416c493f
19 changed files with 204 additions and 308 deletions

View File

@@ -14,7 +14,7 @@ from .constructors import ValidTopKarmaRequest, ValidKarmaRetrievalRequest,\
class KarmaDB:
"""Karma DB Util"""
def __init__(self):
def __init__(self) -> None:
self.db_path: LiteralString = os.path.join("/", "usr", "local", "share",
"sqlite_dbs", "karma.db")
@@ -36,7 +36,7 @@ class KarmaDB:
'errorText': f'No records for {keyword}',
}
async def get_top(self, n: Optional[int] = 10) -> list[tuple]:
async def get_top(self, n: Optional[int] = 10) -> Optional[list[tuple]]:
"""Get Top n=10 Karma Entries
Args:
n (Optional[int]) = 10: The number of top results to return
@@ -49,7 +49,7 @@ class KarmaDB:
return await db_cursor.fetchall()
except:
traceback.print_exc()
return
return None
async def update_karma(self, granter: str, keyword: str, flag: int) -> Optional[bool]:
"""Update Karma for Keyword
@@ -62,7 +62,7 @@ class KarmaDB:
"""
if not flag in [0, 1]:
return
return None
modifier: str = "score + 1" if not flag else "score - 1"
query: str = f"UPDATE karma SET score = {modifier}, last_change = ? WHERE keyword LIKE ?"
@@ -90,13 +90,14 @@ class KarmaDB:
return True
else:
return False
return False
class Karma(FastAPI):
"""Karma Endpoints"""
def __init__(self, app: FastAPI, util, constants, glob_state): # pylint: disable=super-init-not-called
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
self.app = app
self.util = util
self.constants = constants
self.glob_state = glob_state
self.db = KarmaDB()
self.endpoints: dict = {
@@ -117,12 +118,17 @@ class Karma(FastAPI):
raise HTTPException(status_code=403, detail="Unauthorized")
n: int = 10
if data:
n: int = int(data.n)
if data and data.n:
n = int(data.n)
try:
top10: list[tuple] = await self.db.get_top(n=n)
top10: Optional[list[tuple]] = await self.db.get_top(n=n)
if not top10:
return {
'err': True,
'errorText': 'General failure',
}
return top10
except:
traceback.print_exc()