-
Notifications
You must be signed in to change notification settings - Fork 4
/
database.py
295 lines (220 loc) · 9.26 KB
/
database.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
import pyrebase
import json
class DBhandler:
def __init__(self):
with open('./authentication/firebase_auth.json') as f:
config=json.load(f)
firebase = pyrebase.initialize_app(config)
self.db = firebase.database()
# 회원가입
def insert_user(self, ID, pw):
user_info = {
"id": ID,
"pw": pw,
}
if self.user_duplicate_check(str(ID)):
self.db.child("user").push(user_info)
return True
else:
return False
# id 중복검사
def user_duplicate_check(self, id_string):
users = self.db.child("user").get()
print("users###", users.val())
if str(users.val()) == "None":# first registration
return True
else:
for res in users.each():
value = res.val()
if value['id'] == id_string:
return False
return True
# 리뷰를 작성할 때마다 해당 메뉴 별점, 매장 청결도 갱신
def update_rate(self, restaurant, menu, rating, clean):
info = self.db.child("restaurant").child(restaurant).child("menu").child(menu).get()
value = info.val()
# 메뉴 별점 갱신
rate_update = {}
count = value['review_count']
new_rate = (value['rate']*count + float(rating)) / (count+1)
rate_update['rate'] = new_rate
rate_update['review_count'] = count+1
self.db.child("restaurant").child(restaurant).child("menu").child(menu).update(rate_update)
#매장 청결도 갱신
info = self.db.child("restaurant").child(restaurant).get()
value = info.val()
clean_update = {}
count = value['review_count']
new_clean = (value['clean_rate'] * count + float(clean)) / (count+1)
clean_update['clean_rate'] = new_clean
clean_update['review_count'] = count+1
self.db.child("restaurant").child(restaurant).update(clean_update)
return True
# 리뷰 등록
def insert_review(self, restaurant, time_r, rating, menuName, menu1_rating, spicy, reviewText, img_path_r):
review_info ={
"time": time_r,
"menu1_rating": menu1_rating,
"spicy": spicy,
"reviewText": reviewText,
"img_path": img_path_r,
}
self.db.child("restaurant").child(restaurant).child("menu").child(menuName).child("review").push(review_info)
print(time_r, rating, menuName, menu1_rating, spicy, reviewText, img_path_r)
return True
# 대표 메뉴 등록
def insert_menu(self, name, menuName, price, spicy, img_path_m):
menu_info ={
"menuName": menuName,
"price": price,
"spicy": spicy,
"img_path": img_path_m,
"review_count": 0,
"rate": 0,
}
if self.menu_duplicate_check(name, menuName):
self.db.child("restaurant").child(name).child("menu").child(menuName).set(menu_info)
print(name, menuName, price, spicy, img_path_m)
return True
else:
return False
# 이름으로 유저 데이터 가져오기
def get_users_byID(self, ID):
users = self.db.child("user").get()
for user in users.each():
value = user.val()
if value['id'] == ID:
return user;
# 유저가 찜한 맛집 데이터 가져오기
def get_bookmarked_by_userID(self, user):
u = self.get_users_byID(user).val()
result={}
if "bookmarked" in u:
result = dict(u['bookmarked'])
return result
# 맛집 찜하기 기능
def bookmark(self, user, restaurant):
username = self.get_users_byID(user).key()
if self.db.child("user").child(username).child("bookmarked").child(restaurant).set(1):
res_info = self.db.child("restaurant").child(restaurant).get().val()
bookmarked = res_info['bookmarked']+1
if self.db.child("restaurant").child(restaurant).child('bookmarked').set(bookmarked):
return True
return False
# 찜 해제하기 기능
def bookmark_delete(self, user, restaurant):
username = self.get_users_byID(user).key()
self.db.child("user").child(username).child("bookmarked").child(restaurant).remove()
res_info = self.db.child("restaurant").child(restaurant).get().val()
bookmarked = res_info['bookmarked']-1
if self.db.child("restaurant").child(restaurant).child('bookmarked').set(bookmarked):
return True
return False
# 맛집 등록
def insert_restaurant(self, name, address, phone, parking, category, link, open_time, close_time, noop, breaktime, img_path):
restaurant_info ={
"name": name,
"address": address,
"phone": phone,
"parking": parking,
"category": category,
"link": link,
"open_time": open_time,
"close_time": close_time,
"noop": noop,
"breaktime": breaktime,
"img_path": img_path,
"bookmarked" : 0,
"review_count": 0,
"clean_rate" : 0
}
if self.restaurant_duplicate_check(name):
self.db.child("restaurant").child(name).set(restaurant_info)
print(name, address, phone, parking, category, link, open_time, close_time, noop, breaktime, img_path)
return True
else:
return False
# 맛집 이름 중복 검사
def restaurant_duplicate_check(self, name):
restaurants = self.db.child("restaurant").get()
if restaurants.val() is None:
return True
for res in restaurants.each():
if res.key() == name:
return False
return True
def menu_duplicate_check(self, name, menuName):
menus = self.db.child("restaurant").child(name).child("menu").get()
if menus.val() is None:
return True
for menu in menus.each():
value = menu.val()
if value['menuName'] == menuName:
return False
return True
#맛집 이름으로 restaurant 테이블에서 정보 가져오기
def get_shop_byname(self,name):
restaurants = self.db.child("restaurant").get()
target_value=""
for res in restaurants.each():
value = res.val()
if value['name']==None:
return None
if value['name']==name:
target_value=value
return target_value
#별점 평균 계산
def get_avgrate_byname(self,name):
menus=self.db.child("restaurant").child(name).child("menu").get()
if menus.val() is None:
return 0
rate = 0.0
count = 0
for menu in menus.each():
value = menu.val()
if value['rate'] is not 0:
rate += float(value['rate'])
count += 1
if count == 0 :
return 0
else:
return round(rate/count, 2)
#식당이름으로 review 접근하기 --> 메뉴 이름으로 메뉴마다의 리뷰를 보게 하는 것이 더 좋을 것 같다...?
def get_review_byname(self,name,menuName):
menus=self.db.child("restaurant").child(name).child("menu").get()
target_value=""
for res in menus.each():
value=res.val()
if value['menuName']==menuName:
target_value=value
return target_value
# 모든 식당 데이터 반환(메인화면에서 필요)
def get_all_data(self):
data=self.db.child("restaurant").get()
return data.val()
# 맛집 등록 테이블에서 식당 데이터 가져오기
def get_restaurants(self):
restaurants = self.db.child("restaurant").get().val()
return restaurants
#입력받은 아이디와 비밀번호의 해시값이 동일한 경우가 있는지 확인
def find_user(self, id_, pw_):
users = self.db.child("user").get()
target_value =[]
for res in users.each():
value = res.val()
if value['id'] == id_ and value['pw']==pw_:
return True
return False
#카테고리 기능
def get_restaurants_bycategory(self,cate):
restaurants=self.db.child("restaurant").get();
target_value=[]
for res in restaurants.each():
value=res.val()
if value['category']==cate:
target_value.append(value)
print("######target_value",target_value)
new_dict={}
for k,v in enumerate(target_value):
new_dict[k]=v
return new_dict