-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
526 lines (411 loc) · 14.8 KB
/
controller.py
File metadata and controls
526 lines (411 loc) · 14.8 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import mysql.connector
import os
import matplotlib.pyplot as pt
from PIL import Image
# Configurations
from config import config
from dotenv import load_dotenv
load_dotenv() # Imports environemnt variables from the '.env' file
# ===================SQL Connectivity=================
# SQL Connection
connection = mysql.connector.connect(
host=config.get("DB_HOST"),
user=config.get("DB_USER"),
password=config.get("DB_PASSWORD"),
database=config.get("DB_NAME"),
port="3306",
autocommit=config.get("DB_AUTOCOMMIT"),
)
cursor = connection.cursor(buffered=True)
# SQL functions
def updateemail(old_username , new_username):
cursor = connection.cursor()
cmd = "UPDATE users SET email = %s WHERE email = %s LIMIT 1;"
cursor.execute(cmd, (new_username, old_username))
connection.commit()
return
def checkUser(username, password=None):
"""
Checks if a user exists in the 'users' table with the given username (email) and password.
Returns the user_id if a match is found.
"""
try:
# Use a parameterized query to securely check the user
if password:
cmd = "SELECT user_id FROM users WHERE email = %s AND BINARY password = %s"
cursor.execute(cmd, (username, password))
user_id = cursor.fetchone()
return user_id[0] if user_id else None
else:
cmd = "SELECT COUNT(*) FROM users WHERE email = %s"
cursor.execute(cmd, (username,))
exists = cursor.fetchone()
return exists[0] > 0
except Exception as e:
print(f"Error checking user: {e}")
return None
def get_unique_pincodes(user_id):
"""
Fetches all unique pincodes from the food_listings table.
Returns:
list: A list of unique pincodes.
"""
try:
query = "SELECT DISTINCT pincode FROM food_listings where user_id != %s"
cursor.execute(query,(user_id))
results = cursor.fetchall()
# Convert fetched results into a simple list of pincodes
pincodes = [row[0] for row in results]
return pincodes
except Exception as e:
print(f"Error fetching pincodes: {e}")
return []
def get_food_items(user_id=None):
"""
Fetches food items from the food_listings table. If user_id is provided,
fetches food items specific to the user.
Args:
user_id (int, optional): The ID of the user to filter food items by.
Returns:
list: A list of dictionaries where each dictionary represents a food item.
"""
try:
if user_id:
query = """
SELECT listing_id, food_type, quantity, location, expiration_date, pincode, date_listed
FROM food_listings
WHERE user_id = %s
"""
cursor.execute(query, (user_id,))
else:
query = """
SELECT listing_id, food_type, quantity, location, expiration_date, pincode, date_listed
FROM food_listings
"""
cursor.execute(query)
food_items = cursor.fetchall()
return food_items
except Exception as e:
print(f"Error retrieving food items: {e}")
return []
def request_pickup(user_id, listing_id, pickup_date, pickup_time):
"""
Saves a pickup request to the pickups table.
Args:
user_id (int): The ID of the user requesting the pickup.
listing_id (int): The ID of the food listing to be picked up.
pickup_date (str): The date for the pickup in 'YYYY-MM-DD' format.
pickup_time (str): The time for the pickup in 'HH:MM' format.
Returns:
bool: True if the pickup request is successfully saved, False otherwise.
"""
try:
# Insert pickup request into the database
query = """
INSERT INTO pickups (user_id, listing_id, pickup_time, status, date_scheduled)
VALUES (%s, %s, %s, %s, NOW())
"""
full_pickup_time = f"{pickup_date} {pickup_time}" # Combine date and time
cursor.execute(query, (user_id, listing_id, full_pickup_time, "pending"))
return True
except Exception as e:
print(f"Error saving pickup request: {e}")
return False
import mysql.connector
def add_food_items_bulk(food_items, user_id):
"""
Adds multiple food items to the database in a single transaction.
Args:
food_items (list): List of dictionaries containing food item details.
user_id (int): The ID of the user adding the items.
Returns:
bool: True if items were added successfully, False otherwise.
"""
try:
# SQL query to insert food items
query = """
INSERT INTO food_listings (food_type, quantity, expiration_date, location, pincode, user_id)
VALUES (%s, %s, %s, %s, %s, %s)
"""
# Prepare the values for bulk insertion
values = [
(
item["food_type"], # Food Type
item["quantity"], # Quantity
item["expiry_date"], # Expiry Date
item["location"], # Location
item["zipcode"], # Zipcode
user_id, # User ID
)
for item in food_items
]
# Execute the query in bulk
cursor.executemany(query, values)
return True # Items added successfully
except mysql.connector.Error as err:
print(f"Database Error: {err}")
return False
def get_listing_details(user_id):
"""
Fetch food listing details for a specific pincode, excluding listings by the current user.
Args:
user_id (int): The ID of the current user.
pincode (str): The selected pincode.
Returns:
dict: A dictionary where the keys are food types and values are tuples of (listing_id, address).
"""
try:
query = """
SELECT food_type, listing_id, location
FROM food_listings
WHERE user_id != %s
"""
cursor.execute(query, ( user_id))
results = cursor.fetchall()
# Populate the dictionary with food type as key and (listing_id, location) as value
listings = {row[0]: (row[1], row[2]) for row in results}
return listings
except Exception as e:
print(f"Error fetching listing details: {e}")
def get_request_details(user_id):
"""
Fetch details of all pickup requests made by the current user.
Args:
user_id (int): The ID of the current user.
Returns:
list: A list of dictionaries where each dictionary represents a request with
details like listing_id, food_type, location, and pickup_time.
"""
try:
query = """
SELECT p.listing_id, fl.food_type, fl.location, p.pickup_time, p.status
FROM pickups AS p
JOIN food_listings AS fl ON p.listing_id = fl.listing_id
WHERE p.user_id = %s
"""
cursor.execute(query, (user_id,))
results = cursor.fetchall()
# Structure the data into a list of dictionaries
requests = [
{
"listing_id": row[0],
"food_type": row[1],
"location": row[2],
"pickup_time": row[3],
"status": row[4],
}
for row in results
]
except Exception as e:
print(f"Error fetching request details: {e}")
return requests
def create_user(first_name, last_name, email, password):
try:
query = """
INSERT INTO users (first_name, last_name, email, password)
VALUES (%s, %s, %s, %s)
"""
cursor.execute(query, (first_name, last_name, email, password))
return True
except Exception as e:
print(f"Error adding User: {e}")
return False
def add_food_item(food_type, quantity, location, zipcode, expiry_date, user_id):
"""
Adds a new food item to the food_listings table.
Args:
food_type (str): Type of the food (e.g., Vegetables, Fruits, etc.).
quantity (int): Quantity of the food.
location (str): Location where the food is available.
zipcode (str): The pincode of the location.
expiry_date (str): The expiration date of the food item (YYYY-MM-DD).
user_id (int): ID of the user adding the food item.
Returns:
bool: True if the item was successfully added, False otherwise.
"""
try:
query = """
INSERT INTO food_listings (food_type, quantity, expiration_date, location, pincode, user_id)
VALUES (%s, %s, %s, %s, %s, %s)
"""
cursor.execute(query, (food_type, quantity, expiry_date, location, zipcode, user_id))
return True
except Exception as e:
print(f"Error adding food item: {e}")
return False
def available_food_count():
"""
Retrieves the total available food quantity from the 'food_listings' table.
"""
try:
cmd = """
SELECT SUM(quantity)
FROM food_listings
WHERE listing_id NOT IN (SELECT listing_id FROM pickups WHERE status = 'approved')
"""
cursor.execute(cmd)
result = cursor.fetchone()[0]
return result if result else 0
except Exception as e:
print(f"Error retrieving available food count: {e}")
return 0
def donated_food_count():
"""
Retrieves the total donated food quantity from the 'food_listings' table.
"""
try:
cmd = "SELECT SUM(quantity) FROM food_listings"
cursor.execute(cmd)
result = cursor.fetchone()[0]
return result if result else 0
except Exception as e:
print(f"Error retrieving donated food count: {e}")
return 0
def get_total_donation_value(average_value_per_unit=5):
"""
Calculates the total monetary value of donated food.
Assumes an average value per unit is provided as a parameter.
"""
try:
total_donated = donated_food_count()
total_value = total_donated * average_value_per_unit
return total_value
except Exception as e:
print(f"Error calculating total donation value: {e}")
return 0
def meals_distributed():
"""
Retrieves the total meals distributed based on approved pickups in the 'pickups' table.
"""
try:
cmd = """
SELECT SUM(f.quantity)
FROM food_listings f
INNER JOIN pickups p ON f.listing_id = p.listing_id
WHERE p.status = 'approved'
"""
cursor.execute(cmd)
result = cursor.fetchone()[0]
return result if result else 0
except Exception as e:
print(f"Error retrieving meals distributed: {e}")
return 0
def human_format(num):
if num < 1000:
return num
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000
return "%.1f%s" % (num, ["", "K", "M", "G", "T", "P"][magnitude])
def updatePassword(email, password):
cmd = f"update users set password='{password}' where email='{email}' limit 1;"
cursor.execute(cmd)
cmd = f"select count(email) from users where email='{email}' and password='{password}';"
cursor.execute(cmd)
return cursor.fetchone()[0] >= 1
def updateUsername(oldusername, password, newusername):
cmd = f"update login set email='{newusername}' where username='{oldusername}' and password='{password}' limit 1;"
cursor.execute(cmd)
cmd = f"select count(username) from login where username='{newusername}' and password='{password}''"
cursor.execute(cmd)
return cursor.fetchone()[0] >= 1
def insert_food_listing(values):
# Prepare the query
query = """
INSERT INTO food_listings_2
(user_id,food_name, category, quantity, serving_size, prepared_date , expiration_date, zipcode, description, special_notes)
VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
"""
try:
# Execute the query
cursor.execute(query, values)
connection.commit()
print("Food listing inserted successfully!")
except mysql.connector.Error as e:
print(f"Error inserting food listing: {e}")
if connection:
connection.rollback()
from mysql.connector import Error
def fetch_listings_query(user_id):
try:
# SQL Query
query = """
SELECT
listing_id, food_type, quantity, expiration_date,
location, pincode
FROM
food_listings
WHERE
user_id = %s
"""
values = (user_id,) # Correctly formatted tuple
# Execute the query
cursor.execute(query, values)
# Fetch the results
results = cursor.fetchall()
return results
except Error as e:
print(f"Error fetching listings: {e}")
raise
finally:
# Close cursor and connection if initialized
if 'cursor' in locals() and cursor:
cursor.close()
if 'connection' in locals() and connection:
connection.close()
def search_listings_query(zipcode,expiration_date,current_user_id):
try:
# SQL Query
query = """
SELECT fl.listing_id, fl.food_type, fl.quantity, fl.expiration_date, fl.location, fl.pincode, fl.user_id
FROM food_listings fl
LEFT JOIN pickups p ON fl.listing_id = p.listing_id AND p.user_id = %s AND p.status = 'pending'
WHERE fl.pincode = %s
AND ((fl.expiration_date BETWEEN CURDATE() AND %s) OR fl.expiration_date IS NULL)
AND fl.user_id != %s
"""
values = (current_user_id,zipcode, expiration_date, current_user_id)
# Execute the query
cursor.execute(query, values)
# Fetch the results
results = cursor.fetchall()
return results
except Error as e:
print(f"Error fetching listings: {e}")
raise
finally:
# Close cursor and connection if initialized
if 'cursor' in locals() and cursor:
cursor.close()
if 'connection' in locals() and connection:
connection.close()
def search_execute_query(query,values):
try:
# SQL Query
# Execute the query
cursor.execute(query, values)
# Fetch the results
results = cursor.fetchall()
return results
except Error as e:
print(f"Error fetching listings: {e}")
raise
finally:
# Close cursor and connection if initialized
if 'cursor' in locals() and cursor:
cursor.close()
if 'connection' in locals() and connection:
connection.close()
def execute_queries(query,values):
try:
# Execute the query
cursor.execute(query, values)
# Commit for non-SELECT queries
connection.commit()
except mysql.connector.Error as e:
print(f"Error executing query: {e}")
if connection:
connection.rollback()
raise