From 45101d01278d40a0402169d09439a3830fcb290e Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 15 Nov 2022 18:06:55 +0100 Subject: [PATCH] Allow deleting character. Closes #12 --- .../templates/character/character_delete.html | 18 +++++++++++++++ .../characters_list/character_card.html | 6 ++++- src/character/tests/test_interactions.py | 22 +++++++++++++++++++ src/character/urls.py | 1 + src/character/views.py | 12 ++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/character/templates/character/character_delete.html diff --git a/src/character/templates/character/character_delete.html b/src/character/templates/character/character_delete.html new file mode 100644 index 0000000..5dfcd25 --- /dev/null +++ b/src/character/templates/character/character_delete.html @@ -0,0 +1,18 @@ +{% extends "common/base.html" %} +{% load character_extras %} + +{% block title %}Suppression personnage {{ character.name }}{% endblock %} + +{% block content %} +

Suppression personnage {{ character.name }}

+
+ {% csrf_token %} +

+ Êtes-vous certain de vouloir supprimer le personnage {{ character.name }} ?
+ Cette action est irréversible. +

+ +
+{% endblock %} 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 cde9127..1965269 100644 --- a/src/character/templates/character/snippets/characters_list/character_card.html +++ b/src/character/templates/character/snippets/characters_list/character_card.html @@ -1,6 +1,6 @@ {% load character_extras %}
-
+
{% if character.private %} @@ -54,6 +54,10 @@ Refuser l'invitation {% endif %} + {% else %} + + Supprimer + {% endif %} {% elif character|managed_by:user %} diff --git a/src/character/tests/test_interactions.py b/src/character/tests/test_interactions.py index ee84fb2..bb95cc7 100644 --- a/src/character/tests/test_interactions.py +++ b/src/character/tests/test_interactions.py @@ -125,6 +125,28 @@ def test_list_characters(selenium: WebDriver, live_server: LiveServer): 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): selenium.find_element(By.ID, "login").click() selenium.find_element(By.ID, "id_username").send_keys(username) diff --git a/src/character/urls.py b/src/character/urls.py index 8bb8625..f0e3112 100644 --- a/src/character/urls.py +++ b/src/character/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ path("create/", views.character_create, name="create"), path("/", views.character_view, name="view"), path("/change/", views.character_change, name="change"), + path("/delete/", views.character_delete, name="delete"), path( "/health_change/", views.character_health_change, name="health_change" ), diff --git a/src/character/views.py b/src/character/views.py index bca18bb..1320ca2 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -57,6 +57,18 @@ def character_change(request, pk: int): 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 def character_view(request, pk: int): character = get_object_or_404(