Skip to content

Commit

Permalink
fix groups
Browse files Browse the repository at this point in the history
  • Loading branch information
PJDeSmijter committed Mar 13, 2024
1 parent 0b55b17 commit f660e21
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 134 deletions.
69 changes: 3 additions & 66 deletions backend/pigeonhole/apps/groups/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from django.shortcuts import get_object_or_404
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from backend.pigeonhole.apps.courses.models import Course
from backend.pigeonhole.apps.groups.models import Group, GroupSerializer
from backend.pigeonhole.apps.projects.models import Project
from backend.pigeonhole.apps.groups.models import Group, GroupSerializer
from .permission import CanAccessGroup

from django.shortcuts import get_object_or_404

# TODO tests for score/max_score
# TODO bij de update/partial update zorgen dat de user bestaat, anders errors

class GroupViewSet(viewsets.ModelViewSet):
queryset = Group.objects.all()
Expand All @@ -35,65 +33,4 @@ def list(self, request, *args, **kwargs):
if request.user.id not in group["user"]:
del group["final_score"]
del group["feedback"]
return Response(serializer.data, status=status.HTTP_200_OK)

def retrieve(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')
project_id = kwargs.get('project_id')
group_nr = kwargs.get('group_nr')

# Check whether the course exists
get_object_or_404(Course, course_id=course_id)
# Check whether the project exists
get_object_or_404(Project, pk=project_id)
group = get_object_or_404(Group, project_id=project_id, group_nr=group_nr)
user = request.user
serializer = GroupSerializer(instance=group, many=False)
if user.is_superuser or user.is_staff or user.is_teacher:
serializer = GroupSerializer(instance=group, many=False)
return Response(serializer.data, status=status.HTTP_200_OK)
elif user.is_student:
return Response(serializer.get_visible_data(), status=status.HTTP_200_OK)

def update(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')
project_id = kwargs.get('project_id')
group_nr = kwargs.get('group_nr')
get_object_or_404(Course, course_id=course_id)
get_object_or_404(Project, project_id=project_id)
group = get_object_or_404(Group, group_nr=group_nr, project_id=project_id)
serializer = GroupSerializer(instance=group, data=request.data, partial=False)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)

def partial_update(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')
project_id = kwargs.get('project_id')
group_nr = kwargs.get('group_nr')
get_object_or_404(Course, course_id=course_id)
get_object_or_404(Project, project_id=project_id)
group = get_object_or_404(Group, group_nr=group_nr, project_id=project_id)
serializer = GroupSerializer(instance=group, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)

def create(self, request, *args, **kwargs):
return Response({"message": "You can't create groups"}, status=status.HTTP_400_BAD_REQUEST)

def destroy(self, request, *args, **kwargs):
return Response({"message": "You can't delete groups"}, status=status.HTTP_400_BAD_REQUEST)

@action(detail=True, methods=['post'])
def join_group(self, request, *args, **kwargs):
# TODO hier nog checks toevoegen, maar je kan een group joinen
course_id = kwargs.get('course_id')
project_id = kwargs.get('project_id')
group_nr = kwargs.get('group_nr')
get_object_or_404(Course, course_id=course_id)
get_object_or_404(Project, project_id=project_id)
group = get_object_or_404(Group, project_id=project_id, group_nr=group_nr)

group.user.add(request.user)
return Response(status=status.HTTP_200_OK)
return Response(serializer.data, status=status.HTTP_200_OK)
67 changes: 0 additions & 67 deletions backend/pigeonhole/apps/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ class ProjectViewSet(viewsets.ModelViewSet):
serializer_class = ProjectSerializer
permission_classes = [IsAuthenticated & CanAccessProject]

def list(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')
serializer = ProjectSerializer(Project.objects.filter(course_id=course_id), many=True)

# Check whether the course exists
get_object_or_404(Course, course_id=course_id)

return Response(serializer.data, status=status.HTTP_200_OK)

def create(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')

Expand All @@ -46,64 +37,6 @@ def create(self, request, *args, **kwargs):
return Response({"message": "You are not allowed to create a new project."},
status=status.HTTP_400_BAD_REQUEST)

def destroy(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')
project_id = kwargs.get('pk')

if request.user.is_teacher or request.user.is_admin or request.user.is_superuser:
# Check whether the course exists
get_object_or_404(Course, course_id=course_id)

# Check whether the project exists
project = get_object_or_404(Project, pk=project_id)
project.delete()
return Response({"message": "Project has been deleted successfully."},
status=status.HTTP_204_NO_CONTENT)
return Response({"message": "You are not allowed to delete this project."},
status=status.HTTP_403_FORBIDDEN)

def retrieve(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')
project_id = kwargs.get('pk')

# Check whether the course exists
get_object_or_404(Course, course_id=course_id)

# Check whether the project exists
project = get_object_or_404(Project, pk=project_id)
serializer = ProjectSerializer(instance=project, many=False)

return Response(serializer.data, status=status.HTTP_200_OK)

def update(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')
project_id = kwargs.get('pk')

if request.user.is_teacher or request.user.is_admin or request.user.is_superuser:
get_object_or_404(Course, course_id=course_id)

project = get_object_or_404(Project, pk=project_id)
serializer = ProjectSerializer(project, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response({"message": "You are not allowed to update this project."},
status=status.HTTP_403_FORBIDDEN)

def partial_update(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')
project_id = kwargs.get('pk')

if request.user.is_teacher or request.user.is_admin or request.user.is_superuser:
# Check whether the course exists
get_object_or_404(Course, course_id=course_id)

# Check whether the project exists
project = get_object_or_404(Project, pk=project_id)

serializer = ProjectSerializer(project, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response({"message": "You are not allowed to update this project."},
status=status.HTTP_403_FORBIDDEN)
2 changes: 1 addition & 1 deletion backend/pigeonhole/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
router.register(r'users', UserViewSet)
router.register(r'courses', CourseViewSet)
router.register(r'courses/(?P<course_id>[^/.]+)/projects', ProjectViewSet)
router.register(r'courses/(?P<course_id>[^/.]+)/projects/(?P<project_id>[^/.]+)/groups', GroupViewSet)
router.register(r'groups', GroupViewSet)
router.register(r'courses/(?P<course_id>[^/.]+)/'
r'projects/(?P<project_id>[^/.]+)/'
r'groups/(?P<group_id>[^/.]+)/submissions', SubmissionsViewset)
Expand Down

0 comments on commit f660e21

Please sign in to comment.