Skip to content

Commit 5052fcd

Browse files
committed
Base code for the search route
1 parent da95889 commit 5052fcd

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

backend/PyMatcha/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def check_if_token_is_revoked(decrypted_token):
227227
from PyMatcha.routes.api.messages import messages_bp
228228
from PyMatcha.routes.api.recommendations import recommendations_bp
229229
from PyMatcha.routes.api.profile.images import images_bp
230+
from PyMatcha.routes.api.search import search_bp
230231

231232
logging.debug("Registering Flask blueprints")
232233
application.register_blueprint(user_bp)
@@ -243,6 +244,7 @@ def check_if_token_is_revoked(decrypted_token):
243244
application.register_blueprint(messages_bp)
244245
application.register_blueprint(recommendations_bp)
245246
application.register_blueprint(images_bp)
247+
application.register_blueprint(search_bp)
246248

247249
if application.debug:
248250
logging.debug("Registering debug route")
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import datetime
2+
3+
from flask import Blueprint
4+
from flask import request
5+
from flask_jwt_extended import current_user
6+
from flask_jwt_extended import jwt_required
7+
from PyMatcha.utils.decorators import validate_params
8+
from PyMatcha.utils.match_score import _get_common_tags
9+
from PyMatcha.utils.match_score import _get_distance
10+
from PyMatcha.utils.match_score import _get_gender_query
11+
from PyMatcha.utils.success import SuccessOutput
12+
13+
search_bp = Blueprint("search", __name__)
14+
15+
REQUIRED_PARAMS_SEARCH = {
16+
"min_age": int,
17+
"max_age": int,
18+
"min_score": int,
19+
"max_score": int,
20+
"tags": list,
21+
"max_distance": int,
22+
}
23+
24+
25+
@search_bp.route("/search", methods=["POST"])
26+
@validate_params(REQUIRED_PARAMS_SEARCH)
27+
@jwt_required
28+
def search():
29+
data = request.get_json()
30+
min_age = int(data["min_age"])
31+
max_age = int(data["max_age"])
32+
min_score = int(data["min_score"])
33+
max_score = int(data["max_score"])
34+
max_distance = int(data["max_distance"])
35+
tags = data["tags"]
36+
37+
today = datetime.datetime.utcnow()
38+
39+
query = _get_gender_query(current_user.orientation, current_user.gender)
40+
returned_list = []
41+
for user in query:
42+
user_age = (
43+
today.year - user.birthdate.year - ((today.month, today.day) < (user.birthdate.month, user.birthdate.day))
44+
)
45+
46+
if max_age != -1:
47+
if user_age > max_age:
48+
continue
49+
if min_age != -1:
50+
if user_age < min_age:
51+
continue
52+
53+
if max_score != -1:
54+
if user.heat_score > max_score:
55+
continue
56+
if min_score != -1:
57+
if user.heat_score < min_score:
58+
continue
59+
60+
if max_distance != -1:
61+
distance = _get_distance(current_user.geohash, user.geohash)
62+
if distance > max_distance:
63+
continue
64+
65+
if tags:
66+
user_tags = [t.name for t in user.get_tags()]
67+
common_tags = _get_common_tags(tags, user_tags)
68+
if not common_tags:
69+
continue
70+
71+
returned_list.append(user.to_dict())
72+
return SuccessOutput("search_results", returned_list)

0 commit comments

Comments
 (0)