forked from RickBaird/senior-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer
247 lines (208 loc) · 6.25 KB
/
Server
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
import json, operator
# Init app
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
# Database
# should be moved to mySQL
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Init db
db = SQLAlchemy(app)
# Init ma
ma = Marshmallow(app)
# Product Class/Model
# Note: testing data ID added: ID0, ID1, ID2, ID3
class Product(db.Model):
# String ID is pulled from Firebase, attached to user profile
id = db.Column(db.String, primary_key=True)
# 10 questions answered by user, stored as int
q1 = db.Column(db.Integer)
q2 = db.Column(db.Integer)
q3 = db.Column(db.Integer)
q4 = db.Column(db.Integer)
q5 = db.Column(db.Integer)
q6 = db.Column(db.Integer)
q7 = db.Column(db.Integer)
q8 = db.Column(db.Integer)
q9 = db.Column(db.Integer)
q10 = db.Column(db.Integer)
def __init__(self, id, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10):
self.id = id
self.q1 = q1
self.q2 = q2
self.q3 = q3
self.q4 = q4
self.q5 = q5
self.q6 = q6
self.q7 = q7
self.q8 = q8
self.q9 = q9
# Product Schema
class ProductSchema(ma.Schema):
class Meta:
fields = ('id', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9', 'q10')
# Init schema
product_schema = ProductSchema()
products_schema = ProductSchema(many=True)
# Create a Product
@app.route('/product', methods=['POST'])
def add_product():
id = request.json['id']
q1 = request.json['q1']
q2 = request.json['q2']
q3 = request.json['q3']
q4 = request.json['q4']
q5 = request.json['q5']
q6 = request.json['q6']
q7 = request.json['q7']
q8 = request.json['q8']
q9 = request.json['q9']
q10 = request.json['q10']
new_product = Product(id, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10)
db.session.add(new_product)
db.session.commit()
return product_schema.jsonify(new_product)
# Get All Products
@app.route('/product', methods=['GET'])
def get_products():
all_products = Product.query.all()
result = products_schema.dump(all_products)
return jsonify(result)
# Get Single Products
@app.route('/product/<id>', methods=['GET'])
def get_product(id):
product = Product.query.get(id)
return product_schema.jsonify(product)
# Update a Product
@app.route('/product/<id>', methods=['PUT'])
def update_product(id):
product = Product.query.get(id)
id = request.json['id']
q1 = request.json['q1']
q2 = request.json['q2']
q3 = request.json['q3']
q4 = request.json['q4']
q5 = request.json['q5']
q6 = request.json['q6']
q7 = request.json['q7']
q8 = request.json['q8']
q9 = request.json['q9']
q10 = request.json['q10']
product.id = id
product.q1 = q1
product.q2 = q2
product.q3 = q3
product.q4 = q4
product.q5 = q5
product.q6 = q6
product.q7 = q7
product.q8 = q8
product.q9 = q9
product.q10 = q10
db.session.commit()
return product_schema.jsonify(product)
# Delete Product
# not needed for our project but here if so
@app.route('/product/<id>', methods=['DELETE'])
def delete_product(id):
product = Product.query.get(id)
db.session.delete(product)
db.session.commit()
return product_schema.jsonify(product)
# Comparer model
class Comparer(db.Model):
id = db.Column(db.String, primary_key=True)
perc = db.Column(db.Integer)
def __init__(self, id, perc):
seld.id = id
self.perc = perc
# Comparer schema
class ComparerSchema(ma.Schema):
class Meta:
fields = ('id', 'perc')
# Init schema
comparer_schema = ComparerSchema()
comparers_schema = ComparerSchema(many=True)
# Get similar
@app.route('/compare/<id>', methods=['GET'])
def get_comparison(id):
result3 = run(id)
return comparers_schema.jsonify(result3)
#Get the Top 10 User Matches
def getMax10(map):
matches = []
num = 0
if(len(map) >= 10):
num = 10
else:
num = len(map)
for j in range(num):
max1 = max(map.items(), key=operator.itemgetter(1))[0]
matches.append({"id":max1, "perc":max(map.values())})
del map[max1]
# print(matches)
return matches
def run(id0):
currentUserId = id0
product2 = Product.query.get(currentUserId)
currUserJson = product_schema.jsonify(product2)
all_products2 = Product.query.all()
result = products_schema.dump(all_products2)
otherUserJson = jsonify(result)
q1 = currUserJson.json['q1']
q2 = currUserJson.json['q2']
q3 = currUserJson.json['q3']
q4 = currUserJson.json['q4']
q5 = currUserJson.json['q5']
q6 = currUserJson.json['q6']
q7 = currUserJson.json['q7']
q8 = currUserJson.json['q8']
q9 = currUserJson.json['q9']
q10 = currUserJson.json['q10']
map = {}
for i in range(0, len(otherUserJson.json)):
similar = 0
oid = otherUserJson.json[i]['id']
oq1 = otherUserJson.json[i]['q1']
oq2 = otherUserJson.json[i]['q2']
oq3 = otherUserJson.json[i]['q3']
oq4 = otherUserJson.json[i]['q4']
oq5 = otherUserJson.json[i]['q5']
oq6 = otherUserJson.json[i]['q6']
oq7 = otherUserJson.json[i]['q7']
oq8 = otherUserJson.json[i]['q8']
oq9 = otherUserJson.json[i]['q9']
oq10 = otherUserJson.json[i]['q10']
if oq1 == q1:
similar = similar + 1
if oq2 == q2:
similar = similar + 1
if oq3 == q3:
similar = similar + 1
if oq4 == q4:
similar = similar + 1
if oq5 == q5:
similar = similar + 1
if oq6 == q6:
similar = similar + 1
if oq7 == q7:
similar = similar + 1
if oq8 == q8:
similar = similar + 1
if oq9 == q9:
similar = similar + 1
if oq10 == q10:
similar = similar + 1
if oid != currentUserId:
map[oid] = similar
#print(map)
res = getMax10(map)
#print(res)
return res
# Run Server
if __name__ == '__main__':
app.run(host = '0.0.0.0', port=5000, debug = True)