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

Expanded actions for friends to allow for invitations to games, invitations to tournaments #199

Open
wants to merge 25 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cd62f2f
Update urls.py for remove, tournament, and game paths
ishiii-s Nov 13, 2023
c75d6aa
Wrote invitation classes for match and tournament
ishiii-s Nov 13, 2023
aa4bf56
Wrote functions to remove friend, invite to game, and invite to tourn…
ishiii-s Nov 13, 2023
1b4bf2d
Update Tournament and Match definitions in views.py
ishiii-s Nov 30, 2023
68266dc
Update models.py to deal with circular dependencies
ishiii-s Nov 30, 2023
c484edb
Merge branch 'dev' into users/friends-actions
ishiii-s Nov 30, 2023
b485e33
Update models.py to try to fix circular imports
ishiii-s Nov 30, 2023
a45bf4b
Attempt to fix error for invalid model reference
ishiii-s Nov 30, 2023
418205c
Remove remove friend functionality
24raniwalar Dec 3, 2023
bb34f6e
Forgot to remove url
24raniwalar Dec 3, 2023
f7bc774
Needed to migrate
24raniwalar Dec 3, 2023
26278aa
Merge branch 'dev' into users/friends-actions
ishiii-s Dec 3, 2023
a4b7378
Fix resulting errors from resolve merge conflicts urls.py
ishiii-s Dec 3, 2023
56fe98c
Merge branch 'dev' into users/friends-actions
AndrewM131 Dec 4, 2023
6fa6bfa
fixed merge conflicts
Dec 5, 2023
6b34dec
fixed linting issues
Dec 5, 2023
5cedea3
fixed pytest issues
Dec 5, 2023
2469b37
fixed merge
Dec 6, 2023
a330f31
fixed lint
Dec 6, 2023
60c3102
added try/except and pk to url
Dec 6, 2023
b6dc528
Merge branch 'dev' into users/friends-actions
Dec 6, 2023
5e0476e
fixed pk
Dec 6, 2023
55ead9c
working tournament invite now
Dec 6, 2023
a1e9f5e
Merge branch 'dev' into users/friends-actions
Dec 6, 2023
5849ec5
fixed accidentally modified url path
Dec 6, 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
21 changes: 20 additions & 1 deletion src/chigame/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.utils.translation import gettext_lazy as _

from chigame.users.managers import UserManager

from chigame.games.models import Match, Tournament

class User(AbstractUser):
"""
Expand Down Expand Up @@ -91,7 +91,26 @@ class GroupInvitation(models.Model):
accepted = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)

class GameInvitation(models.Model):
"""
An invitation to join a match of a game
"""
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='game_invitations_sent')
receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name='game_invitations_received')
match = models.ForeignKey(Match, on_delete=models.CASCADE)
accepted = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)

class TournamentInvitation(models.Model):
"""
An invitation to join a tournament
"""
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tournament_invitations_sent')
receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tournament_invitations_received')
tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE)
accepted = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)

class NotificationQuerySet(models.QuerySet):
def filter_by_actor(self, actor, **kwargs):
try:
Expand Down
6 changes: 6 additions & 0 deletions src/chigame/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from chigame.users.views import (
cancel_friend_invitation,
send_friend_invitation,
remove_friend,
invite_to_game,
invite_to_tournament,
user_detail_view,
user_profile_detail_view,
user_redirect_view,
Expand All @@ -19,5 +22,8 @@
path("profile/<int:pk>/", view=user_profile_detail_view, name="user-profile"),
path("add_friend/<int:pk>", view=send_friend_invitation, name="add-friend"),
path("cancel_friend_invitation/<int:pk>", view=cancel_friend_invitation, name="cancel-friend-invitation"),
path("remove_friend/<int:pk>", view=remove_friend, name="remove-friend"),
path("invite_to_game/<int:pk>", view=invite_to_game, name="invite-to-game"),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these urls are correct because they don't have the pk for the match which is needed for the view

path("invite_to_tournament/<int:pk>", view=invite_to_tournament, name="invite-to-tournament"),
path("user-list/", views.user_list, name="user_list"),
]
30 changes: 29 additions & 1 deletion src/chigame/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, RedirectView, UpdateView

from .models import FriendInvitation, Notification, UserProfile
from .models import FriendInvitation, Notification, UserProfile, GameInvitation, TournamentInvitation

User = get_user_model()

Expand Down Expand Up @@ -120,3 +120,31 @@ def cancel_friend_invitation(request, pk):
else:
messages.error(request, "Something went wrong please try again later!")
return redirect(reverse("users:user-profile", kwargs={"pk": request.user.pk}))


@login_required
def remove_friend(request, pk):
sender = User.objects.get(pk=request.user.id)
receiver = User.objects.get(pk=pk)
sender_profile = sender.profile
sender_profile.friends.remove(receiver)
messages.success(request, "Friend removed successfully.")
return redirect(reverse("users:user-profile", kwargs={"pk": request.user.pk}))

@login_required
def invite_to_game(request, pk, match_id):
sender = User.objects.get(pk=request.user.id)
receiver = User.objects.get(pk=pk)
Copy link
Contributor

Choose a reason for hiding this comment

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

Everything should be in a try statement because there is no error handling for if the receiver or match don't exist. Same goes for tournament

match = User.objects.get(pk=match_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

when defining match, I think you defined it incorrectly, and it should be Match.objects instead of User.objects, as the User model doesn't have a match_id

GameInvitation.objects.create(sender=sender, receiver=receiver, group=group, match=match)
Copy link
Contributor

Choose a reason for hiding this comment

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

When did you define group in the function? Currently it isn't defined, and may be why you aren't passing the checks.

messages.success(request, "Invitation to game sent successfully.")
return redirect(reverse("users:user-profile", kwargs={"pk": request.user.pk}))

@login_required
def invite_to_tournament(request, pk, tournament_id):
sender = User.objects.get(pk=request.user.id)
receiver = User.objects.get(pk=pk)
tournament = User.objects.get(pk=tournament_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here where it should be Tournament.obkects instead of User.objects , as the User model doesn't have a tournament_id

TournamentInvitation.objects.create(sender=sender, receiver=receiver, tournament=tournament)
messages.success(request, "Invitation to tournament sent successfully.")
return redirect(reverse("users:user-profile", kwargs={"pk": request.user.pk}))
Loading