Implement account setting modification and saving feature
This commit is contained in:
parent
460cb8ac55
commit
4f780805a8
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
psql --host 172.20.0.10 blorgdb dev
|
||||||
18
dbHandler.py
18
dbHandler.py
|
|
@ -164,6 +164,24 @@ def checkFieldValueExistence(dbConnection: psycopg2.extensions.connection, table
|
||||||
)
|
)
|
||||||
return bool(_execQuery(dbConnection, sanitisedQuery)[0][0])
|
return bool(_execQuery(dbConnection, sanitisedQuery)[0][0])
|
||||||
|
|
||||||
|
def checkRowExistence(dbConnection: psycopg2.extensions.connection, tableName: str, fieldName: str) -> bool:
|
||||||
|
try:
|
||||||
|
debugPrint("Checking if field name " + fieldName + " in table " + tableName + " exists...")
|
||||||
|
sanitisedQuery = sql.SQL("""
|
||||||
|
SELECT EXISTS(
|
||||||
|
SELECT
|
||||||
|
{fieldName}
|
||||||
|
FROM
|
||||||
|
{table}
|
||||||
|
);
|
||||||
|
""").format(
|
||||||
|
table=sql.Identifier(tableName),
|
||||||
|
fieldName=sql.Identifier(fieldName),
|
||||||
|
)
|
||||||
|
return bool(_execQuery(dbConnection, sanitisedQuery)[0][0])
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
def getRowRangeByID(dbConnection: psycopg2.extensions.connection, tableName: str, rangeStart: int, rangeEnd: int, latestRecords = True) -> tuple:
|
def getRowRangeByID(dbConnection: psycopg2.extensions.connection, tableName: str, rangeStart: int, rangeEnd: int, latestRecords = True) -> tuple:
|
||||||
debugPrint("Getting rows from table name " + tableName + " from range " + str(rangeStart) + "-" + str(rangeEnd) + "...")
|
debugPrint("Getting rows from table name " + tableName + " from range " + str(rangeStart) + "-" + str(rangeEnd) + "...")
|
||||||
sanitisedQuery = sql.SQL("""
|
sanitisedQuery = sql.SQL("""
|
||||||
|
|
|
||||||
37
main.py
37
main.py
|
|
@ -314,6 +314,43 @@ def postblogCreate(body: postblogCreateBody):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class putuserSettingsChange(BaseModel):
|
||||||
|
authToken: str
|
||||||
|
newValue: str
|
||||||
|
@app.put("/api/user/settings/change/{settingName}")
|
||||||
|
def putuserSettingsChange(body: putuserSettingsChange, settingName: str):
|
||||||
|
try:
|
||||||
|
if tokenHandler.validateTokenExistence(dbConnection, body.authToken):
|
||||||
|
userID = userHandler.getIDByAuthToken(dbConnection, body.authToken)
|
||||||
|
settingNameLowercase = settingName.lower()
|
||||||
|
if userHandler.checkUserSettingExistence(dbConnection, settingNameLowercase):
|
||||||
|
oldValue = userHandler.getUserInfoByID(dbConnection, userID, settingNameLowercase)
|
||||||
|
changedValue = userHandler.changeUserSettingValue(dbConnection, userID, settingNameLowercase, body.newValue)
|
||||||
|
return {
|
||||||
|
"success": True,
|
||||||
|
"message": "Change user settings succeeded! changed " + settingNameLowercase + " from " + oldValue + " to " + body.newValue + "."
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"message": "Change user settings failed! Setting " + settingName + " does not exist."
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"message": "Change user settings failed! authToken provided is not valid."
|
||||||
|
}
|
||||||
|
except Exception as error:
|
||||||
|
msg = "Change user settings failed! Unexpected server error. " + repr(error)
|
||||||
|
debugPrint(msg)
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"message": msg
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# GET
|
# GET
|
||||||
# /api/user/IDByAuthToken
|
# /api/user/IDByAuthToken
|
||||||
# - userID
|
# - userID
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,9 @@ def checkIDExistence(dbConnection: psycopg2.extensions.connection, userID: int)
|
||||||
def checkUserExistence(dbConnection: psycopg2.extensions.connection, username: str) -> bool:
|
def checkUserExistence(dbConnection: psycopg2.extensions.connection, username: str) -> bool:
|
||||||
return dbHandler.checkFieldValueExistence(dbConnection, "users", "username", username)
|
return dbHandler.checkFieldValueExistence(dbConnection, "users", "username", username)
|
||||||
|
|
||||||
|
def checkUserSettingExistence(dbConnection: psycopg2.extensions.connection, settingName: str) -> bool:
|
||||||
|
return dbHandler.checkRowExistence(dbConnection, "users", settingName)
|
||||||
|
|
||||||
def getHashValueByUserID(dbConnection: psycopg2.extensions.connection, userID: int) -> str:
|
def getHashValueByUserID(dbConnection: psycopg2.extensions.connection, userID: int) -> str:
|
||||||
return dbHandler.getFieldValueByID(dbConnection, "users", userID, "passwordhash")
|
return dbHandler.getFieldValueByID(dbConnection, "users", userID, "passwordhash")
|
||||||
|
|
||||||
|
|
@ -47,3 +50,6 @@ def getIDByAuthToken(dbConnection: psycopg2.extensions.connection, authToken: st
|
||||||
authToken=sql.Literal(authToken)
|
authToken=sql.Literal(authToken)
|
||||||
)
|
)
|
||||||
return int(dbHandler._execQuery(dbConnection, sanitisedQuery)[0][0])
|
return int(dbHandler._execQuery(dbConnection, sanitisedQuery)[0][0])
|
||||||
|
|
||||||
|
def changeUserSettingValue(dbConnection: psycopg2.extensions.connection, userID: int, userField: str, newValue) -> str:
|
||||||
|
return dbHandler.changeFieldValueByID(dbConnection, "users", userID, userField, newValue)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue