This repository was archived by the owner on May 11, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
660 lines (505 loc) · 20.1 KB
/
Copy pathmain.py
File metadata and controls
660 lines (505 loc) · 20.1 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
from flask import Flask, request, jsonify, send_file, json, url_for, redirect, render_template, flash
from flask_mail import Mail, Message
from tensorflow.keras import backend
import os
import cv2
import pandas as pd
import numpy as np
import time
import json
from bson import ObjectId
from flask_cors import CORS, cross_origin
from bson.objectid import ObjectId
from datetime import datetime
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager, jwt_required, create_access_token, get_jwt_identity, get_raw_jwt
#custom modules
import config
from ressources.config_db import db
from ressources.model_collab_recommender import predict_ratings
from ressources.picture_list_creation import get_recommended_picture_list
from ressources.email_verification import send_email, verify_email, confirm_token
from image_similarity.get_embeddings import get_embeddings
from image_similarity.train_annoy_model import train_annoy_model
URL = "http://127.0.0.1:3000-"
# init app
app = Flask(__name__)
FILE_PATH = os.path.dirname(os.path.realpath(__file__))
app.config.from_pyfile('config.py')
app.config.update(config.mail_settings)
mail = Mail(app)
# app.config['JWT_SECRET_KEY'] = os.environ['JWT_SECRET_KEY']
app.config['JWT_HEADER_TYPE'] = None
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access']
bcrypt = Bcrypt(app)
jwt = JWTManager(app)
CORS(app)
blacklist = set()
@jwt.token_in_blacklist_loader
def check_if_token_in_blacklist(decrypted_token):
jti = decrypted_token['jti']
return jti in blacklist
@app.route("/", methods= ["GET"])
def testingBackend():
print("Backend is on on fire !")
return 'Backend is on but not all of it !'
@app.route("/check_token", methods= ["GET"])
@jwt_required
def check_token():
current_user = get_jwt_identity()
if current_user["userType"] == request.headers.get('fromUserType') :
return jsonify({"valid" : 'Token is valid'})
else :
return jsonify({"msg" : "Wrong type of user"})
@app.route("/upload_image", methods= ["POST"])
@jwt_required
def upload_image():
current_user = get_jwt_identity()
if current_user["userType"] != 'company' :
return jsonify({"msg" : "Wrong type of user"})
company_name = current_user["company_name"]
UPLOAD_FOLDER = './image_similarity/data/train/'
ALLOWED_EXTENSIONS = ['.png', '.jpg', '.jpeg']
image_file = request.files["imageFile"]
typeCloth = request.form["typeCloth"]
materialCloth = request.form["materialCloth"]
productionMethod = request.form["productionMethod"]
price = request.form["price"]
sex = request.form["sex"]
description = request.form["description"]
extension = os.path.splitext(image_file.filename)[1]
if not extension in ALLOWED_EXTENSIONS :
return 'Invalid extension'
if not os.path.isdir(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
if os.path.isfile('./image_similarity/outfile/nbrFiles.npy'):
nbrFiles = np.load('./image_similarity/outfile/nbrFiles.npy').astype(int)
filename = "IMG_" + str(nbrFiles +1) + extension
else:
filename = "IMG_1" + extension
destination = UPLOAD_FOLDER + filename
image_file.save(destination)
while not os.path.exists(destination):
print('waiting')
time.sleep(1)
if os.path.isfile(UPLOAD_FOLDER + filename):
get_embeddings()
train_annoy_model()
coll = db["image_info"]
x = coll.insert_one({
"name":filename,
"typeCloth": typeCloth,
"materialCloth": materialCloth,
"productionMethod": productionMethod,
"price": int(price),
"sex": sex,
"description": description,
"company_name" : company_name,
"company_id": current_user["_id"]
})
coll = db.company_info
result = coll.find_one({"company_name":company_name})
company_images = result["images_uploaded"]
company_images.append(filename)
cursor = coll.update_one({"company_name":company_name},{"$set":{"images_uploaded":company_images}})
return jsonify({"valid" : "Cloth has been uploaded"})
@app.route('/update_info', methods=["POST"])
@jwt_required
def update_info():
try:
current_user = get_jwt_identity()
user_id = ObjectId(current_user["_id"])
user = db.user_info if current_user["userType"] == 'client' else db.company_info
new_mail = str(request.get_json()["email"])
old_mail = current_user["email"]
mailAlreadyExist = user.find_one({"email":request.get_json()["email"]})
if mailAlreadyExist :
return jsonify({'already_exist' : 'Mail already used'})
res = user.find_one({"_id":user_id})
if res:
x = user.update_one({"_id":user_id},{"$set":request.get_json()})
if old_mail != new_mail:
verify_email(new_mail)
x = user.update_one({"_id":user_id},{"$set":{"mail_verified":False, "mail_verified_on":""}})
return jsonify({'valid' : 'User informations updated !'})
else:
return jsonify({'msg' : 'User not found'})
except:
print('error')
return jsonify({'msg' : 'Something went wrong'})
@app.route('/update_image_info', methods=["POST"])
@jwt_required
def update_image_info():
json_data = request.get_json(force = True)['image_info_card']
try:
coll = db.image_info
image_name = json_data['name']
res = coll.find_one({"name":image_name})
if res:
x = coll.update_one({"name" : image_name},{"$set":json_data})
return jsonify({'valid' : 'User informations updated !'})
else:
return jsonify({'msg' : 'User not found'})
except:
print('error')
return jsonify({'msg' : 'Something went wrong'})
@app.route('/update_filters', methods=["POST"])
@jwt_required
def update_filters():
try:
current_user = get_jwt_identity()
user_id = current_user["_id"]
filters = db.filters
res = filters.find_one({"user_id": user_id })
if res:
x = filters.update_one({ "user_id" : user_id },{ "$set" : request.get_json(force = True) })
return jsonify({'valid' : 'Filters updated'})
else:
json_data = request.get_json(force = True)
json_data["user_id"] = user_id
x = filters.insert_one(json_data)
return jsonify({'valid' : 'Filters created'})
except:
print('error in update filters')
return jsonify({'msg' : 'Something went wrong'})
@app.route('/change_pwd', methods=["POST"])
@jwt_required
def change_pwd():
try:
current_user = get_jwt_identity()
user_id = ObjectId(current_user["_id"])
user = db.user_info if current_user["userType"] == 'client' else db.company_info
res = user.find_one({"_id":user_id})
if res:
old_password = request.get_json()['old_password']
if bcrypt.check_password_hash(res['password'], old_password):
new_password = bcrypt.generate_password_hash(request.get_json()['new_password']).decode('utf-8')
x = user.update_one({"_id":user_id},{"$set":{'password': new_password}})
return jsonify({'valid' : 'User pwd updated !'})
else:
return jsonify({'info' : 'Invalid old password'})
else:
return jsonify({'msg' : 'User not found'})
except:
return jsonify({'msg' : 'Something went wrong'})
@app.route('/re_verify', methods=["POST"])
def re_verify():
email = request.get_json(force = True)["email"]
coll= db.user_info
result = coll.find_one({"email":email})
if not result:
return jsonify({'msg' : 'This email does not exists, try to register again !'})
verify_email(email)
return jsonify({'success' : 'verification email sent'})
@app.route('/new_company', methods=["POST"])
def new_company():
try:
company = db.company_info
company_name = request.get_json()['company_name']
location = request.get_json()["location"]
email = request.get_json()['email']
phone = str(request.get_json()['phone'])
password = bcrypt.generate_password_hash(request.get_json()['password']).decode('utf-8')
created = datetime.utcnow()
try:
res = company.find_one({"email":email})
if res["email"] == email:
return "already exists"
except:
None
x = company.insert_one({
'company_name': company_name,
"location":location,
'email': email,
'password': password,
'created': created,
'phone' : phone,
"images_uploaded":[],
'mail_verified': True # IF TRUE EMAIL VERIF IS NOT ACTIVE !!!
})
result = company.find_one({"email":email})
company_id = str(result["_id"])
verify_email(email)
return "ok"
except:
return ''
@app.route('/new_user', methods=["POST"])
def new_user():
try:
user = db.user_info
first_name = request.get_json()['first_name']
last_name = request.get_json()['last_name']
email = request.get_json()['email']
phone = request.get_json()['phone']
sex = request.get_json()['sex']
password = bcrypt.generate_password_hash(request.get_json()['password']).decode('utf-8')
created = datetime.utcnow()
try:
res = user.find_one({"email":email})
if res["email"] == email:
return "already exists"
except:
None
x = user.insert_one({
'first_name': first_name,
'last_name': last_name,
'email': email,
'password': password,
'created': created,
"sex" : sex,
'phone' : phone,
'mail_verified': True # IF TRUE EMAIL VERIF IS NOT ACTIVE !!!
})
result = user.find_one({"email":email})
user_id = str(result["_id"])
collection = db.list_images
x = collection.insert_one({
"user_id":user_id,
"super_like":[],
"list_image":[]
})
verify_email(email)
return "ok"
except:
return ""
@app.route('/confirm/<token>', methods=["GET"])
def confirm_email(token):
try:
email = confirm_token(token)
except:
flash('The confirmation link is invalid or has expired.', 'danger')
email = None
if email == None:
return redirect(URL + "/notconfirm", code=307) # !!! TO DO : change toward login screen
# and add relevant message
coll = db.user_info
try:
result = coll.find_one({"email":email})
confirmed = result["mail_verified"]
except:
coll = db.company_info
try:
result = coll.find_one({"email":email})
confirmed = result["mail_verified"]
except:
confirmed=None
if confirmed:
flash('Account already confirmed. Please login.', 'success')
else:
confirmed = True
confirmed_on = time.ctime()
cursor = coll.update_one({"email":email},{"$set":{
"mail_verified":confirmed, "mail_verified_on":confirmed_on
}})
flash('You have confirmed your account. Thanks! You can now login.', 'success')
return redirect(URL + "/confirm", code=307) # !!! TO DO : change toward login screen
# and add relevant message
@app.route('/login', methods=['POST'])
def login():
userType = request.get_json(force = True)['userType']
user = db.user_info if userType == 'client' else db.company_info
email = request.get_json(force = True)['email']
password = request.get_json(force = True)['password']
result = ""
response = user.find_one({'email': email})
if response:
if response["mail_verified"] == False:
print(" ! YOU HAVE TO VERIFY YOUR MAIL ! ")
return redirect(URL + "/notconfirm", code=307) # !!! TO DO : change toward login screen
# and add relevant message
if bcrypt.check_password_hash(response['password'], password):
if userType == 'client' :
access_token = create_access_token(identity = {
'first_name': response['first_name'],
'last_name': response['last_name'],
'email': response['email'],
'userType': userType,
'_id': str(response['_id'])
})
elif userType == 'company' :
access_token = create_access_token(identity = {
'company_name': response['company_name'],
'location': response['location'],
'email': response['email'],
'userType': userType,
'_id': str(response['_id'])
})
result = jsonify({'token' : access_token})
else:
print("Invalid username and password")
else:
print("No results found")
return result
@app.route('/getProfileInfo', methods=['POST'])
@jwt_required
def getProfileInfo():
current_user = get_jwt_identity()
user_id = ObjectId(current_user["_id"])
user = db.user_info if current_user["userType"] == 'client' else db.company_info
allInfo = user.find_one({'_id': user_id})
excluded = ['_id','password','images_uploaded', 'created', "mail_verified", "mail_verified_on" ]
allInfo = { k : v for k,v in allInfo.items() if k not in excluded }
return jsonify(allInfo)
@app.route('/logout', methods=['DELETE'])
@jwt_required
def logout():
jti = get_raw_jwt()['jti']
blacklist.add(jti)
return jsonify({"msg": "Successfully logged out"})
@app.route('/remove_account', methods=['POST'])
@jwt_required
def remove_account():
current_user = get_jwt_identity()
if current_user["userType"] != 'client' :
return jsonify({"msg" : "Wrong type of user"})
user_id = current_user["_id"]
collection = db["user_info"]
cursor = collection.delete_one({"_id":ObjectId(user_id)})
collection = db["user_ratings"]
cursor = collection.delete_many({"user_id":user_id})
print(cursor.deleted_count, " documents deleted.")
collection = db["pictures_list"]
cursor = collection.delete_many({"user_id":user_id})
collection = db["filters"]
cursor = collection.delete_one({"user_id":user_id})
jti = get_raw_jwt()['jti']
blacklist.add(jti)
return jsonify({"valid" : "Account has been removed"})
@app.route("/load_image_for_rating", methods=["POST"])
@jwt_required
def load_image_for_rating():
json_data = request.get_json(force = True)
current_user = get_jwt_identity()
collection = db["filters"]
user_id = current_user["_id"]
res = collection.find_one({ "user_id" : user_id })
if res :
del res['_id']
del res['user_id']
filt_dic = res
else :
filt_dic = {
"clothe_sex" : [],
"clothe_type" : [],
"clothe_material" : [],
"clothe_production" : [],
"clothe_price_range" : [0, 999]
}
if current_user["userType"] != 'client' :
return jsonify({"msg" : "Wrong type of user"})
pictures_list = get_recommended_picture_list(user_id, filt_dic, json_data)
if not pictures_list:
return jsonify({"no_more_pictures":"No more pictures to show, try to change your filters", 'filters' : filt_dic})
coll = db["image_info"]
list_dict = []
i=0
for pic in pictures_list:
get_info = coll.find_one({"name":pic})
list_dict.append({
"name": pictures_list[i],
"typeCloth": get_info["typeCloth"],
"materialCloth": get_info["materialCloth"],
"productionMethod": get_info["productionMethod"],
"price": get_info["price"],
"sex": get_info["sex"],
"description": get_info["description"]
})
i+=1
send_image_info = jsonify({'imgs' : list_dict, 'filters' : filt_dic})
return send_image_info
@app.route("/one_image_info", methods=["POST"])
def one_image_info():
image_name = request.get_json(force = True)['image_name']
coll = db["image_info"]
get_info = coll.find_one({"name":image_name})
image_info = {
"name": image_name,
"typeCloth": get_info["typeCloth"],
"materialCloth": get_info["materialCloth"],
"productionMethod": get_info["productionMethod"],
"price": get_info["price"],
"sex": get_info["sex"],
"description": get_info["description"]
}
send_image_info = jsonify({'image_info' : image_info})
return send_image_info
@app.route("/show_image", methods=["POST"])
def show_image():
json_data = request.get_json(force = True)
filename = './imagesOnDb/' + json_data['imageName']
send_file_image = send_file(filename, mimetype='image/jpg')
return send_file_image
@app.route("/rate_image", methods=["POST"])
@jwt_required
def rate_image():
json_data = request.get_json(force = True)
current_user = get_jwt_identity()
if current_user["userType"] != 'client' :
return jsonify({"msg" : "Wrong type of user"})
user_id = current_user["_id"]
name = json_data['imageName']
rating = json_data["rating"]
post = {"user_id":user_id, "picture":name,"rating":rating, "timestamp":time.ctime()}
coll = db["user_ratings"]
results = list(coll.find({"user_id":user_id, "picture":name}))
try:
temp_id = results[0]["_id"]
cursor = coll.update_one({"_id":temp_id},{"$set":post})
print(" !!! SAME IMAGE RATED TWICE !!! ")
print(name)
except:
x = coll.insert_one(post)
coll = db["list_images"]
results = list(coll.find({}))
for res in results:
if res["user_id"] == user_id:
if rating == 2:
try:
super_like = res["super_like"]
super_like.append(name)
except:
super_like = [name]
result = coll.update_one({"_id":res["_id"]},{"$set":{"super_like":super_like}})
list_pic = res["list_image"]
push_db = coll.update_one({"_id":res["_id"]},{"$set":{"list_image":list_pic[1:]}})
return jsonify({"valid" : "Cloth has been rated"})
@app.route("/cart", methods=["POST"])
@jwt_required
def cart():
current_user = get_jwt_identity()
if current_user["userType"] != 'client' :
return jsonify({"msg" : "Wrong type of user"})
user_id = current_user["_id"]
try:
collection = db["user_ratings"]
super_like = list(collection.find({"user_id":user_id, "rating":2}))
like = list(collection.find({"user_id":user_id, "rating":1}))
for i in range(len(super_like)):
super_like[i] = super_like[i]["picture"]
for i in range(len(like)):
like[i] = like[i]["picture"]
liked_picture = {"like" : tuple(like), "super_like" : tuple(super_like) }
return liked_picture
except:
return ''
@app.route("/images_uploaded", methods=["POST"])
@jwt_required
def images_uploaded():
current_user = get_jwt_identity()
if current_user["userType"] != 'company' :
return jsonify({"msg" : "Wrong type of user"})
company_id = current_user["_id"]
try:
collection = db["company_info"]
company_list_images = list(collection.find({"_id":ObjectId(company_id)}))[0]['images_uploaded']
return {"company_list_images" : tuple(company_list_images)}
except:
return ''
@app.errorhandler(404)
def page_not_found(e):
return '', 404
# run server
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5000, debug = True)
# app.run(host="0.0.0.0", port = os.environ["PORT"])