-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB_handler.py
290 lines (287 loc) · 11.9 KB
/
DB_handler.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
from logging import setLoggerClass
from re import S
import re
import pymysql,jsonify,json
from flask import flash, request
from pymysql.cursors import DictCursor
class DBHandler:
def __init__(self, host, user, password, database):
self.host = host
self.user = user
self.password = password
self.database = database
def connection(self):
try:
self.db = pymysql.connect(host=self.host,user=self.user,password=self.password,database=self.database)
self.cursor=self.db.cursor(DictCursor)
# self.cursor.execute("CREATE TABLE IF NOT EXISTS `client` ( `user_id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(20) NOT NULL , `mobile` BIGINT NOT NULL , `city` VARCHAR(20) NOT NULL , `email` VARCHAR(30) NOT NULL , `password` VARCHAR(30) NOT NULL , `isAdmin` BOOLEAN NOT NULL DEFAULT FALSE , PRIMARY KEY (`user_id`, `email`));")
# self.db.commit()
# self.cursor.execute("CREATE TABLE IF NOT EXISTS `worker` ( `worker_id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(20) NOT NULL , `mobile` BIGINT NOT NULL , `city` VARCHAR(20) NOT NULL , `email` VARCHAR(30) NOT NULL , `password` VARCHAR(30) NOT NULL , `title` VARCHAR(30) NOT NULL , `rating` INT NOT NULL DEFAULT '0' , PRIMARY KEY (`worker_id`, `email`));")
# self.db.commit()
# self.cursor.execute("CREATE TABLE IF NOT EXISTS `job` ( `job_id` INT NOT NULL AUTO_INCREMENT , `worker_id` INT NOT NULL , `job_title` VARCHAR(30) NOT NULL , `rate` INT NOT NULL , `description` LONGTEXT NULL , PRIMARY KEY (`job_id`));")
# self.db.commit()
# self.cursor.execute("CREATE TABLE IF NOT EXISTS `requested` ( `job_id` INT NOT NULL , `worker_id` INT NOT NULL , `client_id` INT NOT NULL );")
# self.db.commit()
# self.cursor.execute("CREATE TABLE IF NOT EXISTS `accepted` ( `job_id` INT NOT NULL , `worker_id` INT NOT NULL , `client_id` INT NOT NULL );")
# self.db.commit()
except Exception:
raise Exception("DB Connection Faild")
def close(self):
if not self.cursor==None and not self.db==None:
self.cursor.close()
self.db.close()
def validation(self,status,email,password):
self.connection()
if status == "As Client":
self.cursor.execute("SELECT user_id FROM client WHERE EMAIL = %s AND password = %s ",(email,password))
result = self.cursor.fetchone()
elif status == "As Worker":
self.cursor.execute("SELECT worker_id FROM worker WHERE EMAIL = %s AND password = %s ",(email,password))
result = self.cursor.fetchone()
if result:
self.close()
return result
else:
self.close()
return False
def isAdmin(self,email):
self.connection()
self.cursor.execute("SELECT * FROM client WHERE EMAIL = %s AND isAdmin = %s ",(email,'1'))
result = self.cursor.fetchone()
if result:
self.close()
return True
else:
self.close()
return False
def insertClient(self,name,mobile,city,email,password):
self.connection()
query="INSERT INTO `client`(`name`,`mobile`,`city`,`email`,`password`)VALUES (%s,%s,%s,%s,%s)"
args=(name,mobile,city,email,password)
try:
self.cursor.execute(query,args)
self.db.commit()
except Exception as e:
self.close()
return False
self.close()
return True
def updateClient(self,cid,name,mobile,city,email,password):
self.connection()
query="UPDATE `client` SET `name`= %s,`mobile`=%s,`city`=%s,`email`=%s,`password`= %s where user_id=%s "
args=(name,mobile,city,email,password,cid)
try:
self.cursor.execute(query,args)
self.db.commit()
except Exception as e:
print(e)
self.close()
return False
self.close()
return True
def insertWorker(self,name,mobile,title,city,email,password):
self.connection()
query="INSERT INTO `worker`(`name`,`mobile`,`title`,`city`,`email`,`password`)VALUES (%s,%s,%s,%s,%s,%s)"
args=(name,mobile,title,city,email,password)
try:
self.cursor.execute(query,args)
self.db.commit()
except Exception as e:
self.close()
return False
self.close()
return True
def isClinetExist(self,email):
self.connection()
self.cursor.execute("SELECT * FROM client WHERE EMAIL = %s ",(email))
result = self.cursor.fetchone()
if result:
self.close()
return True
else:
self.close()
return False
def isWorkerExist(self,email):
self.connection()
self.cursor.execute("SELECT * FROM worker WHERE EMAIL = %s ",(email))
result = self.cursor.fetchone()
if result:
self.close()
return True
else:
self.close()
return False
def getjobs(self):
jobList = []
self.connection()
self.cursor.execute("SELECT name,w.worker_id,job_id,email,mobile,job_title,city,rating,rate from worker w,job j where w.worker_id=j.worker_id")
catchData = self.cursor.fetchall()
for item in catchData:
jobList.append(item)
self.close()
return jobList
def getSearchedjobs(self,searchTeaxt):
jobList = []
self.connection()
search = "%{}%".format(searchTeaxt.upper())
self.cursor.execute("SELECT name,w.worker_id,job_id,email,mobile,job_title,city,rating,rate from worker w,job j where w.worker_id=j.worker_id and UPPER(j.job_title) like %s",(search))
catchData = self.cursor.fetchall()
for item in catchData:
jobList.append(item)
self.close()
return jobList
def getWorkerInfo(self,nemail):
self.connection()
self.cursor.execute("SELECT * FROM worker WHERE email=%s",(nemail))
catchData = self.cursor.fetchall()
self.close()
return catchData
def getClientInfo(self,nemail):
self.connection()
self.cursor.execute("SELECT * FROM client WHERE email=%s",(nemail))
catchData = self.cursor.fetchall()
self.close()
return catchData
def getJobDetails(self,id):
self.connection()
self.cursor.execute("SELECT name,w.worker_id,job_id,email,mobile,job_title,city,description,rating,rate from worker w,job j where w.worker_id=j.worker_id and JOB_ID=%s",(id))
catchData = self.cursor.fetchall()
return catchData
def sendRequest(self,jid,wid,cid):
self.connection()
query="INSERT INTO `requested` (`job_id`, `worker_id`, `client_id`) VALUES (%s, %s, %s)"
args=(jid,wid,cid)
try:
self.cursor.execute(query,args)
r=self.db.commit()
except Exception as e:
self.close()
print(e)
return False
self.close()
return True
def getClientId(self,email):
self.connection()
self.cursor.execute("select user_id from client where email=%s",(email))
catchData= self.cursor.fetchone()
return catchData['user_id']
def getWorkerId(self,email):
self.connection()
self.cursor.execute("select worker_id from worker where email=%s",(email))
catchData= self.cursor.fetchone()
return catchData['worker_id']
def getRequestedJobs(self,cid):
jobList = []
self.connection()
self.cursor.execute("SELECT w.name,r.client_id,w.worker_id,j.job_id,w.email,w.mobile,job_title,w.city,rating,rate from worker w,job j,requested r where w.worker_id=r.worker_id and j.job_id=r.job_id and r.client_id=%s",(cid))
catchData = self.cursor.fetchall()
for item in catchData:
jobList.append(item)
self.close()
return jobList
def getConfirmJobs(self,cid):
jobList = []
self.connection()
self.cursor.execute("SELECT w.name,w.email,r.client_id,w.worker_id,j.job_id,w.email,w.mobile,job_title,w.city,rating,rate from worker w,job j,accepted r where w.worker_id=r.worker_id and j.job_id=r.job_id and r.client_id=%s",(cid))
catchData = self.cursor.fetchall()
for item in catchData:
jobList.append(item)
self.close()
return jobList
def checkRequestedJobs(self,cid):
jobList = []
self.connection()
self.cursor.execute("SELECT c.name,c.user_id,c.city,r.worker_id,j.job_id,c.email,c.mobile,job_title,c.city from client c,job j,requested r where c.user_id=r.client_id and j.job_id=r.job_id and r.worker_id=%s",(cid))
catchData = self.cursor.fetchall()
for item in catchData:
jobList.append(item)
self.close()
return jobList
def checkMyJobs(self,cid):
jobList = []
self.connection()
self.cursor.execute("SELECT job_id,job_title,rate,description from job where worker_id=%s",(cid))
catchData = self.cursor.fetchall()
for item in catchData:
jobList.append(item)
self.close()
return jobList
def checkConfirmJobs(self,cid):
jobList = []
self.connection()
self.cursor.execute("SELECT c.name,c.user_id,r.worker_id,j.job_id,c.email,c.mobile,job_title,c.city from client c,job j,accepted r where c.user_id=r.client_id and j.job_id=r.job_id and r.worker_id=%s",(cid))
catchData = self.cursor.fetchall()
for item in catchData:
jobList.append(item)
self.close()
return jobList
def insertNewJob(self,wid,title,rate,desc):
self.connection()
query="INSERT INTO `job`(`worker_id`,`job_title`,`rate`,`description`)VALUES (%s,%s,%s,%s)"
args=(wid,title,rate,desc)
try:
self.cursor.execute(query,args)
self.db.commit()
except Exception as e:
self.close()
return False
self.close()
return True
def cancelRequest(self,worker_id,job_id,client_id):
self.connection()
query="DELETE from requested where job_id=%s and worker_id=%s and client_id=%s"
args=(job_id,worker_id,client_id)
try:
self.cursor.execute(query,args)
self.db.commit()
except Exception as e:
return False
finally:
self.close()
def deletejobP(self,job_id):
self.connection()
query="DELETE from job where job_id=%s"
args=(job_id)
try:
self.cursor.execute(query,args)
self.db.commit()
except Exception as e:
return False
finally:
self.close()
def jobClose(self,worker_id,job_id,client_id,ratings):
self.connection()
self.cursor.execute("SELECT rating from worker where worker_id=%s",(worker_id))
starRate=self.cursor.fetchone()
starRate=starRate["rating"]
finalRate=(int(starRate)+int(ratings))/2
print(finalRate)
query="UPDATE `worker` SET `rating` = %s WHERE `worker`.`worker_id` = %s"
args=(finalRate,worker_id)
try:
self.cursor.execute(query,args)
self.db.commit()
except Exception as e:
return e
query="DELETE from accepted where job_id=%s and worker_id=%s and client_id=%s"
args=(job_id,worker_id,client_id)
try:
self.cursor.execute(query,args)
self.db.commit()
except Exception as e:
return False
finally:
self.close()
def acceptRequest(self,worker_id,job_id,client_id):
self.connection()
query="INSERT INTO `accepted` (`job_id`, `worker_id`, `client_id`) VALUES (%s, %s, %s)"
args=(job_id,worker_id,client_id)
try:
self.cursor.execute(query,args)
r=self.db.commit()
except Exception as e:
self.close()
print(e)
return False
self.close()
return True