Skip to content

Commit

Permalink
Merged conditionals + refactored
Browse files Browse the repository at this point in the history
- Merged statements due to the output being the same

- Refactored register to make it easier to read
  • Loading branch information
tylerlight071 committed Feb 7, 2024
1 parent bb8b028 commit 90bfe54
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 61 deletions.
7 changes: 1 addition & 6 deletions customer_adoption_form_dog.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,7 @@ def adopt_dog_form():
garden_fenced = get_input("Is your garden fully fenced? (yes/no): " + Style.RESET_ALL).strip().lower()
garden_access = get_input("How is the garden accessed? " + Style.RESET_ALL).strip()

elif garden == "communal":
garden_size = "N/A"
garden_fenced = "N/A"
garden_access = "N/A"

elif garden == "no":
elif garden == "communal" or garden == "no":
garden_size = "N/A"
garden_fenced = "N/A"
garden_access = "N/A"
Expand Down
109 changes: 54 additions & 55 deletions register.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,64 @@ def register():
clear_screen()

while True:
# Prompt user to enter a username
username = input("\nEnter a username: ").strip()
username = get_username()
if username is None:
continue

if username == "":
clear_screen()
print(Fore.RED + "\nUsername cannot be empty. Please try again." + Style.RESET_ALL)
password = get_password("Enter a password: ")
if password is None:
continue

# Check if username already exists in the database
if users_collection.find_one({'username': username}):
print(Fore.RED + "\nUsername already exists. Please choose another one." + Style.RESET_ALL)
time.sleep(2)
clear_screen()

confirm_password = get_password("Confirm your password: ")
if confirm_password is None:
continue

# Continuous loop for user registration
while True:
# Prompt user to enter and confirm a password
while True:
password = getpass.getpass("Enter a password: ")
if not password.strip():
clear_screen()
print(Fore.RED + "\nPassword cannot be empty. Please try again." + Style.RESET_ALL)
continue
else:
break

while True:
confirm_password = getpass.getpass("Confirm your password: ")
if not confirm_password.strip():
clear_screen()
print(Fore.RED + "\nPassword cannot be empty. Please try again." + Style.RESET_ALL)
continue
else:
break
if password != confirm_password:
print_error_message("\nPasswords do not match. Please try again.")
continue

user_level = get_user_level()
if user_level is None:
continue

# Check if passwords match
if password == confirm_password:
# Prompt user to enter their user level
user_level = input("Enter your user level (1-3): ")

# Validate user level input
if user_level.isdigit() and 1 <= int(user_level) <= 3:
# Hash the password
hashed_password = hash_password(password)
hashed_password = hash_password(password)
users_collection.insert_one({
'username': username,
'hashed_password': hashed_password,
'level': int(user_level)
})

# Insert user data into the MongoDB collection
users_collection.insert_one({
'username': username,
'hashed_password': hashed_password,
'level': int(user_level)
})
print(Fore.GREEN + "\nRegistration successful!" + Style.RESET_ALL)
time.sleep(2)
clear_screen()
return

print(Fore.GREEN + "\nRegistration successful!" + Style.RESET_ALL)
time.sleep(2)
clear_screen()
return
else:
print(Fore.RED + "\nInvalid user level. Please enter a number between 1 and 3." + Style.RESET_ALL)
else:
clear_screen()
print(Fore.RED + "\nPasswords do not match. Please try again." + Style.RESET_ALL)
def get_username():
username = input("\nEnter a username: ").strip()
if username == "":
print_error_message("\nUsername cannot be empty. Please try again.")
return None
if users_collection.find_one({'username': username}):
print_error_message("\nUsername already exists. Please choose another one.")
time.sleep(2)
clear_screen()
return None
return username

def get_password(prompt):
password = getpass.getpass(prompt)
if not password.strip():
print_error_message("\nPassword cannot be empty. Please try again.")
return None
return password

def get_user_level():
user_level = input("Enter your user level (1-3): ")
if not user_level.isdigit() or not 1 <= int(user_level) <= 3:
print_error_message("\nInvalid user level. Please enter a number between 1 and 3.")
return None
return user_level

def print_error_message(message):
clear_screen()
print(Fore.RED + message + Style.RESET_ALL)

0 comments on commit 90bfe54

Please sign in to comment.