-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_query.py
379 lines (334 loc) · 12.5 KB
/
db_query.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
import psycopg2
import json
from datetime import datetime
import random
import string
def connection():
connection = psycopg2.connect(user = "postgres",
<<<<<<< HEAD
password = "Garg@9406608047",
=======
password = "4597",
>>>>>>> feb12e48e37b302c0cf56d8bb729281ebc99ac9f
host = "127.0.0.1",
port = "5432",
dbname = "eMandi")
return connection
#################### REGISTER USER ####################
def registerUser(user):
try:
<<<<<<< HEAD
connection = psycopg2.connect(user = "postgres",
password = "Garg@9406608047",
host = "127.0.0.1",
port = "5432",
dbname = "eMandi")
=======
connection = connection()
>>>>>>> feb12e48e37b302c0cf56d8bb729281ebc99ac9f
cursor = connection.cursor()
if user['usertype']=='Farmer':
cursor.execute("INSERT INTO \"Farmer\" (f_username,farmer_name,f_phone,f_email,f_password,farmer_loc,farmer_city,farmer_state) Values (%s,%s,%s,%s,crypt(%s,gen_salt('bf')),%s,%s,%s)", (user['username'],user['fname'],str(user['phno']),user['emailid'],user['password'],user['location'],user['city'],user['state']))
else:
cursor.execute("INSERT INTO \"Buyer\" (b_username,buyer_name,b_phone,b_email,b_password,buyer_loc,buyer_city,buyer_state) Values (%s,%s,%s,%s,crypt(%s,gen_salt('bf')),%s,%s,%s)", (user['username'],user['fname'],str(user['phno']),user['emailid'],user['password'],user['location'],user['city'],user['state']))
print("is in final")
#closing database connection.
if(connection):
connection.commit()
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
return 200, "OK"
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
#################### CHECK USER LOGIN ####################
def checkUser(user):
try:
connect = connection()
cursor = connect.cursor()
if user['usertype']=='Farmer':
cursor.execute("SELECT * FROM \"Farmer\" WHERE f_email=%s AND f_password = crypt(%s, f_password)", (user['emailid'],user['password']))
else:
cursor.execute("SELECT * FROM \"Buyer\" WHERE b_email=%s AND b_password = crypt(%s, b_password)", (user['emailid'],user['password']))
record = cursor.fetchall()
#closing database connection.
if(connect):
cursor.close()
connect.close()
if len(record) == 0:
return 404, "empty"
return 200, record[0]
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
#################### RESET PASS ####################
def resetPassword(resetdetails):
try:
connection = connection()
cursor = connection.cursor()
if resetdetails['password']==None:
return 404, "password not found"
cursor.execute("""UPDATE user_details SET password = crypt(%s,gen_salt('bf')) WHERE username=%s""",(resetdetails['password'],resetdetails['username']))
number_of_rows_changed = cursor.rowcount
if number_of_rows_changed==0:
return 404,"user not found"
if(connection):
connection.commit()
cursor.close()
connection.close()
return 200, "updated"
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
#################### AUDITOR LOGIN ####################
def AuditorLogin(user):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("SELECT * From public.\"Auditor\" WHERE a_email=%s and a_password=crypt(%s,a_password)", (user['emailid'],user['password']))
record = cursor.fetchall()
#closing database connection.
if(connect):
cursor.close()
connect.close()
if len(record) == 0:
return 404, "empty"
return 200, record[0]
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
#################### INSERT CROP ####################
def insert_crop(game):
try:
connect = connection()
cursor = connect.cursor()
crop_id = ''.join(random.choices(string.ascii_uppercase + string.digits + string.ascii_lowercase, k = 6))
cursor.execute("""INSERT INTO "Crop"("crop_ID", crop_type, crop_region, crop_name, "upload_Date", f_username, crop_weight_kg, crop_img)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""",(crop_id,game['crop_type'],game['crop_region'],game['crop_name'],datetime.today().strftime('%Y-%m-%d'),game['f_username'],game['crop_weight'],game['crop_img']))
record = cursor.rowcount
if(connect):
connect.commit()
cursor.close()
connect.close()
if record!=0:
return 200, crop_id
return 404, 0
except (Exception, psycopg2.Error) as error :
print ("Error in adding crop", error)
return 500, 0
################### TEMP AUDIT ###################
def add_price_crop(crop_id):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("UPDATE \"Crop\" SET final_bid_price=%s, min_bid_price=%s WHERE \"crop_ID\"=%s", (100, 100, crop_id))
if(connect):
cursor.close()
connect.commit()
connect.close()
return 200, 'no error'
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
def close(crop_id):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("SELECT b_username, \"bidAmount\" from \"Auction\" WHERE \"crop_ID\"=%s ORDER BY \"bidAmount\" desc limit 1", (crop_id,))
record = cursor.fetchall()
winner = record[0][0]
cursor.execute("UPDATE \"Crop\" SET b_username=%s WHERE \"crop_ID\"=%s", (winner, crop_id))
if(connect):
cursor.close()
connect.commit()
connect.close()
return winner, 'no error'
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
#################### CROP SEARCH FOR DISPLAY ####################
def searchCrop(cropID='%'):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("SELECT * FROM \"Crop\" WHERE \"crop_ID\" like %s",(cropID,))
record = cursor.fetchall()
#closing database connection.
if(connect):
cursor.close()
connect.close()
if len(record)!=0:
return 200,record
return 404, "No Such Crops"
except (Exception, psycopg2.Error) as error :
print ("Error in crop searching", error)
return 500,error
#################### MY CROPS ####################
def myCrops(uname, utype):
try:
connect = connection()
cursor = connect.cursor()
if utype=='Farmer':
cursor.execute("SELECT * From \"Crop\" WHERE f_username=%s",(uname,))
else:
cursor.execute("SELECT * FROM \"Crop\" WHERE \"Crop\".\"crop_ID\" in (SELECT \"crop_ID\" from \"Auction\" where b_username=%s);",(uname,))
record = cursor.fetchall()
if(connect):
cursor.close()
connect.close()
if len(record)!=0:
return 200,record
return 404, "No Such Crops"
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
#################### INSERT BID ####################
def insertBid(bid):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("INSERT INTO \"Auction\"( \"bidAmount\", b_username, bid_time, \"crop_ID\") VALUES (%s, %s, %s, %s)", (bid['bid_price'],bid['username'],datetime.now(), bid['crop_id']))
cursor.execute("UPDATE \"Crop\" SET final_bid_price=%s WHERE \"crop_ID\"=%s", (bid['bid_price'], bid['crop_id']))
if(connect):
cursor.close()
connect.commit()
connect.close()
return 200, 'no error'
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
#################### FINAL BIDDER ####################
def finalBidderName(crop_id):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("SELECT b_username FROM \"Crop\" WHERE \"crop_ID\"=%s",(crop_id,))
record = cursor.fetchall()
if(connect):
cursor.close()
connect.close()
if len(record)!=0:
return 200,record[0]
return 404, "no user found"
except (Exception, psycopg2.Error) as error :
print ("Error in purchase", error)
return 500,error
#################### TRANSACTION ####################
def insertTransaction(transaction):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("""INSERT INTO "Transaction"("crop_ID", b_username, order_id, pay_date) VALUES (%s, %s, %s, %s);""",(transaction['crop_id'],transaction['username'],transaction['order_id'],transaction['selling_date']))
record = cursor.rowcount
if record!=0:
if(connect):
connect.commit()
cursor.close()
connect.close()
return 200,record
except (Exception, psycopg2.Error) as error :
print ("Error in transaction table", error)
return 500,False
#################### PAYMENT CHECK ####################
def is_Paid(uname, crop_id):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("SELECT * FROM \"Transaction\" WHERE \"crop_ID\" = %s", (crop_id,))
record = cursor.fetchall()
if(connect):
cursor.close()
connect.close()
if len(record) == 0:
return 200, False
return 200, True
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
#########################################################
def insertCategory(category, gameName):
try:
connection = connection()
cursor = connection.cursor()
cursor.execute("""SELECT gameid FROM identty WHERE game_name = %s""",(gameName,))
gameid = cursor.fetchall()
for cat in category:
cursor.execute("""INSERT INTO category(gameid, cat_name)
Values (%s, %s)""",(gameid[0][0], cat))
if(connection):
connection.commit()
cursor.close()
connection.close()
return 200,True
except (Exception, psycopg2.Error) as error :
print ("Error in inserting category", error)
return 500,False
def updateGame(game):
try:
connection = connection()
cursor = connection.cursor()
cursor.execute("""UPDATE game SET update_link = %s, curr_version=%s
WHERE game_name = %s """,(game['game_link'],game['curr_version'],game['game_name']))
record = cursor.rowcount
if record!=0:
cursor.execute("""SELECT gameid FROM identty WHERE game_name = %s""",(game['game_name'],))
gameid = cursor.fetchall()
cursor.execute("""SELECT emailid FROM user_details
WHERE username in (SELECT username FROM transactions
WHERE gameid=%s)""",(gameid[0][0],))
record = cursor.fetchall()
if(connection):
connection.commit()
cursor.close()
connection.close()
return 200,record
except (Exception, psycopg2.Error) as error :
print ("Error in updating game", error)
return 500,False
def gameDetails(gameName):
try:
connection = connection()
cursor = connection.cursor()
cursor.execute("""SELECT mrp FROM game where game_name=%s""",(gameName,))
record = cursor.fetchall()
if(connection):
cursor.close()
connection.close()
if len(record)!=0:
return 200,record
return 404, "No Such Games"
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
return 500,error
def findGameCategory(gameName):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("SELECT cat_name FROM category WHERE gameid = (SELECT gameid FROM identty WHERE game_name=%s)",(gameName,))
record = cursor.fetchall()
#closing database connection.
print(record)
if(connect):
cursor.close()
connect.close()
if len(record) == 0:
return 404, "empty"
return 200, record
except (Exception, psycopg2.Error) as error :
print ("Error in category", error)
return 500,error
########## Update Crop Quality in Crops table ##################
def updateCropGrade(crop):
try:
connect = connection()
cursor = connect.cursor()
cursor.execute("""UPDATE "Crop" SET crop_grade=%s, "grading_Date"=%s, final_bid_price=%s,min_bid_price=%s, a_username=%s WHERE "crop_ID"=%s""",(crop['crop_grade'],datetime.now(),crop['min_bid_price'],crop['min_bid_price'],crop['a_username'],crop['crop_id']))
if(connect):
connect.commit()
cursor.close()
connect.close()
return 200, None
except (Exception, psycopg2.Error) as error :
print ("Error in update crop", error)
return 500,error