Skip to content

Commit 52ac09b

Browse files
authored
Merge pull request #221 from Seluj78/feature/recommendations
2 parents eac6215 + f0919e0 commit 52ac09b

File tree

13 files changed

+453
-74
lines changed

13 files changed

+453
-74
lines changed

PyMatcha.postman_collection.json

Lines changed: 147 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,7 +2656,7 @@
26562656
" pm.response.to.have.status(200);",
26572657
"});",
26582658
"",
2659-
"tests[\"Is geohash correct\"] = response.geohash == \"ezzw82vu4u1r\""
2659+
"tests[\"Is geohash correct\"] = response.geohash == \"spb8kg12wgw3\""
26602660
],
26612661
"type": "text/javascript"
26622662
}
@@ -5437,6 +5437,92 @@
54375437
],
54385438
"protocolProfileBehavior": {}
54395439
},
5440+
{
5441+
"name": "Recommendations",
5442+
"item": [
5443+
{
5444+
"name": "Start recommendation calculations",
5445+
"event": [
5446+
{
5447+
"listen": "test",
5448+
"script": {
5449+
"id": "c9b3da99-1f4b-4600-b5d6-4206123e4eb4",
5450+
"exec": [
5451+
"var response = JSON.parse(responseBody);",
5452+
"",
5453+
"",
5454+
"pm.test(\"Status code is 200\", function () {",
5455+
" pm.response.to.have.status(200);",
5456+
"});",
5457+
""
5458+
],
5459+
"type": "text/javascript"
5460+
}
5461+
}
5462+
],
5463+
"request": {
5464+
"method": "POST",
5465+
"header": [
5466+
{
5467+
"key": "debug-auth-token",
5468+
"value": "{{debug_token}}",
5469+
"type": "text"
5470+
}
5471+
],
5472+
"url": {
5473+
"raw": "{{host}}/debug/recommendations/start",
5474+
"host": [
5475+
"{{host}}"
5476+
],
5477+
"path": [
5478+
"debug",
5479+
"recommendations",
5480+
"start"
5481+
]
5482+
}
5483+
},
5484+
"response": []
5485+
},
5486+
{
5487+
"name": "Get user recommendations",
5488+
"event": [
5489+
{
5490+
"listen": "test",
5491+
"script": {
5492+
"id": "1e7dfd1f-a36c-448b-9ce3-acbc0af0a537",
5493+
"exec": [
5494+
"pm.test(\"Status code is 200\", function () {",
5495+
" pm.response.to.have.status(200);",
5496+
"});",
5497+
"",
5498+
"pm.test(\"Recommendations aren't null\", function () {",
5499+
" var jsonData = pm.response.json();",
5500+
" pm.expect(jsonData.recommendations).to.not.eql(null);",
5501+
"});",
5502+
""
5503+
],
5504+
"type": "text/javascript"
5505+
}
5506+
}
5507+
],
5508+
"request": {
5509+
"method": "GET",
5510+
"header": [],
5511+
"url": {
5512+
"raw": "{{host}}/recommendations",
5513+
"host": [
5514+
"{{host}}"
5515+
],
5516+
"path": [
5517+
"recommendations"
5518+
]
5519+
}
5520+
},
5521+
"response": []
5522+
}
5523+
],
5524+
"protocolProfileBehavior": {}
5525+
},
54405526
{
54415527
"name": "Reset",
54425528
"item": [
@@ -5532,6 +5618,48 @@
55325618
}
55335619
},
55345620
"response": []
5621+
},
5622+
{
5623+
"name": "Reset redis",
5624+
"event": [
5625+
{
5626+
"listen": "test",
5627+
"script": {
5628+
"id": "bb64517b-6835-4be3-9662-3ceede1a6316",
5629+
"exec": [
5630+
"pm.test(\"Status code is 200\", function () {",
5631+
" pm.response.to.have.status(204);",
5632+
"});",
5633+
""
5634+
],
5635+
"type": "text/javascript"
5636+
}
5637+
}
5638+
],
5639+
"request": {
5640+
"auth": {
5641+
"type": "noauth"
5642+
},
5643+
"method": "DELETE",
5644+
"header": [
5645+
{
5646+
"key": "debug-auth-token",
5647+
"type": "text",
5648+
"value": "{{debug_token}}"
5649+
}
5650+
],
5651+
"url": {
5652+
"raw": "{{host}}/debug/redis",
5653+
"host": [
5654+
"{{host}}"
5655+
],
5656+
"path": [
5657+
"debug",
5658+
"redis"
5659+
]
5660+
}
5661+
},
5662+
"response": []
55355663
}
55365664
],
55375665
"protocolProfileBehavior": {}
@@ -5595,92 +5723,92 @@
55955723
],
55965724
"variable": [
55975725
{
5598-
"id": "461c4e89-379c-4ecc-ab3d-e9a9ac5df859",
5726+
"id": "ae3c1742-39ab-4715-8465-cb39b5c29c05",
55995727
"key": "host",
56005728
"value": "http://127.0.0.1:5000"
56015729
},
56025730
{
5603-
"id": "b70cbb5c-c6d1-4a86-8001-5b11d2a86ebc",
5731+
"id": "e7d35862-55e7-4448-88f0-e8bc244b05b0",
56045732
"key": "user_id",
56055733
"value": ""
56065734
},
56075735
{
5608-
"id": "0b2b9986-ac92-4be7-9971-8c6cff053764",
5736+
"id": "e98b5a69-255c-49c1-8fa1-003c42ace8ac",
56095737
"key": "user_email",
56105738
"value": "foo@example.org"
56115739
},
56125740
{
5613-
"id": "a582fcb5-db29-4339-8734-d4c842f859a4",
5741+
"id": "affc13a3-4e03-4aa3-bd3b-0c2d3e90e037",
56145742
"key": "user_username",
56155743
"value": "foo"
56165744
},
56175745
{
5618-
"id": "c90dc190-68cc-42dd-b3f7-79b25c8097bd",
5746+
"id": "9ca9cb60-b44c-4b96-a537-a83070ae0615",
56195747
"key": "user_password",
56205748
"value": "admin"
56215749
},
56225750
{
5623-
"id": "c94d4c0a-5eb8-40aa-bfda-81b1af3edf17",
5751+
"id": "026847e3-4dc0-4939-a4f4-0678ed0c8125",
56245752
"key": "user_firstname",
56255753
"value": "Foo"
56265754
},
56275755
{
5628-
"id": "afde1579-3438-45dd-b7fc-063b6baa7d07",
5756+
"id": "695b6e88-1415-48b8-a12b-0b9662ade9e7",
56295757
"key": "user_lastname",
56305758
"value": "Bar"
56315759
},
56325760
{
5633-
"id": "418dab0a-b2b3-4a6a-82c3-50f1ae99d548",
5761+
"id": "7207a34c-d57b-40f2-b889-4c92033b283b",
56345762
"key": "debug_token",
56355763
"value": "xX69jules69Xx"
56365764
},
56375765
{
5638-
"id": "ce3f5bd7-c5d7-4d36-8b69-58fbb34c99cc",
5766+
"id": "1f4d2d09-e806-4790-8a9e-84bf7f0462a2",
56395767
"key": "user_access_token",
56405768
"value": ""
56415769
},
56425770
{
5643-
"id": "00529b58-2d48-49ef-b100-5717853dad2b",
5771+
"id": "b5761a25-0920-417f-9179-8c5be97097ac",
56445772
"key": "user_refresh_token",
56455773
"value": ""
56465774
},
56475775
{
5648-
"id": "2b58f894-ab87-46e9-a36f-e333381e9eb2",
5776+
"id": "2564fe00-7a58-4ce2-a10f-36692c2611f0",
56495777
"key": "expired_token",
56505778
"value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1OTAyNDIyNjQsIm5iZiI6MTU5MDI0MjI2NCwianRpIjoiNTM0ZGI5NzQtOWE4Ni00MGViLWE1NDEtMDg3N2ZmNzQ3NDVhIiwiZXhwIjoxNTkwMjQyMzI0LCJpZGVudGl0eSI6eyJpZCI6NTEyLCJlbWFpbCI6ImZvb0BleGFtcGxlLm9yZyIsInVzZXJuYW1lIjoiYmFyIiwiaXNfb25saW5lIjp0cnVlLCJkYXRlX2xhc3RzZWVuIjoiU2F0LCAyMyBNYXkgMjAyMCAxMzo1Nzo0NCBHTVQifSwiZnJlc2giOnRydWUsInR5cGUiOiJhY2Nlc3MifQ.NTp2P0WNkXDwzuzrcNIQdeAfizQ57HQgkzBLSvY1yHU"
56515779
},
56525780
{
5653-
"id": "7712423f-6e0e-4634-aee6-e6dde0b5a424",
5781+
"id": "2f511bbd-529c-4955-860c-67d059eac1c8",
56545782
"key": "user_orientation",
56555783
"value": "heterosexual"
56565784
},
56575785
{
5658-
"id": "b77f31fc-c749-418b-acfd-4387628b9d57",
5786+
"id": "ca85a202-0c8b-4d10-8262-63dd3cd86beb",
56595787
"key": "user_bio",
56605788
"value": "Lorem Ipsum is the single greatest threat. We are not - we are not keeping up with other websites. Lorem Ipsum best not make any more threats to your website. It will be met with fire and fury like the world has never seen. Does everybody know that pig named Lorem Ipsum? An ‘extremely credible source’ has called my office and told me that Barack Obama’s placeholder text is a fraud."
56615789
},
56625790
{
5663-
"id": "ae2a77eb-02ee-4439-a20b-023432b7ea16",
5791+
"id": "4a4205b9-65d4-4303-bf80-0954310b2a8d",
56645792
"key": "user_gender",
56655793
"value": "male"
56665794
},
56675795
{
5668-
"id": "273e5cdc-2313-4b17-a26c-f5f0a3207f30",
5796+
"id": "c8cf0f92-c24e-4ced-b567-d3eef6ce7789",
56695797
"key": "user_birthdate",
56705798
"value": "06/04/1998"
56715799
},
56725800
{
5673-
"id": "9942a431-5645-43b4-9e85-497d3b85be3e",
5801+
"id": "406075a2-5965-4491-9aa6-c879e3ac304e",
56745802
"key": "user_lat",
56755803
"value": "44.8404"
56765804
},
56775805
{
5678-
"id": "ead5923f-2d1b-4753-9df3-7ddd9b8e721a",
5806+
"id": "703dc3ff-eab2-4b17-b522-7a0a7bc6f894",
56795807
"key": "user_lng",
56805808
"value": "-0.5805"
56815809
},
56825810
{
5683-
"id": "4a54ddd0-4170-429b-9123-1b7aa11ba636",
5811+
"id": "a90d387f-4d5b-4832-9136-3d226844c30b",
56845812
"key": "user_ip",
56855813
"value": "77.205.46.181"
56865814
}

backend/PyMatcha/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,11 @@ def expired_token_callback(expired_token):
154154
host=os.getenv("REDIS_HOST") if not os.getenv("IS_DOCKER_COMPOSE") else "redis",
155155
port=os.getenv("REDIS_PORT", 6379),
156156
decode_responses=True,
157+
db=2,
157158
)
158159

160+
redis.flushdb()
161+
159162
from PyMatcha.models.user import get_user
160163

161164

@@ -172,14 +175,14 @@ def jwt_user_callback(identity):
172175
except NotFoundError:
173176
# The user who the server issues the token for was deleted in the db.
174177
return None
175-
redis.set("user:" + str(identity["id"]), datetime.datetime.utcnow().timestamp())
178+
redis.set("online_user:" + str(identity["id"]), datetime.datetime.utcnow().timestamp())
176179
return u
177180

178181

179182
@jwt.token_in_blacklist_loader
180183
def check_if_token_is_revoked(decrypted_token):
181184
jti = decrypted_token["jti"]
182-
entry = redis.get("jti:" + jti)
185+
entry = redis.get("is_revoked_jti:" + jti)
183186
if entry is None:
184187
return True
185188
return entry == "true"
@@ -197,6 +200,7 @@ def check_if_token_is_revoked(decrypted_token):
197200
from PyMatcha.routes.api.like import like_bp
198201
from PyMatcha.routes.api.match import match_bp
199202
from PyMatcha.routes.api.messages import messages_bp
203+
from PyMatcha.routes.api.recommendations import recommendations_bp
200204

201205
logging.debug("Registering Flask blueprints")
202206
application.register_blueprint(user_bp)
@@ -211,6 +215,7 @@ def check_if_token_is_revoked(decrypted_token):
211215
application.register_blueprint(like_bp)
212216
application.register_blueprint(match_bp)
213217
application.register_blueprint(messages_bp)
218+
application.register_blueprint(recommendations_bp)
214219

215220
if application.debug:
216221
logging.debug("Registering debug route")

backend/PyMatcha/routes/api/auth/login.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ def auth_login():
6868
access_jti = get_jti(access_token)
6969
refresh_jti = get_jti(refresh_token)
7070

71-
redis.set("jti:" + access_jti, "false", ACCESS_TOKEN_EXPIRES * 1.2)
72-
redis.set("jti:" + refresh_jti, "false", REFRESH_TOKEN_EXPIRES * 1.2)
71+
redis.set("is_revoked_jti:" + access_jti, "false", ACCESS_TOKEN_EXPIRES * 1.2)
72+
redis.set("is_revoked_jti:" + refresh_jti, "false", REFRESH_TOKEN_EXPIRES * 1.2)
7373

7474
current_app.logger.debug("/auth/login -> Returning access token for user {}".format(username))
75-
redis.set("user:" + str(u.id), datetime.datetime.utcnow().timestamp())
75+
redis.set("online_user:" + str(u.id), datetime.datetime.utcnow().timestamp())
7676
ret = {"access_token": access_token, "refresh_token": refresh_token, "is_profile_completed": u.is_profile_completed}
7777
return SuccessOutput("return", ret)
7878

@@ -83,21 +83,21 @@ def refresh():
8383
current_user = get_jwt_identity()
8484
access_token = create_access_token(identity=current_user)
8585
access_jti = get_jti(encoded_token=access_token)
86-
redis.set("jti:" + access_jti, "false", ACCESS_TOKEN_EXPIRES * 1.2)
86+
redis.set("is_revoked_jti:" + access_jti, "false", ACCESS_TOKEN_EXPIRES * 1.2)
8787
return SuccessOutput("access_token", access_token)
8888

8989

9090
@auth_login_bp.route("/auth/access_revoke", methods=["DELETE"])
9191
@jwt_required
9292
def logout():
9393
jti = get_raw_jwt()["jti"]
94-
redis.set("jti:" + jti, "true", ACCESS_TOKEN_EXPIRES * 1.2)
94+
redis.set("is_revoked_jti:" + jti, "true", ACCESS_TOKEN_EXPIRES * 1.2)
9595
return Success("Access token revoked")
9696

9797

9898
@auth_login_bp.route("/auth/refresh_revoke", methods=["DELETE"])
9999
@jwt_refresh_token_required
100100
def logout2():
101101
jti = get_raw_jwt()["jti"]
102-
redis.set("jti:" + jti, "true", REFRESH_TOKEN_EXPIRES * 1.2)
102+
redis.set("is_revoked_jti:" + jti, "true", REFRESH_TOKEN_EXPIRES * 1.2)
103103
return Success("Refresh token revoked")

0 commit comments

Comments
 (0)