Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
cleanup projects view
Browse files Browse the repository at this point in the history
  • Loading branch information
PJDeSmijter committed Mar 11, 2024
1 parent c3e2adc commit 629ef66
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
46 changes: 14 additions & 32 deletions backend/pigeonhole/apps/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.shortcuts import get_object_or_404

from .models import Project, ProjectSerializer, Course
from .permissions import CanAccessProject
Expand All @@ -20,18 +21,16 @@ def list(self, request, *args, **kwargs):
serializer = ProjectSerializer(Project.objects.filter(course_id=course_id), many=True)

# Check whether the course exists
if not Course.objects.filter(course_id=course_id).exists():
return Response({"message": "Course does not exist."}, status=status.HTTP_404_NOT_FOUND)
get_object_or_404(Course, course_id=course_id)

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

def create(self, request, *args, **kwargs):
print("ceating new project")
print("creating new project")
course_id = kwargs.get('course_id')

# Check whether the course exists
if not Course.objects.filter(course_id=course_id).exists():
return Response({"message": "Course does not exist."}, status=status.HTTP_404_NOT_FOUND)
get_object_or_404(Course, course_id=course_id)

serializer = ProjectSerializer(data=request.data)
if serializer.is_valid():
Expand All @@ -45,14 +44,10 @@ def destroy(self, request, *args, **kwargs):
project_id = kwargs.get('pk')

# Check whether the course exists
if not Course.objects.filter(course_id=course_id).exists():
return Response({"message": "Course does not exist."}, status=status.HTTP_404_NOT_FOUND)
get_object_or_404(Course, course_id=course_id)

# Check whether the project exists
if not Project.objects.filter(pk=project_id).exists():
return Response({"message": "Project does not exist."}, status=status.HTTP_404_NOT_FOUND)

project = Project.objects.get(pk=project_id)
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)

Expand All @@ -61,30 +56,19 @@ def retrieve(self, request, *args, **kwargs):
project_id = kwargs.get('pk')

# Check whether the course exists
if not Course.objects.filter(course_id=course_id).exists():
return Response({"message": "Course does not exist."}, status=status.HTTP_404_NOT_FOUND)
get_object_or_404(Course, course_id=course_id)

# Check whether the project exists
if not Project.objects.filter(pk=project_id).exists():
return Response({"message": "Project does not exist."}, status=status.HTTP_404_NOT_FOUND)

serializer = ProjectSerializer(instance=Project.objects.get(pk=project_id), many=False)
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')

# Check whether the course exists
if not Course.objects.filter(course_id=course_id).exists():
return Response({"message": "Course does not exist."}, status=status.HTTP_404_NOT_FOUND)

# Check whether the project exists
if not Project.objects.filter(pk=project_id).exists():
return Response({"message": "Project does not exist."}, status=status.HTTP_404_NOT_FOUND)

project = Project.objects.get(pk=project_id)
project = get_object_or_404(Project, pk=project_id)
serializer = ProjectSerializer(project, data=request.data)
if serializer.is_valid():
serializer.save()
Expand All @@ -93,13 +77,11 @@ def update(self, request, *args, **kwargs):
def partial_update(self, request, *args, **kwargs):
instance = self.get_object()
# Check whether the course exists
if not Course.objects.filter(course_id=instance.course_id.course_id).exists():
return Response({"message": "Course does not exist."}, status=status.HTTP_404_NOT_FOUND)

get_object_or_404(Course, course_id=instance.course_id.course_id)

# Check whether the project exists
if not Project.objects.filter(pk=instance.project_id).exists():
return Response({"message": "Project does not exist."}, status=status.HTTP_404_NOT_FOUND)

get_object_or_404(Project, pk=instance.project_id)

serializer = self.get_serializer(instance, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
Expand Down
36 changes: 35 additions & 1 deletion backend/pigeonhole/tests/test_views/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def test_partial_update_project(self):
self.assertEqual(Project.objects.get(project_id=self.project.project_id).name, "Updated Test Project")

# tests with an invalid course

def test_create_project_invalid_course(self):
response = self.client.post(
API_ENDPOINT + f'100/projects/',
Expand All @@ -109,7 +110,40 @@ def test_create_project_invalid_course(self):
)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(Project.objects.count(), 1)


"""TODO
def test_update_project_invalid_course(self):
response = self.client.patch(
API_ENDPOINT + f'100/projects/{self.project.project_id}/',
{
"name": "Updated Test Project",
"description": "Updated Test Project Description",
"course_id": 100
},
format='json'
)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(Project.objects.get(project_id=self.project.project_id).name, "Test Project")
"""
def test_delete_project_invalid_course(self):
response = self.client.delete(
API_ENDPOINT + f'100/projects/{self.project.project_id}/'
)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(Project.objects.count(), 1)

"""TODO
def test_partial_update_project_invalid_course(self):
response = self.client.patch(
API_ENDPOINT + f'100/projects/{self.project.project_id}/',
{
"name": "Updated Test Project"
},
format='json'
)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(Project.objects.get(project_id=self.project.project_id).name, "Test Project")
"""
def test_retrieve_project_invalid_course(self):
response = self.client.get(
API_ENDPOINT + f'100/projects/{self.project.project_id}/'
Expand Down

0 comments on commit 629ef66

Please sign in to comment.