2023-06-27 21:12:03 +08:00
|
|
|
from django.shortcuts import render
|
2023-06-27 21:35:19 +08:00
|
|
|
from rest_framework import generics
|
2023-06-27 22:57:37 +08:00
|
|
|
from rest_framework.exceptions import PermissionDenied
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2023-06-27 21:35:19 +08:00
|
|
|
from .serializers import StudyGroupSerializer
|
|
|
|
from .models import StudyGroup
|
2023-07-26 23:32:03 +08:00
|
|
|
from subjects.models import Subject
|
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-06-27 21:12:03 +08:00
|
|
|
# Create your views here.
|
2023-06-27 21:35:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if not user.is_student:
|
|
|
|
raise PermissionDenied(
|
|
|
|
"You must be a student to view study groups"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get the user's course
|
|
|
|
user_course = user.course
|
|
|
|
print(user_course)
|
2023-07-26 12:14:19 +08:00
|
|
|
|
|
|
|
# Get subject names related to the user's course
|
2023-07-26 23:32:03 +08:00
|
|
|
subject_names = Subject.objects.filter(
|
2023-06-27 22:57:37 +08:00
|
|
|
course=user_course
|
|
|
|
).values_list('subject', flat=True)
|
|
|
|
|
2023-07-26 12:14:19 +08:00
|
|
|
print(subject_names)
|
2023-06-27 22:57:37 +08:00
|
|
|
|
2023-07-26 12:14:19 +08:00
|
|
|
# Now fetch the StudyGroups with the matching subject names
|
|
|
|
studygroups = StudyGroup.objects.filter(subject_name__in=subject_names)
|
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):
|
|
|
|
user = self.request.user
|
|
|
|
user_status = StudentStatus.objects.filter(user=user).first()
|
|
|
|
user_location = fromstr(
|
|
|
|
user_status.location, srid=4326)
|
|
|
|
|
|
|
|
if user_status.active is False:
|
|
|
|
raise exceptions.ValidationError("Student Status is not active")
|
|
|
|
|
|
|
|
if not user.is_student:
|
|
|
|
raise PermissionDenied(
|
|
|
|
"You must be a student to view study groups"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get the user's course
|
|
|
|
user_course = user.course
|
|
|
|
print(user_course)
|
|
|
|
|
|
|
|
# Get subject names related to the user's course
|
|
|
|
subject_names = Subject.objects.filter(
|
|
|
|
course=user_course
|
|
|
|
).values_list('subject', flat=True)
|
|
|
|
|
|
|
|
print(subject_names)
|
|
|
|
|
|
|
|
# Now fetch the StudyGroups with the matching subject names that are within 50m
|
|
|
|
studygroups = StudyGroup.objects.filter(subject_name__in=subject_names).annotate(
|
|
|
|
distance=Distance('location', user_location)).filter(distance__lte=50)
|
|
|
|
return studygroups
|
|
|
|
|
|
|
|
|
2023-06-27 21:35:19 +08:00
|
|
|
class StudyGroupMembershipViewSet(generics.ListAPIView):
|
|
|
|
serializer_class = StudyGroupSerializer
|
|
|
|
queryset = StudyGroup.objects.all()
|