GET user settings, plan out API structure more, init serverside org mode parsing

This commit is contained in:
Curt Spark 2024-04-29 16:57:34 +01:00
parent 68c301c95c
commit 770b17c4d1
3 changed files with 87 additions and 8 deletions

51
main.py
View File

@ -53,7 +53,6 @@ def apiInit():
AuthorID INTEGER, AuthorID INTEGER,
CategoryID INTEGER, CategoryID INTEGER,
DatePosted TIMESTAMP, DatePosted TIMESTAMP,
Description VARCHAR(255)
""") """)
dbHandler.initTable(dbConnection, "Categories", """ dbHandler.initTable(dbConnection, "Categories", """
ID SERIAL PRIMARY KEY, ID SERIAL PRIMARY KEY,
@ -121,7 +120,7 @@ def postlogin(body: loginBody, request: Request):
print(msg) print(msg)
return {"success": False, "authToken": None, "message": msg} return {"success": False, "authToken": None, "message": msg}
@app.get("/api/userIDByAuthToken") @app.get("/api/user/IDByAuthToken")
def getuserIDByAuthToken(authToken: Annotated[str | None, Header()] = None): def getuserIDByAuthToken(authToken: Annotated[str | None, Header()] = None):
try: try:
if tokenHandler.validateTokenExistence(dbConnection, authToken): if tokenHandler.validateTokenExistence(dbConnection, authToken):
@ -134,8 +133,8 @@ def getuserIDByAuthToken(authToken: Annotated[str | None, Header()] = None):
print(msg) print(msg)
return {"success": False, "authToken": None, "message": msg} return {"success": False, "authToken": None, "message": msg}
@app.get("/api/publicInfo/{userID}") @app.get("/api/user/publicInfo/{userID}")
def getpublicInfo(userID: int): def getuserPublicInfo(userID: int):
try: try:
if userHandler.checkIDExistence(dbConnection, userID): if userHandler.checkIDExistence(dbConnection, userID):
return { return {
@ -162,22 +161,58 @@ def getpublicInfo(userID: int):
"message": "Get public info failed! Unexpected server error. " + repr(error) "message": "Get public info failed! Unexpected server error. " + repr(error)
} }
@app.get("/api/user/settings/account")
def getuserSettingsAccount(authToken: Annotated[str | None, Header()] = None):
try:
if tokenHandler.validateTokenExistence(dbConnection, authToken):
userID = userHandler.getIDByAuthToken(dbConnection, authToken)
return {
"success": True,
"username": userHandler.getUserInfoByID(dbConnection, userID, "username"),
"firstName": userHandler.getUserInfoByID(dbConnection, userID, "firstname"),
"lastName": userHandler.getUserInfoByID(dbConnection, userID, "lastname"),
"message": "Get user settings succeeded!"
}
else:
return {
"success": False,
"username": None,
"firstName": None,
"lastName": None,
"message": "Get user settings failed! authToken provided is not valid."
}
except Exception as error:
return {
"success": False,
"username": None,
"firstName": None,
"lastName": None,
"message": "Get user settings failed! Unexpected server error. " + repr(error)
}
# GET # GET
# /api/userByAuthToken # /api/user/ByAuthToken
# - userID # - userID
# /api/publicInfo/{userID} # /api/user/publicInfo/{userID}
# - username # - username
# - firstname # - firstname
# - lastname # - lastname
# - profile picture # - profile picture
# - location # - location
# - public email (For contact) # - public email (For contact)
# /api/privateInfo/{userID} # /api/user/privateInfo/{userID}
# - private email (For authentication/login) # - private email (For authentication/login)
# /api/blog/title
# /api/blog/authorID
# /api/blog/categoryID
# /api/blog/pictureLocation
# /api/blog/description
# /api/blog/datePosted
# POST # POST
# /api/changeInfo/{infotype} # /api/user/changeInfo/{infotype}
@app.get("/api") @app.get("/api")
def getapi(): def getapi():

43
orgHandler.py Normal file
View File

@ -0,0 +1,43 @@
from orgparse import load
debug: bool = True
def debugPrint(msg: str) -> None:
if debug:
print("(ORG HANDLER) PRINT: " + msg)
def checkAndRetrieveMetadata(fileData, metadataName: str):
line = fileData.readline()
metadataFullName = "#+" + metadataName + ":"
if metadataFullName in line:
linetoarray = line.split()
return " ".join(linetoarray[1:])
else:
debugPrint("Could not find " + metadataFullName + " metadata field of document!")
return False
def orgToHTML(filePath: str):
try:
fileData = open(filePath, 'r')
orgRoot = load(filePath)
Title = checkAndRetrieveMetadata(fileData, "TITLE")
if not Title:
raise Exception("A valid #+TITLE: field is required as the first line of the org page.")
shortDescription = checkAndRetrieveMetadata(fileData, "DESCRIPTION")
if not shortDescription:
debugPrint("No valid description found, will generate a placeholder from the blog itself...")
firstText = orgRoot[1].body
shortDescription = (firstText[:60] + "...") if len(firstText) > 60 else firstText
for node in orgRoot[1:]:
if node.heading:
headingLevel = str(node.level)
print("<h" + headingLevel + ">" + node.heading + "</h" + headingLevel + ">")
if node.body:
print(node.body)
except Exception as error:
debugPrint("Error parsing org! " + repr(error))
orgToHTML("./test.org")

View File

@ -7,6 +7,7 @@ click==8.1.7
fastapi==0.110.1 fastapi==0.110.1
h11==0.14.0 h11==0.14.0
idna==3.7 idna==3.7
orgparse==0.4.20231004
psycopg2-binary==2.9.9 psycopg2-binary==2.9.9
pycparser==2.22 pycparser==2.22
pydantic==2.7.0 pydantic==2.7.0