2023-06-27 21:12:03 +08:00
|
|
|
from django.shortcuts import render
|
2023-09-25 21:09:31 +08:00
|
|
|
from rest_framework import generics, mixins
|
2023-06-27 22:57:37 +08:00
|
|
|
from rest_framework.exceptions import PermissionDenied
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2023-09-26 20:28:59 +08:00
|
|
|
from .serializers import StudyGroupSerializer, StudyGroupCreateSerializer
|
2023-06-27 21:35:19 +08:00
|
|
|
from .models import StudyGroup
|
2023-09-24 21:03:32 +08:00
|
|
|
from subjects.models import Subject, SubjectInstance
|
2023-09-20 21:11:53 +08:00
|
|
|
from student_status.models import StudentStatus
|
|
|
|
from rest_framework import generics, viewsets, exceptions
|
|
|
|
from django.contrib.gis.geos import fromstr
|
|
|
|
from django.contrib.gis.db.models.functions import Distance
|
2023-09-24 21:54:14 +08:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.response import Response
|
2023-09-25 21:09:31 +08:00
|
|
|
from rest_framework import permissions
|
|
|
|
|
2023-06-27 21:12:03 +08:00
|
|
|
# Create your views here.
|
2023-06-27 21:35:19 +08:00
|
|
|
|
|
|
|
|
2023-09-26 20:28:59 +08:00
|
|
|
class StudyGroupCreateView(viewsets.ModelViewSet):
|
|
|
|
http_method_names = ['patch', 'post', 'delete']
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
serializer_class = StudyGroupCreateSerializer
|
|
|
|
queryset = StudyGroup.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class StudyGroupListView(generics.ListAPIView):
|
2023-06-27 22:57:37 +08:00
|
|
|
permission_classes = [IsAuthenticated]
|
2023-06-27 21:35:19 +08:00
|
|
|
serializer_class = StudyGroupSerializer
|
|
|
|
queryset = StudyGroup.objects.all()
|
|
|
|
|
2023-06-27 22:57:37 +08:00
|
|
|
def get_queryset(self):
|
|
|
|
user = self.request.user
|
|
|
|
|
|
|
|
# Get the user's course
|
|
|
|
user_course = user.course
|
|
|
|
print(user_course)
|
2023-07-26 12:14:19 +08:00
|
|
|
|
2023-09-24 21:03:32 +08:00
|
|
|
# Get the subject name of the student's subjects from SubjectInstance
|
|
|
|
user_subject_names = user.subjects.values_list('subject', flat=True)
|
|
|
|
print('user subjects:', user_subject_names)
|
|
|
|
# Get the corresponding Subject models
|
|
|
|
user_subjects = Subject.objects.filter(name__in=user_subject_names)
|
|
|
|
# Now fetch the StudyGroups with the matching Subject models
|
|
|
|
studygroups = StudyGroup.objects.filter(subject__in=user_subjects)
|
2023-06-27 22:57:37 +08:00
|
|
|
|
2023-09-24 21:09:38 +08:00
|
|
|
for group in studygroups:
|
|
|
|
# Get all StudentStatus locations of the group
|
2023-09-28 20:09:24 +08:00
|
|
|
group_locations = group.students.values_list('location', flat=True)
|
2023-09-24 21:09:38 +08:00
|
|
|
# Convert string locations to GEOSGeometry objects
|
|
|
|
point_locations = [fromstr(loc, srid=4326)
|
|
|
|
for loc in group_locations]
|
2023-09-28 20:09:24 +08:00
|
|
|
|
2023-09-24 21:09:38 +08:00
|
|
|
# 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
|
2023-06-27 22:57:37 +08:00
|
|
|
return studygroups
|
|
|
|
|
2023-06-27 21:35:19 +08:00
|
|
|
|
2023-09-20 21:11:53 +08:00
|
|
|
class StudyGroupListNearView(generics.ListAPIView):
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
serializer_class = StudyGroupSerializer
|
|
|
|
queryset = StudyGroup.objects.all()
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2023-09-28 21:03:34 +08:00
|
|
|
|
2023-09-20 21:11:53 +08:00
|
|
|
user = self.request.user
|
|
|
|
user_status = StudentStatus.objects.filter(user=user).first()
|
|
|
|
|
2023-09-24 21:33:16 +08:00
|
|
|
if user_status.active is False:
|
|
|
|
raise exceptions.ValidationError("Student Status is not active")
|
|
|
|
|
2023-09-28 21:03:34 +08:00
|
|
|
user_location = fromstr(
|
|
|
|
user_status.location, srid=4326)
|
|
|
|
|
2023-09-20 21:11:53 +08:00
|
|
|
# Get the user's course
|
|
|
|
user_course = user.course
|
|
|
|
print(user_course)
|
|
|
|
|
2023-09-24 21:03:32 +08:00
|
|
|
# Get the subject name of the student's subjects from SubjectInstance
|
|
|
|
user_subject_names = user.subjects.values_list('subject', flat=True)
|
|
|
|
# Get the corresponding Subject models
|
|
|
|
user_subjects = Subject.objects.filter(name__in=user_subject_names)
|
|
|
|
|
|
|
|
# Now fetch the StudyGroups with the matching Subject models that are within 100m
|
|
|
|
studygroups = StudyGroup.objects.filter(subject__in=user_subjects).annotate(
|
|
|
|
distance=Distance('location', user_location)).filter(distance__lte=100)
|
|
|
|
|
|
|
|
for group in studygroups:
|
|
|
|
# Get all StudentStatus locations of the group
|
2023-09-28 20:09:24 +08:00
|
|
|
group_locations = group.students.values_list('location', flat=True)
|
2023-09-24 21:03:32 +08:00
|
|
|
# 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
|
2023-09-20 21:11:53 +08:00
|
|
|
|
|
|
|
return studygroups
|
|
|
|
|
|
|
|
|
2023-09-25 21:09:31 +08:00
|
|
|
class IsOwnerOrReadOnly(permissions.BasePermission):
|
|
|
|
def has_object_permission(self, request, view, obj):
|
|
|
|
# Check if the requesting user's student status matches the user field
|
|
|
|
return obj.user == request.user.studentstatus
|