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 all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Generated by Django 4.2.4 on 2023-12-03 02:40

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
("games", "0016_update_tournaments_2"),
("users", "0003_merge_20231115_1525"),
]

operations = [
migrations.CreateModel(
name="TournamentInvitation",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("accepted", models.BooleanField(default=False)),
("timestamp", models.DateTimeField(auto_now_add=True)),
(
"receiver",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="tournament_invitations_received",
to=settings.AUTH_USER_MODEL,
),
),
(
"sender",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="tournament_invitations_sent",
to=settings.AUTH_USER_MODEL,
),
),
("tournament", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="games.tournament")),
],
),
migrations.CreateModel(
name="GameInvitation",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("accepted", models.BooleanField(default=False)),
("timestamp", models.DateTimeField(auto_now_add=True)),
("match", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="games.match")),
(
"receiver",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="game_invitations_received",
to=settings.AUTH_USER_MODEL,
),
),
(
"sender",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="game_invitations_sent",
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
12 changes: 12 additions & 0 deletions src/chigame/users/migrations/0006_merge_20231205_1748.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 4.2.4 on 2023-12-05 23:48

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("users", "0004_tournamentinvitation_gameinvitation"),
("users", "0005_alter_user_username"),
]

operations = []
15 changes: 15 additions & 0 deletions src/chigame/users/migrations/0007_delete_gameinvitation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Django 4.2.4 on 2023-12-06 21:54

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("users", "0006_merge_20231205_1748"),
]

operations = [
migrations.DeleteModel(
name="GameInvitation",
),
]
16 changes: 16 additions & 0 deletions src/chigame/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from chigame.users.managers import UserManager

# from chigame.games.models import Match, Tournament


def validate_username(value):
"""
Expand Down Expand Up @@ -130,6 +132,20 @@ class GroupInvitation(models.Model):
timestamp = models.DateTimeField(auto_now_add=True)


class TournamentInvitation(models.Model):
"""
An invitation to join a tournament
"""

sender = models.ForeignKey("users.User", on_delete=models.CASCADE, related_name="tournament_invitations_sent")
receiver = models.ForeignKey(
"users.User", on_delete=models.CASCADE, related_name="tournament_invitations_received"
)
tournament = models.ForeignKey("games.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, include_deleted=False, **kwargs):
try:
Expand Down
2 changes: 2 additions & 0 deletions src/chigame/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
cancel_friend_invitation,
decline_friend_invitation,
friend_list_view,
invite_to_tournament,
notification_detail,
notification_search_results,
remove_friend,
Expand All @@ -31,6 +32,7 @@
path("add_friend/<int:pk>", view=send_friend_invitation, name="add-friend"),
path("remove_friend/<int:pk>", view=remove_friend, name="remove-friend"),
path("cancel_friend_invitation/<int:pk>", view=cancel_friend_invitation, name="cancel-friend-invitation"),
path("invite_to_tournament/<int:pk>/<int:tournament_pk>/", view=invite_to_tournament, name="invite-to-tournament"),
path("user-detail/", views.user_detail_view, name="user-detail"),
path("user-list/", view=user_list, name="user-list"),
path("accept_friend_invitation/<int:pk>", view=accept_friend_invitation, name="accept-friend-invitation"),
Expand Down
14 changes: 13 additions & 1 deletion src/chigame/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from chigame.games.models import Lobby, Player, Tournament

from .models import FriendInvitation, Notification, UserProfile
from .models import FriendInvitation, Notification, TournamentInvitation, UserProfile
from .tables import FriendsTable, UserTable

User = get_user_model()
Expand Down Expand Up @@ -185,6 +185,18 @@ def cancel_friend_invitation(request, pk):


@login_required
def invite_to_tournament(request, pk, tournament_pk):
try:
sender = get_object_or_404(User, pk=request.user.id)
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to do one more check where sender and receiver are not the same. You shouldn't be able to send an invite to yourself.

receiver = get_object_or_404(User, pk=pk)
tournament = get_object_or_404(Tournament, pk=tournament_pk)
TournamentInvitation.objects.create(sender=sender, receiver=receiver, tournament=tournament)
Copy link
Contributor

Choose a reason for hiding this comment

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

Along with the check on user id, the invitation object should only be created if that check succeeds.

messages.success(request, "Invitation to tournament sent successfully.")
except Exception as e:
messages.error(request, f"Error: {str(e)}")
return redirect(reverse("users:user-profile", kwargs={"pk": request.user.pk}))


def accept_friend_invitation(request, pk):
try:
friendship = FriendInvitation.objects.get(pk=pk)
Expand Down
Loading