colorized warning, success and error messages

This commit is contained in:
Ryan Bakkes 2023-11-11 19:32:20 +01:00
parent 9b34bf0b3f
commit e0edbfefaf
3 changed files with 55 additions and 59 deletions

View File

@ -19,14 +19,7 @@ class User:
user = self.db.loginUser(input_username, hashed_password)
# check if user exists
# if user:
# private_key, public_key = Signature.bytesToKeys(user[0], user[1])
# self.private_key = private_key
# self.public_key = public_key
# self.username = user[2]
# return True
if user:
private_key, public_key = Signature.bytesToKeys(user[0], user[1])
self.private_ser = user[0]
self.public_ser = user[1]
self.username = user[2]
@ -41,7 +34,7 @@ class User:
# check if username is already taken
if self.db.fetchUserByUsername(input_username):
print("Username already taken")
print(f"{UtilityHelper.errorMessage('Username already taken')}")
return False
# create sig for user
@ -63,63 +56,63 @@ class User:
def updatePassword(self):
# Get new password
new_password = getpass('Enter your new password: ')
check_new_password = getpass('Enter your new password again: ')
new_password = getpass("Enter your new password: ")
check_new_password = getpass("Enter your new password again: ")
while new_password != check_new_password:
print('Passwords do not match')
new_password = getpass('Enter your new password: ')
check_new_password = getpass('Enter your new password again: ')
print(f"{UtilityHelper.warningMessage('Passwords do not match')}")
new_password = getpass("Enter your new password: ")
check_new_password = getpass("Enter your new password again: ")
old_password = getpass('Enter your old password: ')
old_password = getpass("Enter your old password: ")
hashed_new_password = UtilityHelper.computeHash(new_password)
hashed_old_password = UtilityHelper.computeHash(old_password)
if self.db.changePassword(self.private_ser, hashed_old_password, hashed_new_password) == True:
print('Password updated')
print(f"{UtilityHelper.successMessage('Password updated')}")
else:
print('Something went wrong while trying to update password..')
print(f"{UtilityHelper.errorMessage('Something went wrong while trying to update password..')}")
def updateAccount(self):
# Get new username
new_username = input('Enter your new username: ')
new_username = input("Enter your new username: ")
# check if username is already taken
if self.db.fetchUserByUsername(new_username):
print("Username already taken")
print(f"{UtilityHelper.errorMessage('Username already taken')}")
return False
if self.db.changeUsername(self.private_ser, new_username) == True:
self.username = new_username
print('Username updated')
print(f"{UtilityHelper.successMessage('Username updated')}")
else:
print('Something went wrong while trying to update username..')
print(f"{UtilityHelper.errorMessage('Something went wrong while trying to update username..')}")
def deleteAccount(self):
while True:
print('Are you sure you want to delete this account? (y/n)\n(this will burn all coins available in this account) (y/n)')
confirm = input('>>: ')
if confirm == 'y' or confirm == 'Y':
print('Deleting account..')
print(f"Are you sure you want to delete this account?\n{UtilityHelper.warningMessage('(this will burn all coins available in this account)')} (y/n)")
confirm = input(">>: ")
if confirm == "y" or confirm == "Y":
print(f"{UtilityHelper.warningMessage('Deleting account..')}")
break
elif confirm == 'n' or confirm == 'N':
elif confirm == "n" or confirm == "N":
return False
else:
print('Invalid input')
print(f"{UtilityHelper.warningMessage('Invalid input')}")
if self.db.deleteUser(self.private_ser) == True:
print('Account deleted')
print(f"{UtilityHelper.successMessage('Account deleted')}")
self.logout()
return True
else:
print('Something went wrong while trying to delete account..')
print(f"{UtilityHelper.errorMessage('Something went wrong while trying to delete account..')}")
return False
def printAccountInfo(self):
print('Username: ' + self.username)
print("Username: " + self.username)
print(self.public_ser)
print(self.private_ser)

View File

@ -56,7 +56,7 @@ class MenuHelper:
# check if choice is in menu
match self.start_menu[choice]:
case "Exit":
print('Exiting system')
print(f"{utilityHelper.blinkMessage('Exiting system')}")
exit()
case "Explore the Blockchain":
@ -69,7 +69,7 @@ class MenuHelper:
if logged_in:
print(f'Welcome {self.user.username}')
return
print("Login failed")
print(f"{utilityHelper.errorMessage('Login failed')}")
self.user = None
case "Sign up":
@ -77,19 +77,19 @@ class MenuHelper:
registered = self.user.register()
utilityHelper.clearScreen()
if registered:
print("Registration successful")
print(f"{utilityHelper.successMessage('Registration successful')}")
new_tx = Tx()
new_tx.createRewardTransaction(self.user.public_ser, self.user.private_ser, "SIGNUP")
if new_tx != False and new_tx.is_valid():
print("Received sign up bonus")
print(f"{utilityHelper.blinkMessage('Received sign up bonus')}")
utilityHelper.saveFile("../data/transaction_pool.dat", new_tx)
return
print("Transaction is invalid")
print("Registration failed")
print(f"{utilityHelper.errorMessage('Transaction is invalid')}")
print(f"{utilityHelper.errorMessage('Registration failed')}")
self.user = None
case _:
print("Wrong input, try again")
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
def runUserMainMenu(self):
utilityHelper.loginStartup(self)
@ -102,7 +102,7 @@ class MenuHelper:
# check if choice is in menu
match self.user_main_menu[choice]:
case "Exit":
print('Exiting system')
print(f"{utilityHelper.blinkMessage('Exiting system')}")
exit()
case "Log out":
@ -116,7 +116,7 @@ class MenuHelper:
self.runUserBlockchainMenu()
case _:
print("Wrong input, try again")
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
def runUserBlockchainMenu(self):
while(self.user):
@ -142,10 +142,10 @@ class MenuHelper:
case "Transfer coins":
new_tx = transactionHelper.transaction(self)
if new_tx != False and new_tx.is_valid():
print("Transaction is valid")
print(f"{utilityHelper.successMessage('Transaction is valid')}")
utilityHelper.saveFile("../data/transaction_pool.dat", new_tx)
continue
print("Transaction is invalid")
print(f"{utilityHelper.errorMessage('Transaction is invalid')}")
case "Check balance":
@ -156,7 +156,7 @@ class MenuHelper:
case "Check the pool":
transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
if len(transactions) == 0:
print("Transaction pool is empty")
print(f"{utilityHelper.warningMessage('Transaction pool is empty')}")
continue
x = 0
for transaction in transactions:
@ -169,7 +169,7 @@ class MenuHelper:
blockHelper.createBlock(self)
case _:
print("Wrong input, try again")
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
def runUserSettingsMenu(self):
while(self.user):
@ -197,7 +197,7 @@ class MenuHelper:
self.user = None
case _:
print("Wrong input, try again")
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
def getMenuInput(self, menu):
self.printMenu(menu)
@ -206,12 +206,12 @@ class MenuHelper:
choice = int(input(">>: "))
except:
utilityHelper.clearScreen()
print("Wrong input, try again")
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
return None
if choice >= len(menu):
utilityHelper.clearScreen()
print("Wrong input, try again")
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
return None
utilityHelper.clearScreen()

View File

@ -13,6 +13,7 @@ class bcolors:
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
BLINK = '\033[5m'
def computeHash(data):
hash = hashlib.sha256()
@ -57,27 +58,29 @@ def loginStartup(self):
blockHelper.validateMinedBlock(self)
# TODO - check if there are any notifcations
def printError(text):
print(f"{bcolors.FAIL}{text}{bcolors.ENDC}")
def errorMessage(text):
return bcolors.FAIL + text + bcolors.ENDC
def printSuccess(text):
print(f"{bcolors.OKGREEN}{text}{bcolors.ENDC}")
def successMessage(text):
return bcolors.OKGREEN + text + bcolors.ENDC
def printWarning(text):
print(f"{bcolors.WARNING}{text}{bcolors.ENDC}")
def warningMessage(text):
return bcolors.WARNING + text + bcolors.ENDC
def printInfo(text):
print(f"{bcolors.OKBLUE}{text}{bcolors.ENDC}")
def infoMessage(text):
return bcolors.OKBLUE + text + bcolors.ENDC
def printBold(text):
print(f"{bcolors.BOLD}{text}{bcolors.ENDC}")
def boldMessage(text):
return bcolors.BOLD + text + bcolors.ENDC
def printUnderline(text):
print(f"{bcolors.UNDERLINE}{text}{bcolors.ENDC}")
def printHeader(text):
print(f"{bcolors.HEADER}{text}{bcolors.ENDC}")
def underlineMessage(text):
return bcolors.UNDERLINE + text + bcolors.ENDC
def headerMessage(text):
return bcolors.HEADER + text + bcolors.ENDC
def blinkMessage(text):
return bcolors.BLINK + text + bcolors.ENDC