Add individual reset stats button #23

This commit is contained in:
Gabriel Augendre 2022-11-19 10:00:48 +01:00
parent d4bf0ed1a2
commit b1513d4e11
6 changed files with 76 additions and 2 deletions

View file

@ -394,3 +394,10 @@ class Character(models.Model):
def managed_by(self, user):
return self in Character.objects.managed_by(user)
def reset_stats(self):
self.health_remaining = self.health_max
self.mana_remaining = self.mana_max
self.luck_points_remaining = self.luck_points_max
self.recovery_points_remaining = self.recovery_points_max
self.save()

View file

@ -17,7 +17,9 @@
{% endif %}
{% if character|managed_by:user %}
<p>
<a href="{% url "character:change" pk=character.pk %}">Edit</a>
<a href="{% url "character:change" pk=character.pk %}">Modifier</a>
&bullet;
<a href="{% url "character:reset_stats" pk=character.pk %}" id="reset-stats">Réinitialiser les stats</a>
</p>
{% endif %}
<p>

View file

@ -0,0 +1,17 @@
{% extends "common/base.html" %}
{% block title %}Réinitialisation des stats de {{ character.name }}{% endblock %}
{% block content %}
<h1>Réinitialisation des stats de {{ character.name }}</h1>
<form action="{% url "character:reset_stats" pk=character.pk %}" method=post>
{% csrf_token %}
<p>
Êtes-vous certain de vouloir réinitialiser les stats de {{ character.name }} ?<br>
Cette action est irréversible.
</p>
<button class="btn btn-primary" type="submit">
<i class="fa-solid fa-suitcase-medical"></i> Réinitialiser les stats
</button>
</form>
{% endblock %}

View file

@ -6,7 +6,7 @@ from pytest_django.live_server_helper import LiveServer
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.webdriver import WebDriver
from character.models import Character
from character.models import Character, Profile
from common.models import User
@ -144,6 +144,40 @@ def test_delete_character(selenium: WebDriver, live_server: LiveServer):
assert Character.objects.filter(pk=characters[0].pk).first() is None
@pytest.mark.django_db
def test_reset_stats_view(
selenium: WebDriver, live_server: LiveServer, initial_data: None
):
username, password = "user", "some_password"
player = User.objects.create_user(username, password=password)
profile = Profile.objects.get(name__iexact="Magicien")
character = baker.make(Character, player=player, profile=profile)
character.health_max = 20
character.health_remaining = 15
character.value_intelligence = 15
character.level = 3
character.mana_remaining = character.mana_max - 1
character.recovery_points_remaining = 2
character.value_charisma = 15
character.luck_points_remaining = character.luck_points_max - 2
character.save()
login(selenium, live_server, username, password)
url = reverse("character:view", kwargs={"pk": character.pk})
selenium.get(live_server.url + url)
selenium.find_element(By.ID, "reset-stats").click()
selenium.find_element(By.CSS_SELECTOR, "[type=submit]").click()
assert selenium.current_url == live_server.url + character.get_absolute_url()
character.refresh_from_db()
assert character.health_remaining == character.health_max
assert character.mana_remaining == character.mana_max
assert character.recovery_points_remaining == character.recovery_points_max
assert character.luck_points_remaining == character.luck_points_max
def login(
selenium: WebDriver, live_server: LiveServer, username: str, password: str
) -> None:

View file

@ -85,4 +85,5 @@ urlpatterns = [
"<int:pk>/remove_state/<int:state_pk>/", views.remove_state, name="remove_state"
),
path("<int:pk>/add_state/<int:state_pk>/", views.add_state, name="add_state"),
path("<int:pk>/reset_stats/", views.reset_stats, name="reset_stats"),
]

View file

@ -409,3 +409,16 @@ def add_state(request, pk: int, state_pk: int):
request, "character/snippets/character_details/states.html", context
)
return trigger_client_event(response, "refresh_tooltips", {})
@login_required
def reset_stats(request, pk: int):
character: Character = get_object_or_404(
Character.objects.managed_by(request.user), pk=pk
)
context = {"character": character}
if request.method == "POST":
character.reset_stats()
messages.success(request, f"Les stats de {character} ont été réinitialisées.")
return redirect(character)
return render(request, "character/character_reset_stats.html", context)