-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.py
150 lines (127 loc) · 4.98 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
import pyrebase
import json
import re
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 ={
"sellerId": data['sellerId'],
"contact": data['contact'],
"productName": data['productName'],
"price": data['price'],
"img_path": img_path,
"continent": data['continent'],
"nation": data['nation'],
"address": data['address'],
"state": data['state'],
"description": data['description']
}
self.db.child("item").child(name).set(item_info)
print(data,img_path)
return True
def get_items(self):
items = self.db.child("item").get().val()
return items
# name값으로 item 정보 가져오기
def get_item_byname(self, name):
item = self.db.child("item").child(name).get()
if item.val() is None: # name에 해당하는 데이터가 없을 경우
return None
return item.val()
def insert_user(self, data, pw) :
user_info = {
"id" : data['id'],
"pw" : pw,
# "nickname" : data['nickname']
}
if self.user_duplicate_check(str(data['id'])): # 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 validate_user_id(self,id):
# 영문자로 시작하고, 영문자와 숫자만 포함하며 5~15자 길이
pattern = r'^[a-zA-Z][0-9a-zA-Z]{4,14}$'
return re.match(pattern, id)
def validate_password(self,pw):
# 최소 8자, 문자, 숫자, 특수문자 각각 1개 이상 포함
pattern = r'^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$'
return re.match(pattern, pw)
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
# my page 관련
def get_user_wishlist(self,id):
wishlist_ref = self.db.child('wishlist').order_by_child('id').equal_to(id).get()
wishlist = [item.val() for item in wishlist_ref.each()]
return wishlist
def get_user_purchases(self,id):
purchases_ref = self.db.child('purchases').order_by_child('id').equal_to(id).get()
purchases = [item.val() for item in purchases_ref.each()]
return purchases
def get_user_sales(self,id):
sales_ref = self.db.child('products').order_by_child('sellerId').equal_to(id).get()
sales = [item.val() for item in sales_ref.each()]
return sales
def get_heart_by_name(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
# 리뷰 작성 등록
def reg_review(self, data, img_path):
review_info ={
"buyerId": data['buyerId'],
"sellerId": data['sellerId'],
"title": data['title'],
"rate": data['reviewStar'],
"review": data['reviewContents'],
"review_time": data['review_time'],
"img_path": img_path
}
self.db.child("review").child(data['name']).set(review_info)
return True
# 리뷰 상세 조회
def get_review_by_name(self, name):
item = self.db.child("review").child(name).get()
if item.val() is None: # name에 해당하는 데이터가 없을 경우
return None
return item.val()
# 회원 별 리뷰 전체 조회
def get_all_review_by_id(self, id):
all_reviews = self.db.child("review").get()
# 전체 리뷰 들고와서 sellerId와 id가 동일한 것만
for rev in all_reviews.each():
if rev.val().get("sellerId") == id:
return rev.val()
return None