-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_functions.py
69 lines (48 loc) · 2 KB
/
search_functions.py
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
import geocoder
#import correlation
from math import cos, pi, sqrt
from flask import (Flask, render_template, redirect, request, flash, session, jsonify, url_for)
from model import User, Message, Compatibility, connect_to_db, db
from similarity import euclid, square_rooted, cosine_similarity, corrcoef
def get_radius():
"""Get distance from search form and calc radius."""
#set radius to distance selected in form
distance = request.args.get('distance')
# print(distance)
radius = int(distance)
print(radius)
return radius
def get_pace():
"""Get pace from search form"""
#set pace to pace query
pace = request.args.get('pace')
print(pace)
return pace
def calculate_search_grid():
"""calculate square grid for search using userid saved
in session for lat/long center point, return user list"""
#save user in session as center user
center_user = User.query.get(session['user_id'])
#get user's name
center_user_name = center_user.name
#get lat and longitude for center user
my_lat = center_user.lat
my_long = center_user.lng
radius = get_radius()
pace = get_pace()
df = (radius/69)
dl = ((df)/(cos((my_lat)*(pi/180))))
southernmost_lat = (my_lat - df)
northernmost_lat = (my_lat + df)
westernmost_long = (my_long - dl)
easternmost_long = (my_long + dl)
#search for users/runners who meet criteria in form
# save query as object list
user_list = User.query.filter((User.lat > southernmost_lat) &
(User.lat < northernmost_lat) &
(User.lng > westernmost_long) &
(User.lng < easternmost_long) &
(User.pace == pace)&
(User.user_id != center_user.user_id)).all()
print(user_list)
return user_list