-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_mongo.py
268 lines (217 loc) · 7.76 KB
/
db_mongo.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
import pymongo
# from pymongo.common import SERVER_SELECTION_TIMEOUT
# from bson.objectid import ObjectId
# import pandas as pd
# import json
# import recommender
import healthFilter
# from recommender import recommend
from collections import Counter
def initializeMongoClinet():
try:
client = pymongo.MongoClient(host="localhost", port = 27017)
except:
print("Cannot connect to MongoDB")
return client
def getUserOrders(user_id):
client = initializeMongoClinet()
db = client.frs
orders = db.order_data
user_orders = orders.find({'user_id' : user_id})
for order in user_orders:
print(order)
# def getUserData(user_id):
# client = initializeMongoClinet()
# db = client.frs
# users = db.user_data
# user = users.find_one({'_id':user_id})
# print(user)
# return user
def login(email, password):
client = initializeMongoClinet()
db = client.frs
users = db.user_data
userData = users.find_one({'Email':email, 'Password': password})
if userData:
userData['privelege'] = 'U'
return userData
else:
return {'Message' : 'Invalid Credentials'}
def getRestaurants(city):
client = initializeMongoClinet()
db = client.frs
restaurants = db.restaurant
return list(restaurants.find({'city':city}))
def getDishes(rest_id):
client = initializeMongoClinet()
db = client.frs
dishes = db.dish.find({'Restaurant Id':rest_id})
return [
{
'dish_id' : i['_id'],
'name': i['name'],
'cost': i['cost'],
'discount_cost':i['discount_cost'],
'rating' : i['aver_rate'],
'image_url' : i['image_url']
} for i in dishes
]
def getDishDetails(dish_id):
client = initializeMongoClinet()
db = client.frs
dish = db.dish.find_one({'_id' : dish_id})
rest_id = dish['Restaurant Id']
dish['restaurant_name'] = db.restaurant.find_one({'_id' : rest_id})['Name']
dish['ingredients'] = dish['ingredients'].split('^')
dish.pop('reviews')
dish.pop('cooking_directions')
dish.pop('nutritions')
return dish
def getDishRecommendations(user_id):
client = initializeMongoClinet()
db = client.frs
user_based = None
health_based = None
if user_id != 0:
# dishes = recommender.recommend_user_specific(user_id, 5)
recommendations = healthFilter.health_userbased(user_id, 6)
user_based = recommendations['user_based']
health_based = recommendations['health_based']
popular_list = list(db.dish.find().sort('aver_rate', pymongo.DESCENDING).limit(6))
trending_list = list(db.dish.find().sort('review_nums', pymongo.DESCENDING).limit(6))
if user_based:
user_list = []
for dish_id in user_based:
dish = db.dish.find_one({'_id' : dish_id})
dish.pop('cooking_directions')
dish.pop('reviews')
dish.pop('nutritions')
dish['restName'] = db.restaurant.find_one({'_id' : dish['Restaurant Id']})['Name']
user_list.append(dish)
if health_based:
health_list = []
for dish_id in health_based:
dish = db.dish.find_one({'_id' : dish_id})
dish.pop('cooking_directions')
dish.pop('reviews')
dish.pop('nutritions')
dish['restName'] = db.restaurant.find_one({'_id' : dish['Restaurant Id']})['Name']
health_list.append(dish)
for dish in popular_list:
dish.pop('cooking_directions')
dish.pop('reviews')
dish.pop('nutritions')
dish['restName'] = db.restaurant.find_one({'_id' : dish['Restaurant Id']})['Name']
for dish in trending_list:
dish.pop('cooking_directions')
dish.pop('reviews')
dish.pop('nutritions')
dish['restName'] = db.restaurant.find_one({'_id' : dish['Restaurant Id']})['Name']
if user_id != 0:
return {
'user_based' : user_list,
'health_based' : health_list,
'popular' : popular_list,
'trending_list' : trending_list
}
else:
return {
'popular' : popular_list,
'trending_list' : trending_list
}
def getUserOrders(user_id):
client = initializeMongoClinet()
db = client.frs
orders = [ dict(i) for i in db.order.find({'user_id' : user_id}) ]
# print(list(orders))
for order in orders:
# print(order)
order['restName'] = db.restaurant.find_one({'_id':order['restaurant_id']})['Name']
order['dishName'] = db.dish.find_one({'_id':order['dish_id']})['name']
order['order_datetime'] = order['order_datetime'].strftime('%m/%d/%Y')
# print(list(orders))
return list(orders)
def updateOrder(data):
client = initializeMongoClinet()
db = client.frs
order = db.order
last_id = list(order.find().sort('_id', pymongo.DESCENDING).limit(1))[0]['_id']
data['_id'] = last_id + 1
order.insert_one(data)
order.update(data, {
'$currentDate': {
'order_datetime': { '$type': 'date' }
}
},upsert=True)
def getLatestOrderId(user_id):
client = initializeMongoClinet()
db = client.frs
order = db.order
last_id = list(order.find({'user_id':user_id}).sort('_id', pymongo.DESCENDING).limit(1))[0]['_id']
return last_id
def updateRating(order_id, rating):
client = initializeMongoClinet()
db = client.frs
order = db.order
order.update_one(
{'_id': order_id},
{
"$set" : {'rating': rating}
}
)
return {"Message" : "Updated SUccessfully"}
def updateOrderStatus(order_id, status):
client = initializeMongoClinet()
db = client.frs
order = db.order
order.update_one(
{'_id': order_id},
{
"$set" : {'status': status}
}
)
return {"Message" : "Updated SUccessfully"}
def getUserStats(user_id):
client = initializeMongoClinet()
db = client.frs
orders = db.order.find({"user_id": user_id})
dish_dict = {}
rest_dict = {}
no_of_orders = 0
ratings_count = 0
for order in orders:
if order['dish_id'] not in dish_dict.keys():
dish_dict[order['dish_id']] = 1
else:
dish_dict[order['dish_id']] += 1
if order['restaurant_id'] not in rest_dict.keys():
rest_dict[order['restaurant_id']] = 1
else:
rest_dict[order['restaurant_id']] += 1
if order['rating'] > 0:
ratings_count += 1
no_of_orders += 1
print(dish_dict)
print(rest_dict)
dish_dict = {k: v for k, v in sorted(dish_dict.items(), key=lambda item: item[1], reverse=True)}
rest_dict = {k: v for k, v in sorted(rest_dict.items(), key=lambda item: item[1], reverse=True)}
most_ordered_dish_id = list(dish_dict.keys())[0]
fav_rest_id = list(rest_dict.keys())[0]
fav_dish = db.dish.find_one({'_id':most_ordered_dish_id})
fav_dish_name = fav_dish['name']
rest_id = fav_dish['Restaurant Id']
fav_dish_rest = db.restaurant.find_one({'_id' : rest_id})['Name']
fav_rest = db.restaurant.find_one({'_id':fav_rest_id})
fav_rest_name = fav_rest['Name']
return {
"no_of_orders" : no_of_orders,
"fav_dish" : fav_dish_name,
"fav_dish_rest" : fav_dish_rest,
"fav_rest" : fav_rest_name,
"rating_count" : ratings_count
}
# getUserOrders(54500)
# print(login('[email protected]', '0000'))
# print(getDishes(304))
# print(len(getRestaurants('Bangalore')))
print(getUserStats(1))