forked from Kim-Greeny/camfle_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
267 lines (211 loc) · 7.92 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
import pyrebase
import json
import array
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_item(self, name, data, img_path):
item_info ={
"price": data['price'],
"okprice": data['okprice'],
"seller": data['seller'],
"addr": data['addr'],
"category": data['category'],
"status": data['status'],
"level": data['level'],
"sold": False,
"buyer": "",
"img_path": img_path,
"click_count": 0
}
self.db.child("item").child(name).set(item_info)
print(data,img_path)
return True
def insert_user(self, data, pw):
user_info ={
"id": data['id'],
"pw": pw
}
if self.user_duplicate_check(str(data['id'])):
self.db.child("user").push(user_info)
print(data)
return True
else:
return False
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 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_items(self):
items=self.db.child("item").get().val()
if items is not None:
return items
else :
return {}
def get_top_three_items(self):
items = self.get_items()
# sold가 False인 아이템만 필터링
sold_false_items = {key: value for key, value in items.items() if not value.get('sold', False)}
# 클릭 수 기준으로 정렬
sorted_items = sorted(sold_false_items.items(), key=lambda x: x[1].get('click_count', 0), reverse=True)
# 상위 세 개의 아이템 반환
top_three_items = dict(sorted_items[:3])
return top_three_items
def add_click(self, name):
item = self.get_item_byname(name)
click_count = item.get('click_count', 0)
click_count += 1
item_ref = self.db.child("item").child(name)
item_ref.update({"click_count": click_count})
return True
def get_items_bycategory(self,cate):
items=self.db.child("item").get()
target_value=[]
target_key=[]
for res in items.each():
value=res.val()
key_value=res.key()
if value['category'] == cate:
target_value.append(value)
target_key.append(key_value)
print("######target_value", target_value)
new_dict={}
for k,v in zip(target_key, target_value):
new_dict[k]=v
return new_dict
def get_item_byseller(self, name):
items=self.db.child("item").get()
target_value=[]
target_key=[]
for res in items.each():
value=res.val()
key_value=res.key()
if value['seller'] == name:
target_value.append(value)
target_key.append(key_value)
print("######target_value", target_value)
new_dict={}
for k,v in zip(target_key, target_value):
new_dict[k]=v
return new_dict
def get_review_byseller(self, name):
items = self.db.child("item").get()
reviews = self.db.child("review").get()
target_value = []
target_key = []
for res in items.each():
value = res.val()
key_value = res.key()
if value['seller'] == name and value['sold'] == True:
if reviews:
for rev_res in reviews.each():
rev_value = rev_res.val()
rev_key_value = rev_res.key()
if rev_key_value == key_value:
target_value.append(rev_value)
target_key.append(rev_key_value)
print("######target_value", target_value)
new_dict = {}
for k, v in zip(target_key, target_value):
new_dict[k] = v
return new_dict
def get_item_byname(self, name):
items = self.db.child("item").get()
target_value=""
print("###########",name)
for res in items.each():
key_value = res.key()
if key_value == name:
target_value=res.val()
return target_value
def get_item_bylike(self, name):
likes = self.db.child("heart").child(name).get()
items = self.db.child("item").get()
target_value = []
target_key = []
for like_res in likes.each():
like_value = like_res.val()
like_key_value = like_res.key()
if like_value['interested'] == 'Y':
for res in items.each():
value = res.val()
key_value = res.key()
if like_key_value == key_value:
target_value.append(value)
target_key.append(key_value)
print("######target_value", target_value)
new_dict = {}
for k, v in zip(target_key, target_value):
new_dict[k] = v
return new_dict
def update_sold(self, uid, name, data):
items = self.db.child("item").get()
print("###########", name)
for res in items.each():
key_value = res.key()
if key_value == name:
item_ref = self.db.child("item").child(name)
target_value = res.val()
target_value['sold'] = True
target_value['buyer'] = uid
item_ref.update(target_value)
return target_value
def reg_review(self, uid, data, img_path):
review_info ={
"title": data['title'],
"rate": data['reviewStar'],
"review": data['reviewContents'],
"writer": uid,
"img_path": img_path
}
self.db.child("review").child(data['name']).set(review_info)
return True
def get_reviews(self ):
reviews = self.db.child("review").get().val()
if reviews is not None:
return reviews
else :
return {}
def get_review_byname(self, name):
reviews = self.db.child("review").get()
target_value=""
print("#########", name)
for res in reviews.each():
key_value = res.key()
if key_value == name:
target_value=res.val()
return target_value
def get_heart_byname(self, uid, name):
hearts = self.db.child("heart").child(uid).get()
target_value=""
if hearts.val() == None:
return target_value
for res in hearts.each():
key_value = res.key()
if key_value == name:
target_value=res.val()
return target_value
def update_heart(self, user_id, isHeart, item):
heart_info ={
"interested": isHeart
}
self.db.child("heart").child(user_id).child(item).set(heart_info)
return True