From 4ee558b12d306f322bfe8e06befe8be895f6e298 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 31 Oct 2022 00:06:20 +0100 Subject: [PATCH] Allow updating money with equipment --- src/character/forms.py | 9 ++++++ .../templates/character/equipment_update.html | 28 +++++++++++++++---- src/character/views.py | 14 +++++++++- 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/character/forms.py diff --git a/src/character/forms.py b/src/character/forms.py new file mode 100644 index 0000000..f96cbe1 --- /dev/null +++ b/src/character/forms.py @@ -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"] diff --git a/src/character/templates/character/equipment_update.html b/src/character/templates/character/equipment_update.html index 6b56966..dfbc2f0 100644 --- a/src/character/templates/character/equipment_update.html +++ b/src/character/templates/character/equipment_update.html @@ -1,5 +1,5 @@
-
+
Équipement Save
+ + {% if errors %} +
Errors:{{ errors }}
+ {% endif %}
{% csrf_token %}
diff --git a/src/character/views.py b/src/character/views.py index a46ff12..fba7fae 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -4,6 +4,7 @@ from django.http import HttpResponse from django.shortcuts import get_object_or_404, render from django_htmx.http import trigger_client_event +from character.forms import EquipmentForm from character.models import Character @@ -112,7 +113,18 @@ def character_notes_change(request: WSGIRequest, pk: int) -> HttpResponse: @login_required 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