-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadd_animal.py
92 lines (72 loc) · 3.14 KB
/
add_animal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import time
from colorama import Fore, Style
from common_functions import clear_screen, log_action, get_mongodb_uri, load_animal_data
from sudo_user_login import SudoUser
from tables import print_animal_table
from pymongo import MongoClient
# Connect to MongoDB
uri = get_mongodb_uri()
client = MongoClient(uri)
db = client['animal_rescue']
animals_collection = db['animals']
users_collection = db['users']
def add_animal():
animals = load_animal_data(animals_collection)
# Continuous loop for adding animals
while True:
clear_screen()
SudoUser(users_collection.database).login()
# Display header
print(Fore.CYAN + "\n🐾 Add Animal 🐾\n" + Style.RESET_ALL)
print("Enter animal details or type 'exit' to cancel:")
# Input fields for animal data
name = input(Fore.GREEN + "\nName: " + Style.RESET_ALL).strip().capitalize()
# Check if user wants to exit
if name.lower() == 'exit':
print("\nExiting..." + Style.RESET_ALL)
time.sleep(2)
clear_screen()
print_animal_table(animals)
break
species = input(Fore.GREEN + "Species: " + Style.RESET_ALL).strip().capitalize()
breed = input(Fore.GREEN + "Breed: " + Style.RESET_ALL).strip().capitalize()
gender = input(Fore.GREEN + "Gender: " + Style.RESET_ALL).strip().capitalize()
age = input(Fore.GREEN + "Age: " + Style.RESET_ALL).strip()
# Validate input fields
if not all([name, species, breed, gender, age]):
print(Fore.RED + "\nInvalid input. All fields are required." + Style.RESET_ALL)
time.sleep(2)
continue
# Validate gender fields
if gender.lower() not in ["male", "female"]:
print(Fore.RED + "\nInvalid input. Gender must be 'Male' or 'Female'." + Style.RESET_ALL)
time.sleep(2)
continue
# 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)
time.sleep(2)
continue
age = int(age)
clear_screen()
# Make the user verify their identity
current_user = SudoUser(users_collection.database).login()
# Add hashed animal data to the animals collection
animals_collection.insert_one ({
'name': name,
'species': species,
'breed': breed,
'gender': gender,
'age': age,
'adopted': False,
})
# Log the action of adding the animal into the audit file
log_action(current_user, f"Added animal: {name}, {species}, {breed}")
# Confirm successful addition of the animal
print(Fore.GREEN + "\n✨ Animal added successfully! ✨" + Style.RESET_ALL)
log_action(current_user, "Exited 'Add an animal'")
time.sleep(2)
# Ask if the user wants to add another animal
add_another = input(Fore.LIGHTYELLOW_EX + "Do you want to add another animal? (yes/no): " + Style.RESET_ALL)
if add_another.lower() == 'no':
break