Allow updating money with equipment

This commit is contained in:
Gabriel Augendre 2022-10-31 00:06:20 +01:00
parent 1479a4fe53
commit 4ee558b12d
3 changed files with 45 additions and 6 deletions

9
src/character/forms.py Normal file
View file

@ -0,0 +1,9 @@
from django import forms
from character.models import Character
class EquipmentForm(forms.ModelForm):
class Meta:
model = Character
fields = ["equipment", "money_pp", "money_po", "money_pa", "money_pc"]

View file

@ -1,5 +1,5 @@
<form id="equipment"> <form id="equipment">
<div class="card"> <div class="card {% if errors %}border-danger{% endif %}">
<h5 class="card-header"> <h5 class="card-header">
Équipement Équipement
<a hx-post="{% url "character:equipment_change" pk=character.pk %}" <a hx-post="{% url "character:equipment_change" pk=character.pk %}"
@ -10,15 +10,33 @@
<i class="fa-solid fa-save"></i> Save <i class="fa-solid fa-save"></i> Save
</a> </a>
</h5> </h5>
{% if errors %}
<div class="card-body text-bg-danger">Errors:{{ errors }}</div>
{% endif %}
<div class="card-body"> <div class="card-body">
{% csrf_token %} {% csrf_token %}
<textarea class="form-control" name="equipment" rows="10">{{ character.equipment }}</textarea> <textarea class="form-control" name="equipment" rows="10">{{ character.equipment }}</textarea>
</div> </div>
<div class="card-footer"> <div class="card-footer">
{{ character.money_pp }} pp, <div class="row g-1">
{{ character.money_po }} po, <div class="col input-group input-group-sm">
{{ character.money_pa }} pa, <input type="number" min="0" class="form-control" name="money_pp" value="{{ character.money_pp }}">
{{ character.money_pc }} pc <span class="input-group-text">pp</span>
</div>
<div class="col input-group input-group-sm">
<input type="number" min="0" class="form-control" name="money_po" value="{{ character.money_po }}">
<span class="input-group-text">po</span>
</div>
<div class="col input-group input-group-sm">
<input type="number" min="0" class="form-control" name="money_pa" value="{{ character.money_pa }}">
<span class="input-group-text">pa</span>
</div>
<div class="col input-group input-group-sm">
<input type="number" min="0" class="form-control" name="money_pc" value="{{ character.money_pc }}">
<span class="input-group-text">pc</span>
</div>
</div>
</div> </div>
</div> </div>
</form> </form>

View file

@ -4,6 +4,7 @@ from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django_htmx.http import trigger_client_event from django_htmx.http import trigger_client_event
from character.forms import EquipmentForm
from character.models import Character from character.models import Character
@ -112,7 +113,18 @@ def character_notes_change(request: WSGIRequest, pk: int) -> HttpResponse:
@login_required @login_required
def character_equipment_change(request: WSGIRequest, pk: int) -> HttpResponse: def character_equipment_change(request: WSGIRequest, pk: int) -> HttpResponse:
return update_text_field(request, pk, "equipment") field = "equipment"
character = get_object_or_404(Character.objects.only(field), pk=pk)
context = {"character": character}
if request.method == "GET":
return render(request, f"character/{field}_update.html", context)
form = EquipmentForm(request.POST, instance=character)
if form.is_valid():
form.save()
return render(request, f"character/{field}_display.html", context)
else:
context["errors"] = form.errors
return render(request, f"character/{field}_update.html", context)
@login_required @login_required