Added profiles page and friends system

This commit is contained in:
ITQ
2024-03-03 09:20:17 +03:00
parent 13b4e8bafb
commit c81cd3560f
7 changed files with 132 additions and 3 deletions
+60 -1
View File
@@ -10,7 +10,12 @@ from rest_framework.response import Response
from rest_framework.views import APIView
from api.users.models import Profile
from api.users.serializers import ProfileSerializer, UpdateProfileSerializer
from api.users.permissions import CanAccessProfile
from api.users.serializers import (
ProfileSerializer,
PublicProfileSerializer,
UpdateProfileSerializer,
)
class RegisterUserApiView(APIView):
@@ -116,3 +121,57 @@ class ProfileMeApiView(APIView):
profile["image"] = user.image
return profile
class ProfileAPIView(APIView):
permission_classes = [IsAuthenticated, CanAccessProfile]
def get(self, request, login):
try:
profile = Profile.objects.get(login=login)
self.check_object_permissions(request, profile)
serializer = PublicProfileSerializer(profile)
return Response(serializer.data)
except Profile.DoesNotExist:
return Response(
{"detail": "Profile not found."},
status=status.HTTP_403_FORBIDDEN,
)
class AddFriendAPIView(APIView):
permission_classes = [IsAuthenticated]
def post(self, request):
try:
login = request.data.get("login")
profile = Profile.objects.get(login=login)
request.user.add_friend(profile)
return Response(
{"status": "ok"},
status=status.HTTP_200_OK,
)
except Profile.DoesNotExist:
return Response(
{"detail": "Profile not found."},
status=status.HTTP_404_NOT_FOUND,
)
class RemoveFriendAPIView(APIView):
permission_classes = [IsAuthenticated]
def post(self, request):
try:
login = request.data.get("login")
profile = Profile.objects.get(login=login)
request.user.remove_friend(profile)
return Response(
{"status": "ok"},
status=status.HTTP_200_OK,
)
except Profile.DoesNotExist:
return Response(
{"detail": "Profile not found."},
status=status.HTTP_404_NOT_FOUND,
)