-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
108 lines (90 loc) · 3.06 KB
/
db.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
import json
import chatbot
from datetime import datetime
from datetime import timedelta
from cloudant.client import Cloudant
TYPE_USER = 'user'
TYPE_DOCTOR = 'doctor'
TYPE_COMMUNITY = 'community'
IBM_RECEIVER = 'ibm_assistant'
SESSION_TIMEOUT = 300 #seconds
with open('config.json') as config_file:
config = json.load(config_file)
client = Cloudant.iam(config["couch"]["username"], config["couch"]["api"], connect=True)
sessionsDB = client['sessions']
usersDB = client['users']
def getSessionID(userID):
if userID in sessionsDB:
session = sessionsDB[userID]
#Check expiry
if datetime.strptime(session['last_used'], "%Y-%m-%d %H:%M:%S.%f") > (datetime.utcnow() - timedelta(seconds=SESSION_TIMEOUT)):
#Not expired
session['last_used'] = str(datetime.utcnow())
session.save()
return session['sessionID']
else:
#expired
sessionID = chatbot.new_session()
session['last_used'] = str(datetime.utcnow())
session['sessionID'] = sessionID
session.save()
return sessionID
else:
session = chatbot.new_session()
my_dic = {'_id':userID, 'sessionID':session, 'last_used': str(datetime.utcnow())}
sessionsDB.create_document(my_dic)
return session
def getUserID(sessionID):
for session in sessionsDB:
if session['sessionID'] == sessionID:
return session['_id']
return None
def getReceiver(userID):
if userID in usersDB:
return usersDB[userID]["receiver"]
else:
my_dic = {'_id': userID, 'receiver':IBM_RECEIVER, 'type':'user'}
usersDB.create_document(my_dic)
return 'ibm_assistant'
def updateUserType(userID, type):
if userID in usersDB:
user = usersDB[userID]
user['type'] = type
user.save()
return True
return False
def setupConnection(user1, user2):
if user1 not in usersDB or user2 not in usersDB:
return False
userDoc1 = usersDB[user1]
userDoc1['receiver'] = user2
userDoc1.save()
userDoc2 = usersDB[user2]
userDoc2['receiver'] = user1
userDoc2.save()
return True
def breakConnection(user1, user2):
if user1 not in usersDB or user2 not in usersDB:
return False
userDoc1 = usersDB[user1]
userDoc1['receiver'] = IBM_RECEIVER
userDoc1.save()
userDoc2 = usersDB[user2]
userDoc2['receiver'] = IBM_RECEIVER
userDoc2.save()
return True
def findDoctor(userID):
for user in usersDB:
if user['_id'] != userID and user['type'] == TYPE_DOCTOR and user['receiver'] == IBM_RECEIVER:
return user['_id']
return None
def findCommunity(userID):
for user in usersDB:
if user['_id'] != userID and user['type'] == TYPE_COMMUNITY and user['receiver'] == IBM_RECEIVER:
return user['_id']
return None
def findExpert(userID, type):
for user in usersDB:
if user['_id'] != userID and user['type'].lower() == type.lower() and user['receiver'] == IBM_RECEIVER:
return user['_id']
return None