-
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.
Merge pull request #19 from HE-Arc/revert-18-pictures
Revert "Pictures"
- Loading branch information
Showing
33 changed files
with
422 additions
and
934 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,4 +167,3 @@ frontend/package-lock.lock | |
.vscode/ | ||
|
||
backend/pictures | ||
backend/schema.yml |
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
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
22 changes: 0 additions & 22 deletions
22
backend/kodecupidapp/migrations/0004_remove_tag_users_user_tags.py
This file was deleted.
Oops, something went wrong.
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
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,9 +1,7 @@ | ||
from rest_framework import serializers | ||
from ..models import Picture | ||
|
||
|
||
class PictureSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = Picture | ||
fields = ['id', 'image', 'user'] | ||
fields = ['id', 'image', 'user'] |
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,5 +1,4 @@ | ||
from .like import LikeView | ||
from .user import UserView | ||
from .user import UserView, UserTagView | ||
from .tag import TagView | ||
from .picture import PictureView | ||
from .swipe import SwipeView | ||
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
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,51 +1,41 @@ | ||
from rest_framework.viewsets import GenericViewSet | ||
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin, ListModelMixin, DestroyModelMixin | ||
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 | ||
|
||
from django.http import HttpResponse | ||
import random | ||
|
||
import os | ||
class PictureView(APIView): | ||
|
||
|
||
class PictureView(GenericViewSet, CreateModelMixin, RetrieveModelMixin, DestroyModelMixin): | ||
queryset = Picture.objects.all() | ||
serializer_class = PictureSerializer | ||
permission_classes = [IsAuthenticated] | ||
|
||
def create(self, request): | ||
modified_data = request.data.copy() | ||
modified_data['user'] = request.user.id | ||
|
||
serializer = self.get_serializer(data=modified_data) | ||
def post(self, request): | ||
serializer = PictureSerializer(data=request.data) | ||
if serializer.is_valid(): | ||
instance = serializer.save() | ||
# Access the ID of the newly created instance | ||
new_id = instance.id | ||
return Response({"message": "Picture added successfully.", "id": new_id}, status=status.HTTP_201_CREATED) | ||
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 retrieve(self, request, pk=None): | ||
instance = self.get_object() | ||
|
||
# Get the image path | ||
image_path = instance.image.path | ||
# Read the image data | ||
with open(image_path, 'rb') as image_file: | ||
image_data = image_file.read() | ||
|
||
# Return the image data in an HTTP response | ||
response = HttpResponse(image_data, content_type='image/jpeg') | ||
response['Content-Disposition'] = 'attachment; filename="image.jpg"' | ||
return response | ||
|
||
|
||
def destroy(self, request): | ||
instance = self.get_object() | ||
if request.user.id == instance.user.id: | ||
self.perform_destroy(instance) | ||
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) | ||
|
||
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 was deleted.
Oops, something went wrong.
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,15 +1,54 @@ | ||
from rest_framework.viewsets import GenericViewSet | ||
from rest_framework.mixins import ListModelMixin | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import status | ||
from ..models import Tag | ||
from ..serializers import TagSerializer | ||
from ..models import User | ||
|
||
class TagView(GenericViewSet, ListModelMixin): | ||
queryset = Tag.objects.all() | ||
serializer_class = TagSerializer | ||
|
||
def list(self, request, *args, **kwargs): | ||
queryset = self.get_queryset() | ||
serializer = self.get_serializer(queryset, many=True) | ||
class TagView(APIView): | ||
|
||
def get(self, request): | ||
tags = Tag.objects.all() | ||
serializer = TagSerializer(tags, many=True) | ||
return Response(serializer.data) | ||
|
||
def post(self, request): | ||
|
||
serializer = TagSerializer(data=request.data) | ||
|
||
if not serializer.is_valid(): | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
user = request.user | ||
|
||
tag_name = serializer.validated_data['name'] | ||
tag = get_object_or_404(Tag, name=tag_name) | ||
|
||
if user in tag.users.all(): | ||
return Response({'message': 'User already has tag'}, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
tag.users.add(user) | ||
|
||
return Response({'message': 'Tag added to user'}, status=status.HTTP_201_CREATED) | ||
|
||
|
||
def delete(self, request): | ||
|
||
serializer = TagSerializer(data=request.data) | ||
|
||
if not serializer.is_valid(): | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
user = request.user | ||
|
||
tag_name = serializer.validated_data['name'] | ||
tag = get_object_or_404(Tag, name=tag_name) | ||
|
||
if user not in tag.users.all(): | ||
return Response({'message': 'User does not have tag'}, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
tag.users.remove(user) | ||
|
||
return Response({'message': 'Tag removed from user'}, status=status.HTTP_204_NO_CONTENT) |
Oops, something went wrong.