-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
388 lines (321 loc) · 14.8 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import hashlib
import json
readPrivileges = {
"patient": ["id","username","personalDetails","sicknessDetails","drugPrescription","labTestPrescription"],
"doctor": ["id","username","personalDetails","sicknessDetails","drugPrescription","labTestPrescription"],
"labStaff": ["id","username","personalDetails","labTestPrescription"],
"pharmacyStaff": ["id","username","personalDetails","drugPrescription"],
"nurse": ["id","username","personalDetails","sicknessDetails","drugPrescription","labTestPrescription"]
}
writePrivileges = {
"patient": [],
"doctor": ["id","username","personalDetails","sicknessDetails","drugPrescription","labTestPrescription"],
"labStaff": [],
"pharmacyStaff": [],
"nurse": []
}
editPrivileges = {
"patient": [],
"doctor": ["personalDetails","sicknessDetails","drugPrescription","labTestPrescription"],
"labStaff": [],
"pharmacyStaff": [],
"nurse": ["personalDetails"]
}
label = {
"id": "ID",
"username": "User Name",
"personalDetails": "Personal Details",
"sicknessDetails": "Sickness Details",
"drugPrescription": "Drug Prescription",
"labTestPrescription": "Lab Test Prescription"
}
# This funtion will use for hashing the password (MD5)
def hashPassword(text):
return hashlib.md5(text.encode("utf-8")).hexdigest()
# Compare given plain password with the hashed password
def comparePasswords(plainPassword, hashedPassword):
return hashPassword(plainPassword) == hashedPassword
# Validate the each data field in a given dictionary
def validate(data):
for key in data:
if (len(data[key]) == 0):
return False
return True
def generateID():
config = open('dataRecords.json', 'r')
records = json.load(config)["dataRecords"]
config.close()
return str(int(records[-1]["id"])+1)
# Signup function
def signup(data):
try:
configuration = open('configuration.json', 'r')
obj = json.load(configuration)
configuration.close()
if (data["userType"] == "1"):
data["userType"] = "patient"
data["privilegeLevel"] = "patient"
elif (data["userType"] == "2"):
data["userType"] = "hospitalStaff"
data["privilegeLevel"] = "doctor"
elif (data["userType"] == "3"):
data["userType"] = "hospitalStaff"
data["privilegeLevel"] = "labStaff"
elif (data["userType"] == "4"):
data["userType"] = "hospitalStaff"
data["privilegeLevel"] = "pharmacyStaff"
else:
data["userType"] = "hospitalStaff"
data["privilegeLevel"] = "nurse"
for record in obj[data["userType"]]:
if (record["username"] == data["username"]):
print("\nError: User name is already exist")
return False
obj[data["userType"]].append(data)
configurationWrite = open('configuration.json', 'w')
configurationWrite.writelines(json.dumps(obj))
configurationWrite.close()
return True
except IOError:
print("\nError: Error with writing to file, try again.")
return False
#Login function
def login(data,loginUser):
try:
config = open('configuration.json', 'r')
userType = ""
if (data["userType"] == "1"):
userType = "patient"
else:
userType = "hospitalStaff"
accounts = json.load(config)[userType]
config.close()
for account in accounts:
if (account["username"] == data["username"]):
if (comparePasswords(data["password"],account["password"])):
loginUser.append(account)
return True
print("\nError: Username or password incorrect")
return False
except:
print("\nError: Error with file system, try again.")
return False
# Patient Record Display function
def displayRecords(loginUser):
if (loginUser[0]["privilegeLevel"] == "patient"):
data = {"username":loginUser[0]["username"]}
else:
username = input('Enter the username of the patient: ').strip()
data = {"username":username}
validationResult = validate(data)
while not validationResult:
print("\nError: Validation Error")
username = input('Enter the username of the patient: ').strip()
data = {"username":username}
validationResult = validate(data)
try:
dataRecord = open('dataRecords.json', 'r')
records = json.load(dataRecord)["dataRecords"]
dataRecord.close()
recordList = []
for record in records:
if (record["username"] == data["username"]):
recordList.append(record)
if (len(recordList) != 0):
print("\nPatient Records of " + data["username"])
print("------------------------------------------------------")
for e in recordList:
print("")
for key in readPrivileges[loginUser[0]["privilegeLevel"]]:
print(label[key] + ": " + e[key])
else:
print("\nError: No Data")
except:
print("\nError: Error with file system, try again.")
# Add New Patient Record function
def addNewRecord(loginUser):
username = input('Enter the username of the patient: ').strip()
personalDetails = input('Enter the personal details of the patient: ').strip()
sicknessDetails = input('Enter the sickness details of the patient: ').strip()
drugPrescription = input('Enter the drug precription: ').strip()
labTestPrescritpion = input('Enter the lab test precription: ').strip()
data = {"id":generateID(),"username":username,"personalDetails":personalDetails,"sicknessDetails":sicknessDetails,"drugPrescription":drugPrescription,"labTestPrescription":labTestPrescritpion}
validationResult = validate(data)
while not validationResult:
print("\nError: Validation Error")
username = input('Enter the username of the patient: ').strip()
personalDetails = input('Enter the personal details of the patient: ').strip()
sicknessDetails = input('Enter the sickness details of the patient: ').strip()
drugPrescription = input('Enter the drug precription: ').strip()
labTestPrescritpion = input('Enter the lab test precription: ').strip()
data = {"id":generateID(),"username":username,"personalDetails":personalDetails,"sicknessDetails":sicknessDetails,"drugPrescription":drugPrescription,"labTestPrescription":labTestPrescritpion}
validationResult = validate(data)
try:
dataRecord = open('dataRecords.json', 'r')
records = json.load(dataRecord)["dataRecords"]
dataRecord.close()
accessList = writePrivileges[loginUser[0]["privilegeLevel"]];
for key in data:
if key not in accessList:
del data[key]
records.append(data)
recordWrite = open('dataRecords.json', 'w')
recordWrite.writelines(json.dumps({"dataRecords":records}))
recordWrite.close()
except:
print("\nError: Error with file system, try again.")
# Edit Patient Record function
def editRecord(loginUser):
sicknessDetails = "temp"
drugPrescription = "temp"
labTestPrescritpion = "temp"
id = input('Enter the id of the patient record: ').strip()
personalDetails = input('Enter the personal details of the patient: ').strip()
if (loginUser[0]["privilegeLevel"] == "doctor"):
sicknessDetails = input('Enter the sickness details of the patient: ').strip()
drugPrescription = input('Enter the drug precription: ').strip()
labTestPrescritpion = input('Enter the lab test precription: ').strip()
data = {"id":id,"personalDetails":personalDetails,"sicknessDetails":sicknessDetails,"drugPrescription":drugPrescription,"labTestPrescription":labTestPrescritpion}
validationResult = validate(data)
while not validationResult:
print("\nError: Validation Error")
id = input('Enter the id of the patient record: ').strip()
personalDetails = input('Enter the personal details of the patient: ').strip()
if (loginUser[0]["privilegeLevel"] == "doctor"):
sicknessDetails = input('Enter the sickness details of the patient: ').strip()
drugPrescription = input('Enter the drug precription: ').strip()
labTestPrescritpion = input('Enter the lab test precription: ').strip()
data = {"id":id,"personalDetails":personalDetails,"sicknessDetails":sicknessDetails,"drugPrescription":drugPrescription,"labTestPrescription":labTestPrescritpion}
validationResult = validate(data)
try:
dataRecord = open('dataRecords.json', 'r')
records = json.load(dataRecord)["dataRecords"]
dataRecord.close()
accessList = editPrivileges[loginUser[0]["privilegeLevel"]];
id = data["id"]
del data["id"]
for key in list(data.keys()):
if key not in accessList:
del data[key]
for record in records:
if (record["id"] == id):
for key in data:
try:
record[key] = data[key]
except:
record[key] = record[key]
break
recordWrite = open('dataRecords.json', 'w')
recordWrite.writelines(json.dumps({"dataRecords":records}))
recordWrite.close()
except:
print("\nError: Error with file system, try again.")
# Logout
def logout(loginUser):
loginUser.clear()
renderLandingView(loginUser)
# Sample Views
# Sign up view
def renderSignUpView(loginUser):
print("\nSign Up Form")
print("------------------------------------------------------")
username = input('\nEnter the username: ').strip()
password = input('Enter the password: ').strip()
print("### System user roles : (Patient: 1, Doctor: 2, Lab Staff: 3, Pharmacy Staff: 4, Nurse: Any number) ###")
userType = input('Enter the user type number from above list: ').strip()
data = {"username":username,"password":hashPassword(password),"userType":userType}
validationResult = validate(data)
while not validationResult:
print("\nError: Validation Error")
username = input('\nEnter the username: ').strip()
password = input('Enter the password: ').strip()
print("### System user roles : (Patient: 1, Doctor: 2, Lab Staff: 3, Pharmacy Staff: 4, Nurse: Any number) ###")
userType = input('Enter the user type number from above list: ').strip()
data = {"username":username,"password":hashPassword(password),"userType":userType}
validationResult = validate(data)
if signup(data):
print('\nSuccess: Register successfull and Login to continue')
else:
print('\nError: Register is not successfull')
renderSignUpView(loginUser)
return
renderLoginView(loginUser)
# Login view
def renderLoginView(loginUser):
print("\nLogin Form")
print("------------------------------------------------------")
username = input('\nEnter the username: ').strip()
password = input('Enter the password: ').strip()
print("### System user roles : (Patient: 1, Doctor: 2, Lab Staff: 3, Pharmacy Staff: 4, Nurse: Any number) ###")
userType = input('Enter the user type number from above list: ').strip()
data = {"username":username,"password":password,"userType":userType}
validationResult = validate(data)
while not validationResult:
print("\nError: Validation Error")
username = input('\nEnter the username: ').strip()
password = input('Enter the password: ').strip()
print("### System user roles : (Patient: 1, Doctor: 2, Lab Staff: 3, Pharmacy Staff: 4, Nurse: Any number) ###")
userType = input('Enter the user type number from above list: ').strip()
data = {"username":username,"password":password,"userType":userType}
validationResult = validate(data)
if login(data,loginUser):
print('\nSuccess: Login successfull')
renderDashboardView(loginUser)
else:
print('\nError: Login is not successfull')
renderLoginView(loginUser)
# Dashboard view
def renderDashboardView(loginUser):
print("\nWelcome "+ loginUser[0]["username"])
print("------------------------------------------------------")
print("### View patient records - 1 ###")
if (loginUser[0]["privilegeLevel"] == "doctor"):
print("### Add a patient record - 2 ###")
if (loginUser[0]["privilegeLevel"] == "doctor" or loginUser[0]["privilegeLevel"] == "nurse"):
print("### Edit a patient record - 3 ###")
print("### Logout From System - 4 ###")
code = input('\nEnter the required functionality number: ').strip()
data = {"function":code}
validationResult = validate(data)
while not validationResult:
print("\nError: Validation Error")
print("### View patient records - 1 ###")
if (loginUser[0]["privilegeLevel"] == "doctor"):
print("### Add a patient record - 2 ###")
if (loginUser[0]["privilegeLevel"] == "doctor" or loginUser[0]["privilegeLevel"] == "nurse"):
print("### Edit a patient record - 3 ###")
print("### Logout From System - Press Any Other Key ###")
code = input('\nEnter the required functionality number: ').strip()
data = {"function":code}
validationResult = validate(data)
if (data["function"] == "1"):
displayRecords(loginUser)
elif (data["function"] == "2" and loginUser[0]["privilegeLevel"] == "doctor"):
addNewRecord(loginUser)
elif (data["function"] == "3" and (loginUser[0]["privilegeLevel"] == "doctor" or loginUser[0]["privilegeLevel"] == "nurse")):
editRecord(loginUser)
else:
logout(loginUser)
return
renderDashboardView(loginUser)
# Landing view
def renderLandingView(loginUser):
print("\nWelcome to Hospital Management System")
print("------------------------------------------------------")
print("### Sign Up - 1 ###")
print("### Login - Any Key ###")
code = input('\nEnter the required functionality number: ').strip()
data = {"function":code}
validationResult = validate(data)
while not validationResult:
print("### Sign Up - 1 ###")
print("### Login - Any Key ###")
code = input('\nEnter the required functionality number: ').strip()
data = {"function":code}
validationResult = validate(data)
if (data["function"] == "1"):
renderSignUpView(loginUser)
else:
renderLoginView(loginUser)
loginUser = []
renderLandingView(loginUser)