From 85df1baa52976c4c5fac5f828cf8bf66ac0c867b Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 30 Oct 2022 18:36:18 +0100 Subject: [PATCH] Refactor text field updates --- src/character/views.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/character/views.py b/src/character/views.py index 1d54772..62d871a 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -56,23 +56,19 @@ def get_updated_value(max_value, request): @login_required def character_notes_change(request: WSGIRequest, pk: int) -> HttpResponse: - character = get_object_or_404(Character.objects.only("notes"), pk=pk) - context = {"character": character} - if request.method == "GET": - return render(request, "character/notes_update.html", context) - - character.notes = request.POST.get("notes") - character.save() - return render(request, "character/notes_display.html", context) + return update_text_field(request, pk, "notes") @login_required def character_equipment_change(request: WSGIRequest, pk: int) -> HttpResponse: - character = get_object_or_404(Character.objects.only("equipment"), pk=pk) + return update_text_field(request, pk, "equipment") + + +def update_text_field(request, pk, field): + character = get_object_or_404(Character.objects.only(field), pk=pk) context = {"character": character} if request.method == "GET": - return render(request, "character/equipment_update.html", context) - - character.equipment = request.POST.get("equipment") + return render(request, f"character/{field}_update.html", context) + setattr(character, field, request.POST.get(field)) character.save() - return render(request, "character/equipment_display.html", context) + return render(request, f"character/{field}_display.html", context)