Allow leaving groups

This commit is contained in:
Gabriel Augendre 2022-11-06 14:13:30 +01:00
parent eb7e2c6db9
commit 256fcb149f
4 changed files with 39 additions and 2 deletions

View file

@ -6,13 +6,17 @@
{% block content %}
<h1>{{ party.name }}</h1>
<p>MJ : {{ party.game_master.get_full_name|default:party.game_master.username }}</p>
<p>Personnages :</p>
<ul>
{% for character in party.characters.all %}
<li>
<a href="{% url "character:view" pk=character.pk %}">
{{ character.name }}
</a>
{{ character.name }}</a>
{% if character.player == user %}
(<a href="{% url "party:leave" pk=party.pk character_pk=character.pk %}">
Quitter</a>)
{% endif %}
</li>
{% empty %}
<li>Aucun...</li>

View file

@ -0,0 +1,17 @@
{% extends "common/base.html" %}
{% load character_extras %}
{% block title %}Quitter le groupe {{ party.name }}{% endblock %}
{% block content %}
<h1>Quitter le groupe {{ party.name }}</h1>
<form action="{% url "party:leave" pk=party.pk character_pk=character.pk %}" method=post>
{% csrf_token %}
<p>
Êtes-vous certain de vouloir faire quitter le groupe {{ party.name }} à {{ character.name }} ?
</p>
<button class="btn btn-danger" type="submit">
<i class="fa-solid fa-person-walking-arrow-right"></i> Quitter le groupe
</button>
</form>
{% endblock %}

View file

@ -8,4 +8,5 @@ urlpatterns = [
path("create/", views.party_create, name="create"),
path("<int:pk>/", views.party_details, name="details"),
path("<int:pk>/delete/", views.party_delete, name="delete"),
path("<int:pk>/leave/<int:character_pk>/", views.party_leave, name="leave"),
]

View file

@ -2,6 +2,7 @@ from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, redirect, render
from character.models import Character
from party.forms import PartyForm
from party.models import Party
@ -48,3 +49,17 @@ def party_delete(request, pk):
messages.success(request, f"Le groupe {name} a été supprimé.")
return redirect("party:list")
return render(request, "party/party_delete.html", context)
@login_required
def party_leave(request, pk, character_pk):
party = get_object_or_404(Party.objects.managed_by(request.user), pk=pk)
character = get_object_or_404(
Character.objects.owned_by(request.user), pk=character_pk
)
context = {"party": party, "character": character}
if request.method == "POST":
character.parties.remove(party)
messages.success(request, f"{character} a quitté le groupe {party}.")
return redirect("party:list")
return render(request, "party/party_leave.html", context)