From ba33291972b4610706c93632059bdd7565f47c73 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 6 Nov 2022 15:35:32 +0100 Subject: [PATCH] Allow responding to invites --- .../characters_list/character_card.html | 35 +++++++++++-------- src/party/models.py | 3 ++ src/party/templates/party/parties_list.html | 20 ++++++++++- src/party/urls.py | 2 ++ src/party/views.py | 26 +++++++++++++- 5 files changed, 70 insertions(+), 16 deletions(-) diff --git a/src/character/templates/character/snippets/characters_list/character_card.html b/src/character/templates/character/snippets/characters_list/character_card.html index 6ec21bd..2c78d14 100644 --- a/src/character/templates/character/snippets/characters_list/character_card.html +++ b/src/character/templates/character/snippets/characters_list/character_card.html @@ -23,21 +23,28 @@ {% endwith %}

- {% if character|managed_by:user %} - {% if character.player == user %} - - Jouer - - {% else %} - - Gérer - - {% endif %} - {% if party and character in party.characters.all %} - - Quitter le groupe - + {% if character.player == user %} + + Jouer + + {% if party %} + {% if character in party.characters.all %} + + Quitter le groupe + + {% elif character in party.invited_characters.all %} + + Rejoindre + + + Refuser l'invitation + + {% endif %} {% endif %} + {% elif character|managed_by:user %} + + Gérer + {% else %} Observer diff --git a/src/party/models.py b/src/party/models.py index ce6b898..f9811ce 100644 --- a/src/party/models.py +++ b/src/party/models.py @@ -19,6 +19,9 @@ class PartyQuerySet(models.QuerySet): | Q(characters__in=Character.objects.filter(player=user)) ).distinct() + def invited_to(self, user): + return self.filter(invited_characters__in=Character.objects.filter(player=user)) + class PartyManager(UniquelyNamedModelManager): pass diff --git a/src/party/templates/party/parties_list.html b/src/party/templates/party/parties_list.html index e542be9..3ffc4c6 100644 --- a/src/party/templates/party/parties_list.html +++ b/src/party/templates/party/parties_list.html @@ -39,7 +39,25 @@

Groupes dont vous êtes membre

+

Groupes auxquels vous avez été invités

+
+ {% for party in invited_to %}
diff --git a/src/party/urls.py b/src/party/urls.py index 0247677..cc74dcc 100644 --- a/src/party/urls.py +++ b/src/party/urls.py @@ -10,4 +10,6 @@ urlpatterns = [ path("/change/", views.party_change, name="change"), path("/delete/", views.party_delete, name="delete"), path("/leave//", views.party_leave, name="leave"), + path("/join//", views.party_join, name="join"), + path("/refuse//", views.party_refuse, name="refuse"), ] diff --git a/src/party/views.py b/src/party/views.py index f0854ec..8fe6b1d 100644 --- a/src/party/views.py +++ b/src/party/views.py @@ -12,6 +12,7 @@ def parties_list(request): context = { "managed_parties": Party.objects.managed_by(request.user), "played_parties": Party.objects.played_by(request.user).distinct(), + "invited_to": Party.objects.invited_to(request.user).distinct(), } return render(request, "party/parties_list.html", context) @@ -72,7 +73,7 @@ def party_change(request, pk): @login_required def party_leave(request, pk, character_pk): - party = get_object_or_404(Party.objects.managed_by(request.user), pk=pk) + party = get_object_or_404(Party.objects.played_by(request.user).distinct(), pk=pk) character = get_object_or_404( Character.objects.owned_by(request.user), pk=character_pk ) @@ -82,3 +83,26 @@ def party_leave(request, pk, character_pk): messages.success(request, f"{character} a quitté le groupe {party}.") return redirect("party:list") return render(request, "party/party_leave.html", context) + + +@login_required +def party_join(request, pk, character_pk): + party = get_object_or_404(Party.objects.invited_to(request.user).distinct(), pk=pk) + character = get_object_or_404( + Character.objects.owned_by(request.user), pk=character_pk + ) + party.characters.add(character) + party.invited_characters.remove(character) + messages.success(request, f"{character} a rejoint le groupe {party}.") + return redirect("party:list") + + +@login_required +def party_refuse(request, pk, character_pk): + party = get_object_or_404(Party.objects.invited_to(request.user).distinct(), pk=pk) + character = get_object_or_404( + Character.objects.owned_by(request.user), pk=character_pk + ) + party.invited_characters.remove(character) + messages.success(request, f"{character} a refusé l'invitation au groupe {party}.") + return redirect("party:list")