Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST API Endpoints for Sending and Accepting Friend invitations between two Users; GET request endpoint for listview for Friend Invites #370

Open
wants to merge 37 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
90d71a7
Create serializers.py
Esterello2 Oct 30, 2023
d9792c7
Update urls.py
Esterello2 Oct 30, 2023
fd712df
Update views.py
Esterello2 Oct 30, 2023
734d40d
Update views.py
Esterello2 Nov 6, 2023
e48159f
Update
Esterello2 Nov 13, 2023
f3bc490
Deleted some files
Esterello2 Nov 13, 2023
7ec1924
Merge branch 'dev' of https://github.com/uchicago-cs/chigame into api…
Esterello2 Nov 27, 2023
6525622
Fixed CI error
Esterello2 Dec 1, 2023
ed8943a
Merge branch 'dev' into apis/post-users
Esterello2 Dec 1, 2023
d4d41c1
Fixing CI issues
Esterello2 Dec 1, 2023
23ae6ea
fixing CI issue in urls.py
Esterello2 Dec 1, 2023
b04b0fa
Fixing CI issues
Esterello2 Dec 1, 2023
6ee326c
Fixing CI
Esterello2 Dec 1, 2023
8be6de9
Fix CI
Esterello2 Dec 1, 2023
d05711d
CI ISsue
Esterello2 Dec 1, 2023
2a5fa11
Fixing CI
Esterello2 Dec 1, 2023
2759d2a
sd
Esterello2 Dec 1, 2023
554e5e8
djsdf
Esterello2 Dec 1, 2023
f17eda4
update:
Esterello2 Dec 1, 2023
8a2f865
Merge branch 'dev' into apis/post-users
Esterello2 Dec 1, 2023
4deeba5
update
Esterello2 Dec 1, 2023
86aad2a
Fixing CI
Esterello2 Dec 2, 2023
e2bb59c
Merge branch 'dev' into apis/create-friend-requests
abrahmasandra Dec 2, 2023
e355d91
Adjustments
Esterello2 Dec 3, 2023
f2e7ef4
update
Esterello2 Dec 5, 2023
a845ea4
Merge branch 'dev' into apis/create-friend-requests
Esterello2 Dec 5, 2023
8e8dbeb
Merge branch 'apis/create-friend-requests' of https://github.com/uchi…
Esterello2 Dec 5, 2023
888ac68
Merge branch 'dev' into apis/create-friend-requests
majorsylvie Dec 5, 2023
a472162
update
Esterello2 Dec 5, 2023
e46b2d5
Merge branch 'dev' into apis/create-friend-requests
Esterello2 Dec 6, 2023
a50ea82
lint
Esterello2 Dec 5, 2023
615d142
Merge branch 'dev' into apis/create-friend-requests
Esterello2 Dec 6, 2023
28cad16
update
Esterello2 Dec 5, 2023
23e62f8
deleting extra code
Esterello2 Dec 7, 2023
c804cfa
updatre
Esterello2 Dec 7, 2023
9506857
update
Esterello2 Dec 7, 2023
b4ca844
Merge branch 'dev' into apis/create-friend-requests
majorsylvie Dec 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/chigame/api/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rest_framework.permissions import BasePermission


class IsUnauthenticated(BasePermission):
def has_permission(self, request, view):
return not request.user or not request.user.is_authenticated
16 changes: 14 additions & 2 deletions src/chigame/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework import serializers

from chigame.games.models import Category, Chat, Game, Lobby, Mechanic, Message, Tournament, User
from chigame.users.models import Group
from chigame.users.models import FriendInvitation, Group, UserProfile


class GameSerializer(serializers.ModelSerializer):
Expand All @@ -10,6 +10,12 @@ class Meta:
fields = "__all__"


class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = "__all__"


class LobbySerializer(serializers.ModelSerializer):
class Meta:
model = Lobby
Expand Down Expand Up @@ -59,7 +65,6 @@ class Meta:

class MessageSerializer(serializers.ModelSerializer):
sender = serializers.EmailField(write_only=True)

tournament = serializers.IntegerField(write_only=True)

class Meta:
Expand All @@ -81,9 +86,16 @@ def create(self, validated_data):
return message


class FriendInvitationSerializer(serializers.ModelSerializer):
class Meta:
model = FriendInvitation
fields = "__all__"


class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group

fields = "__all__"


Expand Down
18 changes: 18 additions & 0 deletions src/chigame/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@
]

urlpatterns = [
path(
"friend-invitations/send/<int:sender_pk>/<int:receiver_pk>/",
views.SendFriendInvitationView.as_view(),
name="send-friend-invitation",
),
path(
"friend-invitations/accept/<int:invitation_pk>/",
views.AcceptFriendInvitationView.as_view(),
name="accept-friend-invitation",
),
path("friend-invitations/", views.FriendInvitationList.as_view(), name="friend-invitation-list"),
path("user-profiles/create/<int:user_pk>/", views.UserProfileCreateView.as_view(), name="create-user-profile"),
path("user-profiles/", views.UserProfileListView.as_view(), name="user-profile-list"),
path(
"user-profiles/update/<int:user_profile_pk>/",
views.UserProfileUpdateView.as_view(),
name="update-user-profile",
),
path("games/", include(game_patterns)),
path("lobbies/", include(lobby_patterns)),
path("users/", include(user_patterns)),
Expand Down
74 changes: 71 additions & 3 deletions src/chigame/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
from chigame.api.filters import GameFilter
from chigame.api.serializers import (
CategorySerializer,
FriendInvitationSerializer,
GameSerializer,
GroupSerializer,
LobbySerializer,
MechanicSerializer,
MessageFeedSerializer,
MessageSerializer,
UserProfileSerializer,
UserSerializer,
)
from chigame.games.models import Game, Lobby, Message, User
from chigame.users.models import Group, UserProfile
from chigame.games.models import Game, Lobby, Message
from chigame.users.models import FriendInvitation, Group, User, UserProfile


# Helper function to get user from slug
def get_user(lookup_value):
# If the lookup_value is an integer, use the id field
if lookup_value.isdigit():
Expand Down Expand Up @@ -106,6 +107,73 @@ class MessageView(generics.CreateAPIView):
serializer_class = MessageSerializer


class SendFriendInvitationView(APIView):
def post(self, request, *args, **kwargs):
sender_pk = self.kwargs["sender_pk"]
receiver_pk = self.kwargs["receiver_pk"]
sender = get_object_or_404(User, pk=sender_pk)
receiver = get_object_or_404(User, pk=receiver_pk)
if sender == receiver:
return Response(
{"detail": "You cannot send an invitation to yourself."}, status=status.HTTP_400_BAD_REQUEST
)
invitation = FriendInvitation(sender=sender, receiver=receiver)
invitation.save()
serializer = FriendInvitationSerializer(invitation)
return Response(serializer.data, status=status.HTTP_201_CREATED)


class AcceptFriendInvitationView(APIView):
def post(self, request, *args, **kwargs):
invitation_pk = self.kwargs["invitation_pk"]
invitation = get_object_or_404(FriendInvitation, pk=invitation_pk)
if invitation.accepted:
return Response(
{"detail": "This invitation has already been accepted."}, status=status.HTTP_400_BAD_REQUEST
)
invitation.accept_invitation() # Error with this method in the users.models
serializer = FriendInvitationSerializer(invitation)
return Response(serializer.data, status=status.HTTP_200_OK)


class FriendInvitationList(generics.ListAPIView):
queryset = FriendInvitation.objects.all()
serializer_class = FriendInvitationSerializer


class UserProfileCreateView(APIView):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this class have to do with friend invitations?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IT is because Friend Invitations use User Profile pks rather than User class, so I had to create a way to make these UserProfile objects

def post(self, request, *args, **kwargs):
user_pk = self.kwargs["user_pk"]
user = get_object_or_404(User, pk=user_pk)
existing_profile = UserProfile.objects.filter(user=user).first()
if existing_profile:
serializer = UserProfileSerializer(existing_profile)
return Response(serializer.data, status=status.HTTP_200_OK)

serializer = UserProfileSerializer(data=request.data)
if serializer.is_valid():
profile = serializer.save(user=user)
return Response(UserProfileSerializer(profile).data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class UserProfileListView(generics.ListAPIView):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer


class UserProfileUpdateView(APIView):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both this update and user profile list view are relevant for JWT and are related to authentication.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I am removing the permission classes

def patch(self, request, *args, **kwargs):
user_profile_pk = self.kwargs["user_profile_pk"]
user_profile = get_object_or_404(UserProfile, pk=user_profile_pk)

serializer = UserProfileSerializer(user_profile, data=request.data, partial=True)
if serializer.is_valid():
updated_profile = serializer.save()
return Response(UserProfileSerializer(updated_profile).data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class GroupListView(generics.ListCreateAPIView):
queryset = Group.objects.all()
serializer_class = GroupSerializer
Expand Down
1 change: 1 addition & 0 deletions src/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"django_filters",
"django_tables2",
]

THIRD_PARTY_APPS = [
"crispy_forms",
"crispy_bootstrap5",
Expand Down
Loading