-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB_handler.py
67 lines (57 loc) · 1.77 KB
/
DB_handler.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
import pyrebase
import json
import uuid
class DBModule:
def __init__(self):
with open("./auth/firebaseAuth.json") as f:
config = json.load(f)
firebase = pyrebase.initialize_app(config)
self.db = firebase.database()
def login(self, uid, pwd):
users = self.db.child("users").get().val()
try:
userinfo = users[uid]
if userinfo["pwd"] == pwd:
return True
else:
return False
except:
return False
def signin_verification(self, uid):
users = self.db.child("users").get().val()
for i in users:
if uid == i:
return False
return True
def signin(self, _id_, pwd, name, email):
information = {
"pwd": pwd,
"uname":name,
"email":email
}
if self.signin_verification(_id_):
self.db.child("users").child(_id_).set(information)
return True
else:
return
def write_post(self, title, contents, uid):
pid = str(uuid.uuid4())[:12]
information = {
"title":title,
"contents":contents,
"uid":uid
}
self.db.child("posts").child(pid).set(information)
def post_list(self):
post_lists = self.db.child("posts").get().val()
return post_lists
def post_detail(self, pid):
post = self.db.child("posts").get().val()[pid]
return post
def get_user(self, uid):
post_list = []
users_post = self.db.child("posts").get().val()
for post in users_post.items():
if post[1]["uid"] == uid:
post_list.append(post)
return post_list