Skip to content

Commit

Permalink
Everything fixed to create submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
RunoBoy committed Mar 10, 2024
1 parent 2f9a381 commit 82c29e2
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backend/pigeonhole/apps/groups/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
9 changes: 9 additions & 0 deletions backend/pigeonhole/apps/groups/views.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)
3 changes: 3 additions & 0 deletions backend/pigeonhole/apps/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
9 changes: 9 additions & 0 deletions backend/pigeonhole/apps/submissions/views.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)
3 changes: 2 additions & 1 deletion backend/pigeonhole/apps/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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']
Expand Down
48 changes: 48 additions & 0 deletions backend/pigeonhole/apps/users/views.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 3 additions & 1 deletion backend/pigeonhole/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/<int:course_id>/projects/<project_id>', ProjectViewSet)
Expand Down

0 comments on commit 82c29e2

Please sign in to comment.