mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-16 22:09:25 +08:00
Added initial study group creation, joining, and deletion functionality
This commit is contained in:
parent
268069eeb4
commit
f9a3e7a440
1 changed files with 42 additions and 3 deletions
|
@ -9,15 +9,55 @@ 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
|
||||
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
# Create your views here.
|
||||
|
||||
|
||||
class StudyGroupListView(generics.ListAPIView):
|
||||
class StudyGroupListView(generics.ListCreateAPIView, generics.UpdateAPIView, generics.DestroyAPIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
serializer_class = StudyGroupSerializer
|
||||
queryset = StudyGroup.objects.all()
|
||||
|
||||
def partial_update(self, instance, request, *args, **kwargs):
|
||||
# Ensure only "users" field is being updated
|
||||
if set(request.data.keys()) != {"users"}:
|
||||
return Response({"detail": "Only the 'users' field can be updated."}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Get the current list of users
|
||||
instance = self.get_object()
|
||||
current_users = set(instance.users.values_list('id', flat=True))
|
||||
|
||||
# Get the new list of users from the request
|
||||
new_users = set(request.data['users'])
|
||||
|
||||
# Check if the only difference between the two sets is the current user
|
||||
diff = current_users.symmetric_difference(new_users)
|
||||
if len(diff) > 1 or (len(diff) == 1 and request.user.id not in diff):
|
||||
return Response({"detail": "You can only add or remove yourself from the study group."}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Delete the study group if there are no users left
|
||||
instance = self.get_object()
|
||||
if not instance.users.exists():
|
||||
instance.delete()
|
||||
|
||||
return super().partial_update(request, *args, **kwargs)
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
|
||||
# Check if the current user is the creator of the study group
|
||||
# Assuming 'date_joined' is a field in your User model
|
||||
creator = instance.users.all().first()
|
||||
if request.user != creator:
|
||||
return Response({"detail": "Only the creator can delete the study group."}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Check if the current user is the only one in the study group
|
||||
if instance.users.count() > 1:
|
||||
return Response({"detail": "The study group cannot be deleted if there are other users in it."}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
return super().destroy(request, *args, **kwargs)
|
||||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
|
||||
|
@ -52,7 +92,6 @@ class StudyGroupListView(generics.ListAPIView):
|
|||
group_radius = max(group_radius, 15)
|
||||
# Annotate the group with the radius
|
||||
group.radius = group_radius
|
||||
print('test', studygroups)
|
||||
return studygroups
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue