-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
100 lines (79 loc) · 1.98 KB
/
example.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
from pymongo import MongoClient
try:
print("Connecting to Database...")
client = MongoClient()
db = client.HackBU
print("Connected to db :)")
except:
print("Could not connect to db :(")
users = db.users
def create():
username = input("Username: ")
name = input("Name: ")
new_user = {
'username' : username,
'name' : name
}
user = users.find_one({'username' : username})
if user is not None:
print("User already exists!")
return
try:
users.insert_one(new_user).inserted_id
print("Account created.")
except:
print("Could not insert user.")
def login():
username = input("What is your username? Enter: ")
user = users.find_one({'username' : username})
if user is not None:
print("Hello " + str(user['name']) + "!")
else:
print("Could not find")
def delete():
username = input("What is your username? Enter: ")
user = users.find_one({'username' : username})
if user is None:
print("User does not exist")
else:
users.delete_one(user)
user = users.find_one({'username' : username})
if user is None:
print("User deleted.")
else:
print("Could not delete user.")
def clear():
try:
users.drop()
print("cleared users collection.")
except:
print("Could not clear users collection.")
def show_all():
try:
cursor = users.find({})
for doc in cursor:
print(doc)
except:
print("Could not show users collection.")
accessing = True
while(accessing):
choice = input("\
1 - create, \
2 - login, \
3 - delete, \
4 - clear, \
5 - Show all, \
else to exit. Enter: \
")
if choice == '1':
create()
elif choice == '2':
login()
elif choice == '3':
delete()
elif choice == '4':
clear()
elif choice == '5':
show_all()
else:
accessing = False