Also update study group radius calculation for nearby study groups view

This commit is contained in:
Keannu Bernasol 2023-10-27 23:49:06 +08:00
parent f6e0483706
commit 7b5e84c400

View file

@ -137,20 +137,20 @@ class StudyGroupListNearView(generics.ListAPIView):
distance=Distance('location', user_location)).filter(distance__lte=100) distance=Distance('location', user_location)).filter(distance__lte=100)
for group in studygroups: for group in studygroups:
# Get all StudentStatus locations of the group # Get the number of students in a group
group_locations = group.students.values_list('location', flat=True) student_count = group.students.values_list(
# Convert string locations to GEOSGeometry objects 'user', flat=True).count()
point_locations = [fromstr(loc, srid=4326) # Each student will contribute 10 to the radius
for loc in group_locations] radius = student_count * 10
# Calculate distances between every pair of locations
distances = [(loc1.distance(
loc2)*100000)for loc1 in point_locations for loc2 in point_locations]
# Get the maximum distance
group_radius = max(distances) if distances else 0
group_radius = max(group_radius, 15)
# Annotate the group with the radius
group.radius = group_radius
# If the radius exceeds 50, set it back to 50
if (radius > 50):
radius = 50
# (For debug purposes) If radius is less than 5, set it back to 5
elif (radius < 5):
radius = 5
# Annotate the group with the radius
group.radius = radius
return studygroups return studygroups