-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
147 lines (110 loc) · 4.27 KB
/
main.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
"""Library management application for libraries.
Library users can access library database through this application and
realize their operations according to the limitations. Library admin can
reach all database and manage the library system. Authentication is
required for operations.
"""
import pymongo # library for mongodb database usage
import yaml # library for yaml operations
from src.classes.user import User
from src.classes.librarian import Librarian
from src.classes.status import Status
# library imports for user, librarian and status classes
with open("db_auth.yaml", encoding="utf8") as file:
data = yaml.safe_load(file)
db_username = data["username"]
db_password = data["password"]
# database authentication parameters
client = pymongo.MongoClient(
f"mongodb+srv://{db_username}:{db_password}@cluster0.yah8b9u.mongodb.net/?retryWrites=true&w=majority"
) # database connection establishing
db = client["mydatabase"]
books_col = db["books"]
users_col = db["users"]
librarian_col = db["librarian"]
# database collection connections
def user_func():
"""
Function to monitoring and managing all user operations
"""
user = User(users_col, books_col)
print("\n1. User Login\n2. User Sign Up")
user_input_type = int(input("Choose an option for continue: "))
if user_input_type == 1:
user_username = input("\nUsername: ")
user_password = input("Password: ")
response = user.login(user_username, user_password)
if response["status"] == Status.Fail:
print(response["message"])
else:
print("\n" + response["message"])
while 1:
print(
"\n1. Search books\n2. Reserve a book \n3. Occupy a book\n4. Return a book \n5. Log out"
)
try:
user_action = int(input("Choose an option for continue: "))
except:
print("Invalid input!")
continue
response = user.operations(user_action)
print(response["message"])
if response["status"] == Status.LogOut:
break
if "data" in response.keys():
print("\nResult: ")
for _data in response["data"]:
print(_data)
input("\nPress enter to continue: ")
elif user_input_type == 2:
user_username = input("\nUsername: ")
user_password = input("Password: ")
response = user.register(user_username, user_password)
print(response["message"])
def librarian_func():
"""
Function to monitoring and managing all librarian operations
"""
librarian = Librarian(librarian_col, books_col, users_col)
librarian_password = input("Password: ")
response = librarian.login(librarian_password)
if response["status"] == Status.Fail:
print(response["message"])
else:
print(response["message"])
while 1:
print(
"\n1. Show All Books\n2. Show All Users\n3. Add a Book\n4. Remove a Book\n5. Log Out"
)
try:
librarian_action = int(input("Choose an option for continue: "))
except:
print("Invalid input!")
continue
response = librarian.operations(librarian_action)
print("\n" + response["message"])
if response["status"] == Status.LogOut:
break
if "data" in response.keys() and (response["status"] != Status.Fail):
print("\nResult: ")
for _data in response["data"]:
print(_data)
input("\nPress enter to continue: ")
while 1:
print("\nWelcome to the Library Management System.")
print("\n1. User Operations\n2. Librarian Operations\n3. Close Application")
try:
input_type = int(input("Choose an option for continue: "))
except:
print("Invalid input!")
continue
if input_type == 1:
user_func()
elif input_type == 2:
librarian_func()
elif input_type == 3:
print("\nApplication is closed. Have a nice day!")
break
else:
print("Invalid input!")
input("\nPress enter to continue: ")