-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb.py
More file actions
61 lines (44 loc) · 1.62 KB
/
db.py
File metadata and controls
61 lines (44 loc) · 1.62 KB
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
from pymongo import MongoClient
# Includes database operations
class DB:
# db initializations
def __init__(self):
self.client = MongoClient('mongodb://localhost:27017/')
self.db = self.client['p2p-chat']
# checks if an account with the username exists
def is_account_exist(self, username):
if self.db.accounts.find({'username': username}).count() > 0:
return True
else:
return False
# registers a user
def register(self, username, password):
account = {
"username": username,
"password": password
}
self.db.accounts.insert(account)
# retrieves the password for a given username
def get_password(self, username):
return self.db.accounts.find_one({"username": username})["password"]
# checks if an account with the username online
def is_account_online(self, username):
if self.db.online_peers.find({"username": username}).count() > 0:
return True
else:
return False
# logs in the user
def user_login(self, username, ip, port):
online_peer = {
"username": username,
"ip": ip,
"port": port
}
self.db.online_peers.insert(online_peer)
# logs out the user
def user_logout(self, username):
self.db.online_peers.remove({"username": username})
# retrieves the ip address and the port number of the username
def get_peer_ip_port(self, username):
res = self.db.online_peers.find_one({"username": username})
return (res["ip"], res["port"])