added search APIs
This commit is contained in:
114
app/main.py
114
app/main.py
@@ -1,5 +1,6 @@
|
||||
from typing import Annotated
|
||||
from typing import List, Dict, Tuple
|
||||
from annotated_types import IsDigit
|
||||
from fastapi import FastAPI, File, HTTPException, UploadFile, Request, Form
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
@@ -7,6 +8,7 @@ from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
import pymupdf
|
||||
import json
|
||||
import re
|
||||
|
||||
|
||||
# import os
|
||||
@@ -56,6 +58,97 @@ async def get_file(file_id: str):
|
||||
return FileResponse(f"./app/files/{filename}")
|
||||
|
||||
|
||||
@app.get("/search/lva")
|
||||
async def search_lva(searchterm: str, searchlim: int = 10) -> List[Dict[str, str]]:
|
||||
res = []
|
||||
cur = db.cursor(dictionary=True)
|
||||
if await is_LVID(searchterm):
|
||||
cur.execute(
|
||||
"SELECT lvid,lvname FROM LVAs WHERE lvid LIKE ?", (searchterm + "%",)
|
||||
)
|
||||
res = cur.fetchall()
|
||||
else:
|
||||
cur.execute(
|
||||
"SELECT lvid,lvname FROM LVAs WHERE lvname LIKE ?",
|
||||
("%" + searchterm + "%",),
|
||||
)
|
||||
res = cur.fetchall()
|
||||
if searchlim == 0:
|
||||
return res
|
||||
else:
|
||||
return res[:searchlim]
|
||||
|
||||
|
||||
@app.get("/search/prof")
|
||||
async def search_profs(
|
||||
searchterm: str = "", lvid: str = "", searchlim: int = 10
|
||||
) -> List[Dict[str, str | int]]:
|
||||
res = []
|
||||
cur = db.cursor(dictionary=True)
|
||||
if lvid != "":
|
||||
cur.execute("SELECT id FROM LVAs WHERE LVId=?", (lvid,))
|
||||
lid = cur.fetchall()[0]["id"]
|
||||
cur.execute(
|
||||
"SELECT Profs.id,Profs.name FROM Profs LEFT JOIN LPLink ON Profs.id=LPLink.pid WHERE name like ? AND lid=?",
|
||||
("%" + searchterm + "%", lid),
|
||||
)
|
||||
res = cur.fetchall()
|
||||
cur.execute(
|
||||
"SELECT Profs.id,Profs.name FROM Profs LEFT JOIN LPLink ON Profs.id=LPLink.pid WHERE name NOT like ? AND lid=?",
|
||||
("%" + searchterm + "%", lid),
|
||||
)
|
||||
zw = cur.fetchall()
|
||||
if searchterm != "":
|
||||
cur.execute(
|
||||
"SELECT id,name FROM Profs WHERE name LIKE ?", ("%" + searchterm + "%",)
|
||||
)
|
||||
res += cur.fetchall()
|
||||
res = remove_duplicates(res + zw)
|
||||
if searchlim == 0:
|
||||
return res
|
||||
else:
|
||||
return res[:searchlim]
|
||||
|
||||
|
||||
@app.get(
|
||||
"/search/subcat"
|
||||
) # NOT FULLY TESTED DUE TO INCOMPLETE DATABASE DUE TO INACCEPTABLE FOLDERSTRUCTURE
|
||||
async def search_subcats(
|
||||
searchterm: str = "",
|
||||
lvid: str = "",
|
||||
pid: int | None = None,
|
||||
cat: int | None = None,
|
||||
searchlim: int = 10,
|
||||
) -> List[Dict[str, str | int]]:
|
||||
res = []
|
||||
rest = []
|
||||
cur = db.cursor(dictionary=True)
|
||||
if not (lvid == "" or pid is None or cat is None): # Rest is available
|
||||
cur.execute("SELECT id FROM LVAs WHERE LVId=?", (lvid,))
|
||||
lid = cur.fetchall()[0]["id"]
|
||||
cur.execute(
|
||||
"SELECT id,name FROM SubCats WHERE lid=? AND pid=? AND cat=?",
|
||||
(lid, pid, cat),
|
||||
)
|
||||
rest = cur.fetchall()
|
||||
if searchterm != "": # searchterm is available
|
||||
if not (lvid == "" or pid is None or cat is None):
|
||||
cur.execute(
|
||||
"SELECT id,name FROM SubCats WHERE lid=? AND pid=? AND cat=? AND name LIKE ?",
|
||||
(lid, pid, cat, "%" + searchterm + "%"),
|
||||
)
|
||||
res = cur.fetchall()
|
||||
cur.execute(
|
||||
"SELECT id,name FROM SubCats WHERE name LIKE ?", ("%" + searchterm + "%",)
|
||||
)
|
||||
res += cur.fetchall()
|
||||
res = remove_duplicates(res + rest)
|
||||
if searchlim == 0:
|
||||
return res
|
||||
else:
|
||||
return res[:searchlim]
|
||||
|
||||
|
||||
# @app.post("/files/")
|
||||
# async def create_file(file: Annotated[bytes, File()]):
|
||||
# return {"filesize": len(file)}
|
||||
@@ -165,6 +258,27 @@ def censor_pdf(
|
||||
print("CENSORING DONE")
|
||||
|
||||
|
||||
async def is_LVID(term: str) -> bool:
|
||||
if re.match(r"[a-zA-Z0-9]{3}\.[a-zA-Z0-9]*", term):
|
||||
return True
|
||||
if term.isdigit():
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def remove_duplicates(
|
||||
results: List[Dict[str, str | int]],
|
||||
) -> List[Dict[str, str | int]]:
|
||||
ids = []
|
||||
res = []
|
||||
for result in results:
|
||||
if result["id"] in ids:
|
||||
continue
|
||||
ids.append(result["id"])
|
||||
res.append(result)
|
||||
return res
|
||||
|
||||
|
||||
# async def get_submittion(request: Request):
|
||||
# reqJson = await request.form()
|
||||
# print(reqJson)
|
||||
|
||||
Reference in New Issue
Block a user