diff --git a/src/chigame/users/migrations/0004_tournamentinvitation_gameinvitation.py b/src/chigame/users/migrations/0004_tournamentinvitation_gameinvitation.py new file mode 100644 index 00000000..e6b3d5cf --- /dev/null +++ b/src/chigame/users/migrations/0004_tournamentinvitation_gameinvitation.py @@ -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, + ), + ), + ], + ), + ] diff --git a/src/chigame/users/migrations/0006_merge_20231205_1748.py b/src/chigame/users/migrations/0006_merge_20231205_1748.py new file mode 100644 index 00000000..101ce21c --- /dev/null +++ b/src/chigame/users/migrations/0006_merge_20231205_1748.py @@ -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 = [] diff --git a/src/chigame/users/migrations/0007_delete_gameinvitation.py b/src/chigame/users/migrations/0007_delete_gameinvitation.py new file mode 100644 index 00000000..74941951 --- /dev/null +++ b/src/chigame/users/migrations/0007_delete_gameinvitation.py @@ -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", + ), + ] diff --git a/src/chigame/users/models.py b/src/chigame/users/models.py index 4d484759..dcef2646 100644 --- a/src/chigame/users/models.py +++ b/src/chigame/users/models.py @@ -11,6 +11,8 @@ from chigame.users.managers import UserManager +# from chigame.games.models import Match, Tournament + def validate_username(value): """ @@ -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: diff --git a/src/chigame/users/urls.py b/src/chigame/users/urls.py index eadf9b9f..03119fa5 100644 --- a/src/chigame/users/urls.py +++ b/src/chigame/users/urls.py @@ -7,6 +7,7 @@ cancel_friend_invitation, decline_friend_invitation, friend_list_view, + invite_to_tournament, notification_detail, notification_search_results, remove_friend, @@ -31,6 +32,7 @@ path("add_friend/", view=send_friend_invitation, name="add-friend"), path("remove_friend/", view=remove_friend, name="remove-friend"), path("cancel_friend_invitation/", view=cancel_friend_invitation, name="cancel-friend-invitation"), + path("invite_to_tournament///", 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/", view=accept_friend_invitation, name="accept-friend-invitation"), diff --git a/src/chigame/users/views.py b/src/chigame/users/views.py index 569ce663..59bcdf4f 100644 --- a/src/chigame/users/views.py +++ b/src/chigame/users/views.py @@ -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() @@ -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) + 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) + 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)