mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-16 22:09:25 +08:00
Reworked study group radius calculation to be based on the number of students with a set limit rather than by largest distance
This commit is contained in:
parent
0007f84b47
commit
f6e0483706
1 changed files with 13 additions and 12 deletions
|
@ -89,20 +89,21 @@ class StudyGroupListView(generics.ListAPIView):
|
|||
studygroups = StudyGroup.objects.filter(subject__in=user_subjects)
|
||||
|
||||
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]
|
||||
# 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
|
||||
|
||||
# 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)
|
||||
# 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 = group_radius
|
||||
group.radius = radius
|
||||
|
||||
return studygroups
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue