-
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 #18 from HE-Arc/pictures
Pictures
- Loading branch information
Showing
33 changed files
with
934 additions
and
422 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,3 +167,4 @@ 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: 22 additions & 0 deletions
22
backend/kodecupidapp/migrations/0004_remove_tag_users_user_tags.py
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,22 @@ | ||
# Generated by Django 5.0.4 on 2024-04-03 22:20 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('kodecupidapp', '0003_like_unique_like'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='tag', | ||
name='users', | ||
), | ||
migrations.AddField( | ||
model_name='user', | ||
name='tags', | ||
field=models.ManyToManyField(related_name='tags', to='kodecupidapp.tag'), | ||
), | ||
] |
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,7 +1,9 @@ | ||
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,4 +1,5 @@ | ||
from .like import LikeView | ||
from .user import UserView, UserTagView | ||
from .user import UserView | ||
from .tag import TagView | ||
from .picture import PictureView | ||
from .picture import PictureView | ||
from .swipe import SwipeView |
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,41 +1,51 @@ | ||
from rest_framework.views import APIView | ||
from rest_framework.viewsets import GenericViewSet | ||
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin, ListModelMixin, DestroyModelMixin | ||
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 | ||
from django.http import HttpResponse | ||
|
||
class PictureView(APIView): | ||
import os | ||
|
||
|
||
class PictureView(GenericViewSet, CreateModelMixin, RetrieveModelMixin, DestroyModelMixin): | ||
queryset = Picture.objects.all() | ||
serializer_class = PictureSerializer | ||
permission_classes = [IsAuthenticated] | ||
|
||
def post(self, request): | ||
serializer = PictureSerializer(data=request.data) | ||
def create(self, request): | ||
modified_data = request.data.copy() | ||
modified_data['user'] = request.user.id | ||
|
||
serializer = self.get_serializer(data=modified_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) | ||
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) | ||
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) | ||
|
||
|
||
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) |
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,58 @@ | ||
from rest_framework import status | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
from rest_framework.decorators import action | ||
|
||
from drf_spectacular.utils import extend_schema, OpenApiParameter | ||
|
||
from rest_framework.viewsets import GenericViewSet | ||
|
||
from ..serializers import UserSerializer | ||
|
||
from ..models import User, Like | ||
|
||
import random | ||
|
||
|
||
|
||
class SwipeView(GenericViewSet): | ||
permission_classes = [IsAuthenticated] | ||
serializer_class = UserSerializer | ||
|
||
@action(detail=False, methods=['get'],url_path='<int:pk>') | ||
def user_by_id(self,request, pk): | ||
if User.objects.filter(id = pk).exists(): | ||
user = User.objects.get(id = pk) | ||
serializer = self.serializer_class(user, many=False) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
else: | ||
return Response({"message": "User not found."}, status=status.HTTP_404_NOT_FOUND) | ||
|
||
@action(detail=False, methods=['get']) | ||
def matches(self, request): | ||
user = request.user | ||
|
||
likes_target_user = Like.objects.filter(target_user=user).values_list('source_user', flat=True) | ||
users = User.objects.filter(id__in=likes_target_user) | ||
|
||
serializer = self.serializer_class(users, many=True) | ||
|
||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
@action(detail=False, methods=['get']) | ||
def random_user(self,request): | ||
users = User.objects.exclude(id=request.user.id) | ||
|
||
likes_source_user = Like.objects.filter(source_user=request.user).values_list('target_user', flat=True) | ||
users = users.exclude(id__in=likes_source_user) | ||
|
||
if len(users) == 0: | ||
return Response({"message": "Looks like you liked them all"}, status=status.HTTP_404_NOT_FOUND) | ||
|
||
user = random.choice(users) | ||
|
||
serializer = self.serializer_class(user, many=False) | ||
|
||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
|
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,54 +1,15 @@ | ||
from rest_framework.views import APIView | ||
from rest_framework.viewsets import GenericViewSet | ||
from rest_framework.mixins import ListModelMixin | ||
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 | ||
|
||
class TagView(APIView): | ||
|
||
def get(self, request): | ||
tags = Tag.objects.all() | ||
serializer = TagSerializer(tags, many=True) | ||
def list(self, request, *args, **kwargs): | ||
queryset = self.get_queryset() | ||
serializer = self.get_serializer(queryset, 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.