-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from tylerlight071/Features
User Verification + Audit Trails
- Loading branch information
Showing
12 changed files
with
321 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,72 @@ | ||
import time | ||
from colorama import Fore, Style | ||
from common_functions import clear_screen ,load_data, save_data | ||
from common_functions import clear_screen, load_data, save_data, log_action | ||
from sudo_user import sudo_user | ||
|
||
ANIMAL_DATA_FILE = "animals.json" | ||
|
||
def add_animal(): | ||
# Load animal data from file | ||
animals = load_data(ANIMAL_DATA_FILE) | ||
|
||
# Continuous loop for adding animals | ||
while True: | ||
clear_screen() # Clear the screen before prompting for input | ||
clear_screen() | ||
|
||
name = input("\nEnter the animal's name: ") | ||
if not name.strip(): # Check if the name is empty | ||
print(Fore.RED + "Invalid input. Please enter the animal's name." + Style.RESET_ALL) | ||
input(Fore.GREEN + "Press Enter to continue..."+ Style.RESET_ALL) | ||
continue | ||
# Display header | ||
print(Fore.CYAN + "\n🐾 Add Animal 🐾\n" + Style.RESET_ALL) | ||
|
||
species = input("Enter the animal's species: ") | ||
if not species.strip(): # Check if the species is empty | ||
print(Fore.RED + "Invalid input. Please enter the animal's name." + Style.RESET_ALL) | ||
input(Fore.GREEN + "Press Enter to continue..."+ Style.RESET_ALL) | ||
continue | ||
print("Enter animal details or type 'exit' to cancel:") | ||
|
||
breed = input("Enter the animal's breed: ") | ||
if not breed.strip(): # Check if the breed is empty | ||
print(Fore.RED + "Invalid input. Please enter the animal's name." + Style.RESET_ALL) | ||
input(Fore.GREEN + "Press Enter to continue..."+ Style.RESET_ALL) | ||
continue | ||
#Input fields for animal data | ||
name = input(Fore.CYAN + "Name: " + Style.RESET_ALL).strip() | ||
|
||
gender = input("Enter the animal's gender: ") | ||
if not gender.strip(): # Check if the breed is empty | ||
print(Fore.RED + "Invalid input. Please enter the animal's name." + Style.RESET_ALL) | ||
input(Fore.GREEN + "Press Enter to continue..."+ Style.RESET_ALL) | ||
continue | ||
# Check if user wants to exit | ||
if name.lower() == 'exit': | ||
print(Fore.YELLOW + "\nExiting..." + Style.RESET_ALL) | ||
time.sleep(2) | ||
break | ||
|
||
species = input(Fore.GREEN + "Species: " + Style.RESET_ALL).strip() | ||
breed = input(Fore.GREEN + "Breed: " + Style.RESET_ALL).strip() | ||
gender = input(Fore.GREEN + "Gender: " + Style.RESET_ALL).strip() | ||
age = input(Fore.GREEN + "Age: " + Style.RESET_ALL).strip() | ||
|
||
age = input("Enter the animal's age: ") | ||
if not age.strip(): # Check if the age is empty | ||
print(Fore.RED + "Invalid input. Please enter the animal's name." + Style.RESET_ALL) | ||
input(Fore.GREEN + "Press Enter to continue..."+ Style.RESET_ALL) | ||
# Validate input fields | ||
if not all([name, species, breed, gender, age]): | ||
print(Fore.RED + "\nInvalid input. All fields are required." + Style.RESET_ALL) | ||
input(Fore.GREEN + "Press Enter to continue..." + Style.RESET_ALL) | ||
continue | ||
elif not age.isdigit(): # Check if the age is a valid number | ||
print(Fore.RED + "Invalid input. Please enter the animal's name." + Style.RESET_ALL) | ||
input(Fore.GREEN + "Press Enter to continue..."+ Style.RESET_ALL) | ||
|
||
# Validate age as positive integer | ||
if not age.isdigit() or int(age) <= 0: | ||
print(Fore.RED + "\nInvalid age. Please enter a positive integer." + Style.RESET_ALL) | ||
input(Fore.GREEN + "Press Enter to continue..." + Style.RESET_ALL) | ||
continue | ||
|
||
age = int(age) # Convert age to an integer | ||
age = int(age) | ||
|
||
# Assuming age should be a positive number | ||
if age <= 0: | ||
print(Fore.RED + "Invalid input. Please enter the animal's name." + Style.RESET_ALL) | ||
input(Fore.GREEN + "Press Enter to continue..."+ Style.RESET_ALL) | ||
continue | ||
# Add animals to the data dictionary | ||
animals[name] = { | ||
'name': name, | ||
'species': species, | ||
'breed': breed, | ||
'gender': gender, | ||
'age': age, | ||
'adopted': False | ||
} | ||
|
||
# Make the user verify their identity | ||
current_user = sudo_user() | ||
|
||
animals[name] = {'name': name, 'species': species, 'breed': breed, 'gender': gender, 'age': age, 'adopted': False} | ||
save_data(animals, ANIMAL_DATA_FILE) | ||
print(Fore.GREEN + "\nAnimal added successfully!" + Style.RESET_ALL) | ||
|
||
# Log the action of adding animal into the audit file | ||
log_action(current_user, f"Added animal: {name}") | ||
|
||
# Confirm successful addition of the animal | ||
print(Fore.GREEN + "\n✨ Animal added successfully! ✨" + Style.RESET_ALL) | ||
time.sleep(2) | ||
break # Break the loop after successfully adding the animal | ||
|
||
# Exit the loop after successful addition | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,37 @@ | ||
import json | ||
import os | ||
import datetime | ||
|
||
def clear_screen(): | ||
# Clears the terminal screen based on the OS type | ||
os.system('cls' if os.name == 'nt' else 'clear') | ||
|
||
def load_data(file_name): | ||
# Load data from a JSON file | ||
with open(file_name, 'r') as f: | ||
return json.load(f) | ||
|
||
def save_data(data, file_name): | ||
# Save data to the JSON file with indentation for readability | ||
with open(file_name, 'w') as f: | ||
json.dump(data, f, indent=4) | ||
|
||
def log_action(username, action_description): | ||
# Get the current timestamp | ||
current_time = datetime.datetime.now() | ||
|
||
# Format the log entry | ||
log_entry = f"[{current_time}] - {username}: {action_description}" | ||
|
||
# Define the filepath for the audit log file | ||
log_file_path = "audit_log.txt" | ||
|
||
|
||
# Check if the file exists, if not, create it | ||
if not os.path.exists(log_file_path): | ||
with open(log_file_path, "w"): | ||
pass | ||
|
||
# Append the log entry to the file | ||
with open(log_file_path, "a") as log_file: | ||
log_file.write(log_entry + "\n") |
Oops, something went wrong.