Skip to content

Commit

Permalink
Merge pull request #461 from bounswe/feature/BE-follow-unfollow
Browse files Browse the repository at this point in the history
Feature/be follow unfollow
  • Loading branch information
hikasap authored Dec 11, 2024
2 parents a38a01f + 542c00c commit 46a6960
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
13 changes: 13 additions & 0 deletions backend/onboarding/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.auth import get_user_model
from onboarding.models import *
from rest_framework import serializers
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
Expand Down Expand Up @@ -109,3 +110,15 @@ def update(self, instance, validated_data):
instance.following.set(following)

return super().update(instance, validated_data)

User = get_user_model()
class FollowUnfollowSerializer(serializers.Serializer):
username = serializers.CharField()

def validate_username(self, value):
# Ensure the username exists in the database
try:
user = User.objects.get(username=value)
except User.DoesNotExist:
raise serializers.ValidationError(f"User with username '{value}' does not exist.")
return value
2 changes: 2 additions & 0 deletions backend/onboarding/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
path('register/', RegisterView.as_view(), name='auth_register'),
path('email-verify/', VerifyEmail.as_view(), name='email-verify'),
path('logout/', LogoutView.as_view(), name='logout'),
path('follow/', FollowView.as_view(), name='follow'),
path('unfollow/', UnfollowView.as_view(), name='unfollow'),
path('', include(router.urls)),
]
46 changes: 45 additions & 1 deletion backend/onboarding/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from django.contrib.auth import get_user_model
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_exempt
from django.contrib.sites.shortcuts import get_current_site
from django.urls import reverse
from django.template import Template, Context
from django.shortcuts import get_object_or_404
from django.conf import settings
from django.http.response import JsonResponse
from onboarding.models import User as User
from onboarding.models import Profile
from rest_framework import permissions, status , viewsets, generics
from rest_framework.response import Response
from rest_framework_simplejwt.tokens import RefreshToken
Expand Down Expand Up @@ -199,4 +202,45 @@ def update(self, request, pk=None):
def destroy(self, request, pk=None):
currency = self.get_object()
currency.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_204_NO_CONTENT)

User = get_user_model()

class FollowView(generics.GenericAPIView):
serializer_class = FollowUnfollowSerializer
permission_classes = [IsAuthenticated]

def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
username = serializer.validated_data['username']

user_to_follow = get_object_or_404(User, username=username)
profile_to_follow = user_to_follow.profile
user_profile = request.user.profile

if user_profile in profile_to_follow.followers.all():
return Response({'detail': 'Already following this user.'}, status=status.HTTP_400_BAD_REQUEST)

profile_to_follow.followers.add(user_profile)
return Response({'detail': f'Successfully followed {username}.'}, status=status.HTTP_200_OK)


class UnfollowView(generics.GenericAPIView):
serializer_class = FollowUnfollowSerializer
permission_classes = [IsAuthenticated]

def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
username = serializer.validated_data['username']

user_to_unfollow = get_object_or_404(User, username=username)
profile_to_unfollow = user_to_unfollow.profile
user_profile = request.user.profile

if user_profile not in profile_to_unfollow.followers.all():
return Response({'detail': 'You are not following this user.'}, status=status.HTTP_400_BAD_REQUEST)

profile_to_unfollow.followers.remove(user_profile)
return Response({'detail': f'Successfully unfollowed {username}.'}, status=status.HTTP_200_OK)

0 comments on commit 46a6960

Please sign in to comment.