-
Notifications
You must be signed in to change notification settings - Fork 5
/
view_animal_profile.py
161 lines (128 loc) · 6.25 KB
/
view_animal_profile.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import tkinter as tk
import time
from PIL import Image, ImageTk
from tkinter import filedialog
from colorama import Fore, Style
from common_functions import clear_screen, log_action, get_mongodb_uri
from sudo_user_login import SudoUser
from tables import print_animal_table_with_index
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']
invalid_input = "Invalid input! Please enter a valid index."
def search_animal_by_name():
# Asks the user for the name of the animal to search for
search_name = input("\nEnter the name of the animal to search for: ")
# Searches the MongoDB collection for animals with the specified name
animals = list(animals_collection.find({"name": search_name, "adopted": False}))
print_animal_table_with_index(animals)
if not animals:
print(Fore.RED + "No animals found with that name." + Style.RESET_ALL)
time.sleep(2)
view_animals_full()
else:
selected_index = input("\nEnter the index of the animal to view its profile: ")
if selected_index == "exit":
print("\nExiting search...")
time.sleep(2)
clear_screen()
view_animal_profile(selected_index, selected_animal)
try:
selected_index = int(selected_index)
if 1 <= selected_index <= len(animals):
selected_animal = animals[selected_index - 1]
view_animal_profile(selected_index, selected_animal)
else:
print(Fore.RED + f"{invalid_input}" + Style.RESET_ALL)
time.sleep(2)
view_animal_profile(selected_index, selected_animal)
except ValueError:
print(Fore.RED + f"{invalid_input}" + Style.RESET_ALL)
time.sleep(2)
view_animal_profile(selected_index, selected_animal)
def view_animal_profile(selected_index, selected_animal):
clear_screen()
if not selected_animal:
print(Fore.RED + "No animal found with that name." + Style.RESET_ALL)
time.sleep(2)
view_animals_full()
return
try:
selected_index = int(selected_index)
if 1 <= selected_index <= len(selected_animal):
animal = selected_animal
# Create a new Tkinter window
root = tk.Tk()
root.title(f"Animal Profile - {animal['name']}")
root.geometry("800x700")
def on_closing():
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
if 'image' in animal:
image = Image.open(animal['image'])
image = image.resize((350, 350))
photo = ImageTk.PhotoImage(image)
image_label = tk.Label(root, image=photo)
image_label.image = photo
image_label.pack(pady=10)
# Create a frame to hold the labels and upload button
frame = tk.Frame(root)
frame.pack(pady=10)
# Display the animal details
tk.Label(frame, text="Name:", font=("Helvetica", 12, "bold")).grid(row=0, column=0, sticky="w")
tk.Label(frame, text=animal['name'], font=("Helvetica", 12)).grid(row=0, column=1, sticky="w")
tk.Label(frame, text="Species:", font=("Helvetica", 12, "bold")).grid(row=1, column=0, sticky="w")
tk.Label(frame, text=animal['species'], font=("Helvetica", 12)).grid(row=1, column=1, sticky="w")
tk.Label(frame, text="Breed:", font=("Helvetica", 12, "bold")).grid(row=2, column=0, sticky="w")
tk.Label(frame, text=animal['breed'], font=("Helvetica", 12)).grid(row=2, column=1, sticky="w")
tk.Label(frame, text="Gender:", font=("Helvetica", 12, "bold")).grid(row=3, column=0, sticky="w")
tk.Label(frame, text=animal['gender'], font=("Helvetica", 12)).grid(row=3, column=1, sticky="w")
tk.Label(frame, text="Age:", font=("Helvetica", 12, "bold")).grid(row=4, column=0, sticky="w")
tk.Label(frame, text=str(animal['age']), font=("Helvetica", 12)).grid(row=4, column=1, sticky="w")
tk.Label(frame, text="Adopted:", font=("Helvetica", 12, "bold")).grid(row=5, column=0, sticky="w")
tk.Label(frame, text=str(animal['adopted']), font=("Helvetica", 12)).grid(row=5, column=1, sticky="w")
# Upload button
def upload_image():
file_path = filedialog.askopenfilename()
if file_path:
# Save the image to the database
animals_collection.update_one({'name': animal['name']}, {'$set': {'image': file_path}})
upload_button = tk.Button(root, text="Upload Image", command=upload_image, width=15, height=2)
upload_button.pack(pady=10)
# Close button
close_button = tk.Button(root, text="Close", command=root.destroy, width=10, height=2, bg="red", fg="white")
close_button.pack(pady=20)
root.mainloop()
else:
print(Fore.RED + f"{invalid_input}" + Style.RESET_ALL)
time.sleep(2)
view_animal_profile(selected_index, selected_animal)
except ValueError:
print(Fore.RED + f"{invalid_input}" + Style.RESET_ALL)
time.sleep(2)
view_animal_profile(selected_index, selected_animal)
def view_animals_full():
clear_screen()
current_user = SudoUser(users_collection.database).login()
while True:
# Continuous loop for viewing the animal options
print(Fore.CYAN + "\n⚙️ Options ⚙️" + Style.RESET_ALL)
print("\n1. " + Fore.GREEN + "Search for animal" + Style.RESET_ALL)
print("2. " + Fore.YELLOW + "Exit" + Style.RESET_ALL)
user_input = input("\nPlease select an option: ")
if user_input == '1':
search_animal_by_name()
elif user_input == '2':
print("\nExiting...")
log_action(current_user, "Exited 'View Profile'")
time.sleep(2)
clear_screen()
return
else:
print("\nInvalid input. Please choose one of the options.")
time.sleep(2)
clear_screen()