-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
67 lines (43 loc) · 1.65 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
import os
from deta import Deta
from dotenv import load_dotenv
import streamlit_authenticator as stauth
# Load the environment variables
load_dotenv(".env")
DETA_KEY = os.getenv("DETA_KEY")
# Initialize with a project key
deta = Deta(DETA_KEY)
# This is how to create/connect a database
db = deta.Base("User")
def insert_user(username, name, password,email):
"""Returns the user on a successful user creation, otherwise raises and error"""
return db.put({"key": username, "name": name, "password": password, "email":email})
def fetch_all_users():
"""Returns a dict of all users"""
res = db.fetch()
return res.items
def get_user(username):
"""If not found, the function will return None"""
return db.get(username)
def update_user(username, updates):
"""If the item is updated, returns None. Otherwise, an exception is raised"""
return db.update(updates, username)
def delete_user(username):
"""Always returns None, even if the key does not exist"""
return db.delete(username)
def register(username, name, password,email):
"""Returns the user on a successful user creation, otherwise raises and error"""
usernames = []
names = []
passwords = []
emails = []
# append user data
usernames.append(username)
names.append(name)
passwords.append(password)
emails.append(email)
# convert password to hash password
hashed_passwords = stauth.Hasher(passwords).generate()
for (username, name, hash_password,email) in zip(usernames, names, hashed_passwords,emails):
# db.insert_user(username, name, hash_password)
insert_user(username, name, hash_password,email)