Allow deleting character.

Closes #12
This commit is contained in:
Gabriel Augendre 2022-11-15 18:06:55 +01:00
parent 5456b02fd6
commit 45101d0127
5 changed files with 58 additions and 1 deletions

View file

@ -0,0 +1,18 @@
{% extends "common/base.html" %}
{% load character_extras %}
{% block title %}Suppression personnage {{ character.name }}{% endblock %}
{% block content %}
<h1>Suppression personnage {{ character.name }}</h1>
<form action="{% url "character:delete" pk=character.pk %}" method=post>
{% csrf_token %}
<p>
Êtes-vous certain de vouloir supprimer le personnage {{ character.name }} ?<br>
Cette action est irréversible.
</p>
<button class="btn btn-danger" type="submit">
<i class="fa-solid fa-user-minus"></i> Supprimer le personnage
</button>
</form>
{% endblock %}

View file

@ -1,6 +1,6 @@
{% load character_extras %} {% load character_extras %}
<div class="col"> <div class="col">
<div class="card character"> <div class="card character" data-id="{{ character.pk }}">
<div class="card-body"> <div class="card-body">
<h5 class="card-title"> <h5 class="card-title">
{% if character.private %} {% if character.private %}
@ -54,6 +54,10 @@
<i class="fa-solid fa-person-walking-arrow-right"></i> Refuser l'invitation <i class="fa-solid fa-person-walking-arrow-right"></i> Refuser l'invitation
</a> </a>
{% endif %} {% endif %}
{% else %}
<a href="{% url "character:delete" pk=character.pk %}" class="btn btn-danger delete">
<i class="fa-solid fa-user-minus"></i> Supprimer
</a>
{% endif %} {% endif %}
{% elif character|managed_by:user %} {% elif character|managed_by:user %}
<a href="{% url "character:view" pk=character.pk %}{% if party %}?party={{ party.pk }}{% endif %}" class="btn btn-primary"> <a href="{% url "character:view" pk=character.pk %}{% if party %}?party={{ party.pk }}{% endif %}" class="btn btn-primary">

View file

@ -125,6 +125,28 @@ def test_list_characters(selenium: WebDriver, live_server: LiveServer):
assert names == expected_names assert names == expected_names
@pytest.mark.django_db
def test_delete_character(selenium: WebDriver, live_server: LiveServer):
call_command("loaddata", "initial_data")
username, password = "user", "some_password"
player = User.objects.create_user(username, password=password)
characters = baker.make(Character, _quantity=2, player=player)
selenium.get(live_server.url)
login(selenium, username, password)
assert Character.objects.count() == 2
selenium.find_element(
By.CSS_SELECTOR, f".character.card[data-id='{characters[0].pk}'] .delete"
).click()
selenium.find_element(By.CSS_SELECTOR, "[type=submit]").click()
assert selenium.current_url == live_server.url + reverse("character:list")
assert Character.objects.count() == 1
assert Character.objects.filter(pk=characters[0].pk).first() is None
def login(selenium, username, password): def login(selenium, username, password):
selenium.find_element(By.ID, "login").click() selenium.find_element(By.ID, "login").click()
selenium.find_element(By.ID, "id_username").send_keys(username) selenium.find_element(By.ID, "id_username").send_keys(username)

View file

@ -8,6 +8,7 @@ urlpatterns = [
path("create/", views.character_create, name="create"), path("create/", views.character_create, name="create"),
path("<int:pk>/", views.character_view, name="view"), path("<int:pk>/", views.character_view, name="view"),
path("<int:pk>/change/", views.character_change, name="change"), path("<int:pk>/change/", views.character_change, name="change"),
path("<int:pk>/delete/", views.character_delete, name="delete"),
path( path(
"<int:pk>/health_change/", views.character_health_change, name="health_change" "<int:pk>/health_change/", views.character_health_change, name="health_change"
), ),

View file

@ -57,6 +57,18 @@ def character_change(request, pk: int):
return render(request, "character/character_form.html", context) return render(request, "character/character_form.html", context)
@login_required
def character_delete(request, pk: int):
character = get_object_or_404(Character.objects.owned_by(request.user), pk=pk)
context = {"character": character}
if request.method == "POST":
name = character.name
character.delete()
messages.success(request, f"Le personnage {name} a été supprimé.")
return redirect("character:list")
return render(request, "character/character_delete.html", context)
@login_required @login_required
def character_view(request, pk: int): def character_view(request, pk: int):
character = get_object_or_404( character = get_object_or_404(