|
| 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