From 82c29e2310d3ba1f29065f7dff85e9a382d28f32 Mon Sep 17 00:00:00 2001 From: rdyselinck Date: Sun, 10 Mar 2024 18:57:58 +0100 Subject: [PATCH] Everything fixed to create submissions --- backend/pigeonhole/apps/groups/models.py | 2 +- backend/pigeonhole/apps/groups/views.py | 9 ++++ backend/pigeonhole/apps/projects/views.py | 3 ++ backend/pigeonhole/apps/submissions/views.py | 9 ++++ backend/pigeonhole/apps/users/models.py | 3 +- backend/pigeonhole/apps/users/views.py | 48 ++++++++++++++++++++ backend/pigeonhole/urls.py | 4 +- 7 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 backend/pigeonhole/apps/users/views.py diff --git a/backend/pigeonhole/apps/groups/models.py b/backend/pigeonhole/apps/groups/models.py index cf9bab6e..c806f3c9 100644 --- a/backend/pigeonhole/apps/groups/models.py +++ b/backend/pigeonhole/apps/groups/models.py @@ -19,4 +19,4 @@ class Group(models.Model): class GroupSerializer(serializers.ModelSerializer): class Meta: model = Group - fields = ["group_id", "group_nr", "final_score", "project_id", "student", "feedback", "project_id"] + fields = ["group_id", "group_nr", "final_score", "project_id", "student", "feedback"] diff --git a/backend/pigeonhole/apps/groups/views.py b/backend/pigeonhole/apps/groups/views.py index fe5473ec..23bbeca7 100644 --- a/backend/pigeonhole/apps/groups/views.py +++ b/backend/pigeonhole/apps/groups/views.py @@ -1,6 +1,7 @@ from rest_framework import viewsets, status from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response + from backend.pigeonhole.apps.groups.models import Group, GroupSerializer @@ -15,3 +16,11 @@ def perform_create(self, serializer): def list(self, request, *args, **kwargs): serializer = GroupSerializer(self.queryset, many=True) return Response(serializer.data, status=status.HTTP_200_OK) + + def create(self, request, *args, **kwargs): + serializer = GroupSerializer(data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) diff --git a/backend/pigeonhole/apps/projects/views.py b/backend/pigeonhole/apps/projects/views.py index 692c2cfa..e5db62ff 100644 --- a/backend/pigeonhole/apps/projects/views.py +++ b/backend/pigeonhole/apps/projects/views.py @@ -12,6 +12,9 @@ class ProjectViewSet(viewsets.ModelViewSet): serializer_class = ProjectSerializer permission_classes = [IsAuthenticated & CanAccessProject] + def perform_create(self, serializer): + serializer.save(user=self.request.user) + def create(self, request, *args, **kwargs): serializer = ProjectSerializer(data=request.data) if serializer.is_valid(): diff --git a/backend/pigeonhole/apps/submissions/views.py b/backend/pigeonhole/apps/submissions/views.py index 7079c1d5..8a03e474 100644 --- a/backend/pigeonhole/apps/submissions/views.py +++ b/backend/pigeonhole/apps/submissions/views.py @@ -1,6 +1,7 @@ from rest_framework import viewsets, status from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response + from backend.pigeonhole.apps.submissions.models import Submissions, SubmissionsSerializer @@ -15,3 +16,11 @@ def perform_create(self, serializer): def list(self, request, *args, **kwargs): serializer = SubmissionsSerializer(self.queryset, many=True) return Response(serializer.data, status=status.HTTP_200_OK) + + def create(self, request, *args, **kwargs): + serializer = SubmissionsSerializer(data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) diff --git a/backend/pigeonhole/apps/users/models.py b/backend/pigeonhole/apps/users/models.py index ce1c243d..4a35e705 100644 --- a/backend/pigeonhole/apps/users/models.py +++ b/backend/pigeonhole/apps/users/models.py @@ -17,7 +17,7 @@ def name(self): class UserSerializer(serializers.ModelSerializer): class Meta: model = User - fields = ['id', 'e_mail', 'first_name', 'last_name'] + fields = ['id', 'email', 'first_name', 'last_name'] class Student(models.Model): @@ -29,6 +29,7 @@ class Student(models.Model): class StudentSerializer(serializers.ModelSerializer): + id = serializers.PrimaryKeyRelatedField(queryset=User.objects.all()) class Meta: model = Student fields = ['number', 'course', 'id'] diff --git a/backend/pigeonhole/apps/users/views.py b/backend/pigeonhole/apps/users/views.py new file mode 100644 index 00000000..0af8843f --- /dev/null +++ b/backend/pigeonhole/apps/users/views.py @@ -0,0 +1,48 @@ +from rest_framework import viewsets, status +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response + +from backend.pigeonhole.apps.users.models import Student, StudentSerializer, User, UserSerializer + + +class StudentViewSet(viewsets.ModelViewSet): + queryset = Student.objects.all() + serializer_class = StudentSerializer + permission_classes = [IsAuthenticated] + + def create(self, request, *args, **kwargs): + serializer = StudentSerializer(data=request.data) + print(serializer) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + def perform_create(self, serializer): + serializer.save(user=self.request.user) + + def list(self, request, *args, **kwargs): + serializer = StudentSerializer(self.queryset, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) + + +class UserViewSet(viewsets.ModelViewSet): + queryset = User.objects.all() + serializer_class = UserSerializer + permission_classes = [IsAuthenticated] + + def list(self, request, *args, **kwargs): + serializer = UserSerializer(self.queryset, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) + + def perform_create(self, serializer): + serializer.save(user=self.request.user) + + def create(self, request, *args, **kwargs): + serializer = UserSerializer(data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) diff --git a/backend/pigeonhole/urls.py b/backend/pigeonhole/urls.py index c965476d..3676ea4a 100644 --- a/backend/pigeonhole/urls.py +++ b/backend/pigeonhole/urls.py @@ -7,6 +7,7 @@ from backend.pigeonhole.apps.projects.views import ProjectViewSet from backend.pigeonhole.apps.submissions.views import SubmissionsViewset from backend.pigeonhole.apps.groups.views import GroupViewSet +from backend.pigeonhole.apps.users.views import StudentViewSet, UserViewSet #from backend.pigeonhole.apps.projects import views as project_views from drf_yasg.views import get_schema_view from drf_yasg import openapi @@ -25,7 +26,8 @@ ) router = routers.DefaultRouter() -router.register(r'users', test_views.UserViewSet) +router.register(r'users', UserViewSet) +router.register(r'students', StudentViewSet) router.register(r'groups', GroupViewSet) router.register(r'courses', CourseViewSet) router.register(r'courses//projects/', ProjectViewSet)