-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
204 lines (182 loc) · 7.75 KB
/
admin.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import re, os
from datetime import datetime
from commons import calculate_age, signup
# Username and Password hashing encryption
# def login(username, password, tries):
# # Hash the user's password with the salt value
# hashed_username = hashlib.sha256((salt + username[0]).encode()).hexdigest()
# hashed_password = hashlib.sha256((salt + password).encode()).hexdigest()
# username = username.split("@")
# with open("texts/users.txt", "r") as file:
# users = file.readlines()
# for user in users:
# user_data = user.strip().split(", ")
# if user_data[0] == hashed_username and user_data[1] == username[1] and user_data[2] == hashed_password:
# user = [username[0], username[1]]
# return user
# else:
# tries += 1
# response = ["Incorrect Username or Password, You have {tries} tries remaining!", tries]
def add_lecturer_profile(username):
username, role = username.strip().split("@")
name = input("Enter Name: ")
address = input("Enter address: ")
contact = input("Enter Contact No: ")
email = input("Enter email: ")
try:
with open("texts/lecturers.txt", "a+") as file:
users = file.readlines()
if any(user.strip().split(", ")[0] == username and user.strip().split(", ")[1] == role for user in users):
print("Lecturer Profile already exists")
return "exists"
file.write(f"{username}, {role}, {name}, {address}, {contact}, {email}\n")
print("Lecturer profile added successfully")
return "added"
except FileNotFoundError:
with open("texts/lecturers.txt", "w") as file:
file.write(f"{username}, {role}, {name}, {address}, {contact}, {email}\n")
print("Lecturer Profile added successfully")
except PermissionError:
print("Permission denied. Cannot access the file.")
except Exception as e:
print(f"Error: {e}")
return "error"
def update_lecturer_profile(username):
username, role = username.strip().split("@")
try:
with open("texts/lecturers.txt", "r") as file:
lines = file.readlines()
lecturers = [line.strip().split(", ") for line in lines]
if username not in [lecturer[0] for lecturer in lecturers]:
print("Lecturer not found!")
return "error"
index = [lecturer[0] for lecturer in lecturers].index(username)
lecturer = lecturers[index]
choice = input(f"Do you want to edit the profile of {lecturer[2]}, Username = {lecturer[0]}?").lower()
if choice == "y":
lecturer[1] = input("Enter Role: ")
lecturer[2] = input("Enter Name: ")
lecturer[3] = input("Enter address: ")
lecturer[4] = input("Enter Contact No: ")
lecturer[5] = input("Enter email: ")
lecturer[6] = input("Enter your DOB(dd-mm-YYYY): ")
lecturer[7] = calculate_age(lecturer[6])
lecturer[8] = input("Enter citizenship id: ")
print("... Updating Lecturer profile ...")
os.system("sleep 3")
os.system("clear")
else:
exit()
with open("texts/lecturers.txt", "w") as file:
for lecturer in lecturers:
file.write(", ".join(lecturer) + "\n")
except FileNotFoundError:
print("Lecturer Profile not found!")
except PermissionError:
print("Permission denied. Cannot access the file.")
except Exception as e:
print(f"Error: {e}")
return "success"
def subject_topic():
print("... Displaying Subjects...")
topics = []
subjects = []
index = 1
try:
try:
with open("texts/topics.txt", "r") as file:
for line in file:
topic, subject = line.strip().split("@")
if subject not in subjects:
subjects.append(subject)
except FileNotFoundError:
print("No subjects found. Creating a new subject.")
subject = input("Enter subject name: ")
topic = input("Enter topic name within the subject: ")
subjects.append(subject)
with open("texts/topics.txt", "w") as file:
file.write(f"{topic}@{subject}\n")
for sub in subjects:
print(f"{index}. {sub}")
index += 1
print("1. Add subject")
print("2. Add Topic within Subject")
print("3. Exit")
choice = int(input("Choose: "))
if choice == 1:
subject = input("Enter subject name: ")
if subject not in subjects:
subjects.append(subject)
print("Add topic for the subject(Require after creating subject).")
topic = input("Enter topic name: ")
topic = f"{topic}@{subject}"
topics.append(topic)
elif choice == 2:
topic = input("Enter topic name: ")
print("Choose Subjects from below")
index = 1
for subject in subjects:
print(f"{index}. {subject}")
index += 1
choice = int(input("Enter Subject no: ")) - 1
topic = f"{topic}@{subjects[choice]}"
topics.append(topic)
else:
exit()
with open("texts/topics.txt", "a") as file:
for topic in topics:
file.write(f"{topic}\n")
print("Topic/s Registered Successfully!")
return "success"
except PermissionError:
print("Permission denied. Cannot access the file.")
except Exception as e:
print(f"Error: {e}")
def delete_user(username):
users = []
lecturers = []
username, role = username.strip().split("@")
# Read users.txt and store the data in a list
with open("texts/users.txt", "r") as file:
for line in file:
user, password, role = line.strip().split(", ")
users.append({"username": user, "password":password, "role": role})
# Find the user to delete
user_to_delete = None
for user in users:
if (user["username"] == username and user["role"] == role):
user_to_delete = user
break
# Check if the user exists
if user_to_delete is None:
print("User not found.")
return "notFound"
choice = input(f"Are you sure you want to remove {username}@{role}(y/n):").lower()
if choice == 'y':
# Remove the user from the users list
users = [user for user in users if not (user["username"]== username and user["role"]== role)]
# Write the updated users list to users.txt
with open("texts/users.txt", "w") as file:
for user in users:
file.write(f"{user['username']}, {user["password"]}, {user['role']}\n")
# If the user is a lecturer, remove the corresponding line from lecturers.txt
if user_to_delete["role"] == "lecturer":
with open("texts/lecturers.txt", "r") as file:
for lecturer in lecturers:
lecturer = file.readlines().strip().split(", ")
# Find the index of the line to remove
index_to_remove = None
for i, lecturer in enumerate(lecturers):
if user_to_delete["username"] in lecturer:
index_to_remove = i
break
# Remove the line from the lecturers list
if index_to_remove is not None:
del lecturers[index_to_remove]
# Write the updated lecturers list to lecturers.txt
with open("texts/lecturers.txt", "w") as file:
file.writelines(lecturers)
print("User deleted successfully.")
return "success"
else:
print("User not Deleted!")