-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CRUD for Pictures and Users (extensively tested with Postman, n…
…o test cases written yet)
- Loading branch information
Damien Tschan
committed
Mar 21, 2024
1 parent
97bbf74
commit 5d9be73
Showing
6 changed files
with
95 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .user import UserRegistrationSerializer, UserConfigurationSerializer | ||
from .user import UserSerializer, UserRegistrationSerializer, UserConfigurationSerializer | ||
from .tag import TagSerializer | ||
from .picture import PictureSerializer | ||
from .like import LikeSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .user import UserView | ||
from .tag import TagListView | ||
from .tag import TagListView | ||
from .picture import PictureView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from rest_framework.permissions import IsAuthenticated | ||
from ..models import Picture | ||
from ..serializers import PictureSerializer | ||
|
||
import random | ||
|
||
class PictureView(APIView): | ||
|
||
permission_classes = [IsAuthenticated] | ||
|
||
def post(self, request): | ||
serializer = PictureSerializer(data=request.data) | ||
if serializer.is_valid(): | ||
if request.user.id == int(request.data["user"]): | ||
serializer.save() | ||
return Response({"message": "Picture added successfully."}, status=status.HTTP_201_CREATED) | ||
return Response({"message": "Picture cannot be added to another user."}, status=status.HTTP_403_FORBIDDEN) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
def get(self, request): | ||
id = request.data["id"] | ||
if Picture.objects.filter(id = id).exists(): | ||
pic = Picture.objects.get(id = id) | ||
serializer = PictureSerializer(pic, many=False) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
else: | ||
return Response({"message": "Picture not found."}, status=status.HTTP_404_NOT_FOUND) | ||
|
||
def delete(self, request): | ||
id = int(request.data["id"]) | ||
if Picture.objects.filter(id = id).exists(): | ||
pic = Picture.objects.get(id = id) | ||
if request.user.id == pic.user.id: | ||
pic.delete() | ||
return Response({"message": "Picture successfully deleted."},status=status.HTTP_204_NO_CONTENT) | ||
return Response({"message": "Picture cannot be deleted by another user."}, status=status.HTTP_403_FORBIDDEN) | ||
return Response({"message": "Picture not found."}, status=status.HTTP_404_NOT_FOUND) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters