-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
367 lines (304 loc) · 12.6 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
try:
import mysql.connector as sqltor
import PySimpleGUI as sg
import os
import webbrowser
import matplotlib.pyplot as plt
from googlesearch import search
from Layouts import *
from mysql_conection import *
except (ModuleNotFoundError, ImportError):
import sys, subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "matplotlib"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "google"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "PySimpleGUI"])
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "mysql-connector-python"]
)
import PySimpleGUI as sg
import mysql.connector as sqltor
import os
import webbrowser
import matplotlib.pyplot as plt
from googlesearch import search
## Error handling for importing modules
sg.theme("BluePurple")
## To set theme of window
if os.path.isfile("temp.txt"):
## Checking for previously saved passwords
file = open("temp.txt", "r")
string = file.readline()
user = string.split(":")[1]
string = file.readline()
password = string.split(":")[1]
mycon, mycon = initiate_connection(user, password)
file.close()
else:
window1 = sg.Window(title="MYSQL Login Page", layout=Login_Layout)
## Using PySimpleGUI to create user interface for entering sql credentials
while True:
event, value = window1.read()
if event == sg.WINDOW_CLOSED or event == "Cancel":
quit()
elif event == "login":
user = value["username"]
password = value["password"]
if value["--SAVE--"]:
## if user wants to save password
with open("temp.txt", "w") as file:
file.write("User:" + user + "\nPassword:" + password)
try:
mycon, mycur = initiate_connection(user, password)
if mycon.is_connected():
break
except (sqltor.ProgrammingError, sqltor.OperationalError) as e1:
window1.find_element("label").Update(
"Can't connect to server, Check server status and credentials"
)
else:
pass
window1.close()
window2 = sg.Window(title="COVID19 User Finder/Creator", layout=layout2)
while True:
event, value = window2.read()
if event == sg.WINDOW_CLOSED:
break
elif event == "Submit":
data = select_data(mycon, "show databases;")
if ("covid19",) in data:
mycur.execute("use covid19;")
else:
mycur.execute("create database covid19;")
mycur.execute("use covid19;")
data = select_data("show tables;")
if ("meetinginfo",) not in data:
mycur.execute(
"create table MeetingInfo(MeetId int primary key auto_increment,"
"Purpose varchar(20) not null,"
"Date Date not null);"
)
mycur.execute(
"create table MeetingUser(MeetId int not null ,"
"UserId int not null,"
"foreign key(MeetId) references MeetingInfo(MeetId),"
"foreign key (UserId) references user(UserId));"
)
## Checks for existing table (MeetingInfo). If it does not exist, it creates one
try:
mycur.execute(
"insert into meetinginfo(Purpose,Date) values('%s','%s');"
% (value["purpose"], value["Date"])
)
## inserts values entered by user into the table
mycur.execute("select max(MeetId) from MeetingInfo")
for i in mycur.fetchone():
MeetId = i
uid = value["UIs"]
uidlist = [(MeetId, UserID) for UserID in uid.split(",")]
mycur.executemany(
"insert into MeetingUser(MeetId,UserId) values(%s,%s)", uidlist
)
## Insterts value into the table (MeetingUser)
mycon.commit()
except sqltor.Error:
window2.find_element("MeetError").Update(
"There was a problem logging the meet"
)
## Insterts value into table(MeetingUser)
mycon.commit()
mycur.execute(
'select m.meetid from meetinguser m,user n where m.userid=n.userid and n.covidstatus="+ve";'
)
data = mycur.fetchall()
## Selecting meeting IDs of meetings in which covid +ve person was there
data = [i for (i,) in data]
temp = []
for i in range(len(data)):
mycur.execute(
"select m.userid from meetinguser m , user n where m.userid=n.userid and m.meetid=%s"
% data[i]
)
temp.append(mycur.fetchall())
## Selecting user IDs of the the people attending the meetings in which a covid patient was there
mycur.execute("select userid from user where covidstatus='+ve'")
surecases = mycur.fetchall()
surecases = [j for (j,) in surecases]
temp2 = []
for i in temp:
for (j,) in i:
temp2.append(j)
temp = temp2
try:
for i in surecases:
temp.remove(i)
except ValueError:
pass
## Removing those who have been tested +ve for covid
for i in temp:
mycur.execute(
"update user set covidstatus='prob+ve' where (userid=%s and covidstatus='-ve')"
% i
)
## Setting covid status of people who attended meeting with covid patient to prob+ve (prob+ve = probably +ve)
mycon.commit()
elif event == "submit":
## To create new users
data = select_data(mycon, "show databases;")
if ("covid19",) in data:
mycur.execute("use covid19;")
else:
mycur.execute("create database covid19;")
mycur.execute("use covid19;")
## Checking for database covid 19 and if it doesn't exist, it creates one
data = select_data(mycon, "show tables;")
if ("user",) not in data:
mycur.execute(
"create table User(UserID int Primary key auto_increment,"
"Name varchar(18) not null,"
"Phone BIGINT UNIQUE not null,"
"Address varchar(30) not null,"
"CovidStatus varchar(8) not null check (CovidStatus in ('+ve','prob+ve','-ve')))"
)
## Checks if the table User exists or not and if it doesn't exist, it creates one
try:
r_keys = ["+ve", "-ve"]
mycur.execute(
"insert into User(Name,Phone,Address,CovidStatus) values(%s,%s,%s,%s)",
(
value["name"],
value["phone"],
value["address"],
[key for key in r_keys if value[key]][0],
),
)
mycon.commit()
## Inserting personal info. of users into table (User)
except sqltor.IntegrityError:
window2.find_element("UserError").Update(
"A user exists with same phone number."
)
## Helps to restrict duplicacy of data
elif event == "Update":
window2["CreateUser"].Update(visible=False)
window2["UpdateUser"].Update(visible=True)
window2["return"].Update(visible=True)
## Create user is disables and update user is enabled once update button is clicked
elif event == "return":
window2["CreateUser"].Update(visible=True)
window2["UpdateUser"].Update(visible=False)
window2["return"].Update(visible=False)
## Create user is enabled and update user is disabled once return button is clicked
elif event == "update":
option = value["l3option"]
userid = value["UQuery"]
Nvalue = value["NValue"]
mycur.execute("use covid19;")
mycur.execute(
"update user set %s='%s' where UserID='%s'" % (option, Nvalue, userid)
)
mycon.commit()
## Updates info of the user according to the new data provided
elif event == "search":
searchby = value["option"]
query = value["squery"]
mycur.execute("use covid19;")
if query == "":
mycur.execute("select * from user")
else:
mycur.execute("select * from user where %s='%s'" % (searchby, query))
dat = mycur.fetchall()
window2.find_element("list").Update(dat)
## Searches user info according to query entered
elif event == "hospitallist":
pincode = sg.popup_get_text("Enter Pincode", "Chrome")
query = "covid test centres near {}".format(pincode)
res = [i for i in search(query, tld="co.in", num=3, stop=3, pause=2)]
webbrowser.open_new_tab(res[0])
## opens a tab on the internet to show covid testing centers near the pincode entered
elif event == "graph":
try:
graphdata = select_data(
mycon,
"select substring(Address,1,1) as Block,count(*) "
"from user "
"where covidstatus='+ve' "
"group by Block "
"order by Block;",
)
## Counts number of cases per block
blocks = []
cases = []
for (i, j) in graphdata:
blocks.append(i)
cases.append(j)
line = plt.bar(blocks, cases)
plt.xlabel("Blocks")
plt.ylabel("Cases")
for i in range(len(cases)):
plt.annotate(
str(cases[i]), xy=(blocks[i], cases[i]), ha="center", va="bottom"
)
plt.show()
## plots graph of number of cases on Y axis vs Blocks on X axis
except sqltor.Error:
pass
elif event == "Clear Database":
mycur.execute("drop database covid19")
mycon.commit()
## Clears existing data in database covid19
elif event == "Delete Saved Password":
os.remove("temp.txt")
## Deletes saved passwords
elif event == "About...":
string2 = """
Welcome to COVID 19 Case Management Application
This is a RDBMS based system which logs meetings
between Users in the RDBMS
To start,you need to create users in the Create
Users section.You can add as many users you want
and search them in the Search Section
Once a covid +ve person is logged in a meeting with
a non COVID person,The covid -ve person is marked as
prob+ve.You can update any info by clicking the
update button
You can also view a graph based on the block vs.
cases by clicking the graph button """
sg.popup_ok_cancel(string2, title="About...", grab_anywhere=True)
elif event == "Help":
layout3 = [
[
sg.Text("What can I help you with?"),
sg.InputOptionMenu(
values=["Formats", "Graphs", "Inputting Data"],
k="helpoption",
),
],
[sg.Button("Submit", k="help")],
[sg.Text(text="", size=(70, 8), k="L@bel")],
]
window3 = sg.Window(title="Help", layout=layout3)
while True:
event1, value1 = window3.read()
if event1 == sg.WINDOW_CLOSED:
break
elif event1 == "help":
if value1["helpoption"] == "Formats":
window3.find_element("L@bel").Update(
"""The date has to be in YYYY-MM-DD format.Example:2020-09-12\n
The Address should start with Flat no/Plot no. Example:D-128 """
)
elif value1["helpoption"] == "Graphs":
window3.find_element("L@bel").Update(
"The Graph takes data from the Address and no. of cases(hence the block should be the first alphabet)"
"It can be edited using the options provided in the matplotlib library"
)
elif value1["helpoption"] == "Inputting Data":
window3.find_element("L@bel").Update(
"The code handles duplicate data for you so no need to worry about that."
"You need to enter data manually for each entry as there is not automation available."
"The data should follow the formats specified and remember to wait after clicking submit"
)
## helps user to use the application smoothly
# noinspection PyUnboundLocalVariable
window3.close()
window2.close()