Skip to content

Commit

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

urlpatterns = [
Expand All @@ -18,4 +19,5 @@
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"),
]
31 changes: 31 additions & 0 deletions app/back-end/dashboards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,34 @@ def post(self, request):
return Response({'success': 'Friendship Added'}, status=status.HTTP_200_OK)
except Friendship.DoesNotExist:
return Response({'error': 'Friendship does not exist'}, status=status.HTTP_400_BAD_REQUEST)

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

def block_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 getattr(friendship, block_flag):
return Response({'error': 'Friend already blocked'}, status=status.HTTP_400_BAD_REQUEST)
else:
setattr(friendship, block_flag, True)
friendship.save()
return Response({'success': 'Friend blocked'}, 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.block_friend(friendship, user_from)
except Friendship.DoesNotExist:
return Response({'error': 'Friendship does not exist'}, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit b8ea152

Please sign in to comment.