From 7b5e84c4006b54bb80b039dc3dc9817f8e23c235 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Fri, 27 Oct 2023 23:49:06 +0800 Subject: [PATCH] Also update study group radius calculation for nearby study groups view --- stude/study_groups/views.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/stude/study_groups/views.py b/stude/study_groups/views.py index 8c70c97..381075a 100644 --- a/stude/study_groups/views.py +++ b/stude/study_groups/views.py @@ -137,20 +137,20 @@ class StudyGroupListNearView(generics.ListAPIView): distance=Distance('location', user_location)).filter(distance__lte=100) for group in studygroups: - # Get all StudentStatus locations of the group - group_locations = group.students.values_list('location', flat=True) - # Convert string locations to GEOSGeometry objects - point_locations = [fromstr(loc, srid=4326) - for loc in group_locations] - # 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 + # Get the number of students in a group + student_count = group.students.values_list( + 'user', flat=True).count() + # Each student will contribute 10 to the radius + radius = student_count * 10 + # 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