GET user settings, plan out API structure more, init serverside org mode parsing
This commit is contained in:
parent
68c301c95c
commit
770b17c4d1
51
main.py
51
main.py
|
|
@ -53,7 +53,6 @@ def apiInit():
|
|||
AuthorID INTEGER,
|
||||
CategoryID INTEGER,
|
||||
DatePosted TIMESTAMP,
|
||||
Description VARCHAR(255)
|
||||
""")
|
||||
dbHandler.initTable(dbConnection, "Categories", """
|
||||
ID SERIAL PRIMARY KEY,
|
||||
|
|
@ -121,7 +120,7 @@ def postlogin(body: loginBody, request: Request):
|
|||
print(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):
|
||||
try:
|
||||
if tokenHandler.validateTokenExistence(dbConnection, authToken):
|
||||
|
|
@ -134,8 +133,8 @@ def getuserIDByAuthToken(authToken: Annotated[str | None, Header()] = None):
|
|||
print(msg)
|
||||
return {"success": False, "authToken": None, "message": msg}
|
||||
|
||||
@app.get("/api/publicInfo/{userID}")
|
||||
def getpublicInfo(userID: int):
|
||||
@app.get("/api/user/publicInfo/{userID}")
|
||||
def getuserPublicInfo(userID: int):
|
||||
try:
|
||||
if userHandler.checkIDExistence(dbConnection, userID):
|
||||
return {
|
||||
|
|
@ -162,22 +161,58 @@ def getpublicInfo(userID: int):
|
|||
"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
|
||||
# /api/userByAuthToken
|
||||
# /api/user/ByAuthToken
|
||||
# - userID
|
||||
# /api/publicInfo/{userID}
|
||||
# /api/user/publicInfo/{userID}
|
||||
# - username
|
||||
# - firstname
|
||||
# - lastname
|
||||
# - profile picture
|
||||
# - location
|
||||
# - public email (For contact)
|
||||
# /api/privateInfo/{userID}
|
||||
# /api/user/privateInfo/{userID}
|
||||
# - private email (For authentication/login)
|
||||
|
||||
# /api/blog/title
|
||||
# /api/blog/authorID
|
||||
# /api/blog/categoryID
|
||||
# /api/blog/pictureLocation
|
||||
# /api/blog/description
|
||||
# /api/blog/datePosted
|
||||
|
||||
# POST
|
||||
# /api/changeInfo/{infotype}
|
||||
# /api/user/changeInfo/{infotype}
|
||||
|
||||
@app.get("/api")
|
||||
def getapi():
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
@ -7,6 +7,7 @@ click==8.1.7
|
|||
fastapi==0.110.1
|
||||
h11==0.14.0
|
||||
idna==3.7
|
||||
orgparse==0.4.20231004
|
||||
psycopg2-binary==2.9.9
|
||||
pycparser==2.22
|
||||
pydantic==2.7.0
|
||||
|
|
|
|||
Loading…
Reference in New Issue