Skip to content

Commit

Permalink
[Feat] add unblock friend
Browse files Browse the repository at this point in the history
  • Loading branch information
zakarm committed May 5, 2024
1 parent b8ea152 commit 74bff1a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 4 additions & 2 deletions app/back-end/dashboards/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
RemoveFriendshipView,
AcceptFriendshipView,
AddFriendshipView,
BlockFriendshipView
BlockFriendshipView,
UnblockFriendshipView
)

urlpatterns = [
Expand All @@ -19,5 +20,6 @@
path('friends-remove', RemoveFriendshipView.as_view(), name="friends-remove"),
path('friends-accept', AcceptFriendshipView.as_view(), name="friends-accept"),
path('friends-add', AddFriendshipView.as_view(), name="friends-add"),
path('friends-block', BlockFriendshipView.as_view(), name="friends-add"),
path('friends-block', BlockFriendshipView.as_view(), name="friends-block"),
path('friends-unblock', UnblockFriendshipView.as_view(), name="friends-unblock"),
]
34 changes: 33 additions & 1 deletion app/back-end/dashboards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def block_friend(self, friendship, user_from):
block_flag = 'u_one_is_blocked_u_two'
else:
block_flag = 'u_two_is_blocked_u_one'

if getattr(friendship, block_flag):
return Response({'error': 'Friend already blocked'}, status=status.HTTP_400_BAD_REQUEST)
else:
Expand All @@ -164,5 +164,37 @@ def post(self, request):
friendship = Friendship.objects.get(Q(user_from=user_from, user_to=user_accept)|
Q(user_from=user_accept, user_to=user_from))
return self.block_friend(friendship, user_from)
except Friendship.DoesNotExist:
return Response({'error': 'Friendship does not exist'}, status=status.HTTP_400_BAD_REQUEST)


class UnblockFriendshipView(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

def unblock_friend(self, friendship, user_from):
if friendship.user_from == user_from:
block_flag = 'u_one_is_blocked_u_two'
else:
block_flag = 'u_two_is_blocked_u_one'

if not getattr(friendship, block_flag):
return Response({'error': 'Friend is not blocked'}, status=status.HTTP_400_BAD_REQUEST)
else:
setattr(friendship, block_flag, False)
friendship.save()
return Response({'success': 'Friend unblocked'}, status=status.HTTP_200_OK)

def post(self, request):
user_from = request.user
username = request.data.get('username')
try:
user_accept = User.objects.get(username=username)
except User.DoesNotExist:
return Response({'error': 'User does not exist'}, status=status.HTTP_400_BAD_REQUEST)
try:
friendship = Friendship.objects.get(Q(user_from=user_from, user_to=user_accept)|
Q(user_from=user_accept, user_to=user_from))
return self.unblock_friend(friendship, user_from)
except Friendship.DoesNotExist:
return Response({'error': 'Friendship does not exist'}, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit 74bff1a

Please sign in to comment.