-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
232 lines (183 loc) · 6.55 KB
/
app.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from flask import Flask, request, jsonify
from flask_restful import Api, Resource
from flask_cors import CORS
from pymongo import MongoClient
from datetime import date
import os
import bcrypt
import spacy
app = Flask(__name__)
CORS(app)
api = Api(app)
client = MongoClient(os.environ['MONGO_URI'], connect=False)
db = client.similaritiesDB
users = db['Users']
############### FUNCTIONS ####################
def checkUserExists(username):
if users.count_documents({'username': username}) == 1:
return True #User exists
else:
return False #User not exists
def checkEmailExists(email):
True if users.countDocuments({'email':email}) == 1 else False
def verifyPassword(username, password):
#Fetch user's hashed password in the database
hashed_password = users.find({'username':username})[0]['password']
#Compare hashed password from database to password hashed here and see if they match
if bcrypt.checkpw(password.encode('utf-8'), hashed_password):
return True
else:
return False
def countTokens(username):
return users.find({'username':username})[0]['tokens']
def fetchName(username):
return users.find({'username':username})[0]['name']
############# END OF FUNCTIONS #################################
############################# CLASSES ###############################
class Register(Resource):
def post(self):
#Get username and password data
data = request.get_json()
name = data['name']
username = data['username']
email = data['email']
password = data['password']
joined = date.today().strftime("%B %d, %Y")
#Check if username exists in database
if checkUserExists(username):
retJson = {
'Message': 'Username taken, try a different username.',
}
#Error code 417 - Expectation failed.
return (retJson, 417)
#If not, create user, hash password and return 200(ok)
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
users.insert({
'name': name,
'username': username,
'email': email,
'password': hashed_password,
'tokens': 5,
'joined': joined
})
retJson = {
'Message': 'Registration successful',
'payload': {
'name': name,
'username': username,
'tokens': countTokens(username)
}
}
return (retJson, 200)
class Login(Resource):
def post(self):
data = request.get_json()
username = data['username']
password = data['password']
if verifyPassword(username, password):
retJson = {
'Message': 'Login successful',
'payload': {
'name': fetchName(username),
'username': username,
'tokens':countTokens(username)
}
}
return (retJson, 200)
retJson = {
'Message': 'Sorry, could not login',
}
return (retJson,401)
class Detect(Resource):
def post(self):
#Get data from user
data = request.get_json()
username = data['username']
text1 = data['text1']
text2 = data['text2']
#verify login credentials
if not checkUserExists(username):
retJson = {
'Message': 'Username does not exist',
}
return (retJson, 403)
#Check if user has tokens to complete further processes
num_tokens = countTokens(username)
if num_tokens <= 0:
retJson = {
'Message': 'Insufficient tokens',
}
return (retJson, 403)
#convert sentences to nlp, calculate ratio and subtract a token for transaction
nlp = spacy.load("en_core_web_sm")
text1 = nlp(text1)
text2 = nlp(text2)
ratio = text1.similarity(text2)
#Return ratio to user
result = '{0:.2f}%'.format((ratio*100))
retJson = {
'Message': f'The two texts are {result} similar',
}
return (retJson, 200)
class UpdateTokens(Resource):
def put(self):
data = request.get_json()
username = data['username']
if countTokens(username) > 0:
users.update(
{'username': username},
{"$set": {'tokens': countTokens(username)-1}}
)
retJson = {
'Message':'tokens updated',
'payload': countTokens(username)
}
return (retJson, 200)
return('Could not update tokens', 400)
class Refill(Resource):
def post(self):
#Get username and admin password
data = request.get_json()
username = data['username']
admin_password = data['admin_password']
refillNumber = data['refillNumber']
#Verify admin password is correct before adding tokens. For the purpose of simplicity I will use a plain password stored here.
if admin_password != admin_pass:
retJson = {
'Message': 'Invalid Admin Password',
'Status': 302
}
return jsonify(retJson)
#Check if user exists in database
if not checkUserExists(username):
retJson = {
'Message': 'User does not exist.',
'Status': 303
}
return jsonify(retJson)
#Check number of tokens user currently has
currentTokens = int(countTokens(username))
refillNumber = int(refillNumber)
#Add number of tokens you wish to fill plus current number of tokens user has
users.update_one(
{'username':username},
{"$set": {'tokens': currentTokens + refillNumber}}
)
retJson = {
'Message' : 'Tokens refilled successfully.',
'Status' : 200
}
return jsonify(retJson)
############################# END OF CLASSES ###############################
@app.route('/')
def test():
return 'API is running'
################## Add API Resources ##################################
api.add_resource(Register, '/register')
api.add_resource(Login, '/login')
api.add_resource(Detect, '/detect')
api.add_resource(UpdateTokens, '/tokenCount')
api.add_resource(Refill, '/refill')
################## End of Add API Resources ##################################
if __name__ == '__main__':
app.run(debug=True)