From 71faf4dbe1207de82ffaec12a80a4d7840986503 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Wed, 9 Nov 2022 18:53:42 +0100 Subject: [PATCH] Add create and update character views --- src/character/forms.py | 44 ++++++++++++++++++- .../character/character_details.html | 2 +- .../templates/character/character_form.html | 12 +++++ src/character/urls.py | 1 + src/character/views.py | 36 ++++++++++++++- 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/character/templates/character/character_form.html diff --git a/src/character/forms.py b/src/character/forms.py index e341ee8..227bd96 100644 --- a/src/character/forms.py +++ b/src/character/forms.py @@ -2,7 +2,7 @@ from django import forms from django.core.exceptions import ValidationError from django.db.models import Q -from character.models import Character, Path +from character.models import Character, Path, RacialCapability class EquipmentForm(forms.ModelForm): @@ -45,3 +45,45 @@ class AddPathForm(forms.Form): if len(list(filter(None, values))) != 1: raise ValidationError("Vous devez sélectionner une seule valeur.") return cleaned_data + + +class CharacterCreateForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields[ + "racial_capability" + ].queryset = RacialCapability.objects.select_related("race") + + class Meta: + model = Character + fields = [ + "name", + "race", + "profile", + "private", + "level", + "gender", + "age", + "height", + "weight", + "value_strength", + "value_dexterity", + "value_constitution", + "value_intelligence", + "value_wisdom", + "value_charisma", + "health_max", + "racial_capability", + "weapons", + "armor", + "shield", + "defense_misc", + "initiative_misc", + "equipment", + "money_pp", + "money_po", + "money_pa", + "money_pc", + "damage_reduction", + "notes", + ] diff --git a/src/character/templates/character/character_details.html b/src/character/templates/character/character_details.html index 5b2b099..32cda83 100644 --- a/src/character/templates/character/character_details.html +++ b/src/character/templates/character/character_details.html @@ -17,7 +17,7 @@ {% endif %} {% if character|managed_by:user %}

- Edit + Edit

{% endif %}

diff --git a/src/character/templates/character/character_form.html b/src/character/templates/character/character_form.html new file mode 100644 index 0000000..1ed3e65 --- /dev/null +++ b/src/character/templates/character/character_form.html @@ -0,0 +1,12 @@ +{% extends "common/base.html" %} +{% load django_bootstrap5 %} + +{% block title %}Création de personnage{% endblock %} + +{% block content %} +

+ {% csrf_token %} + {% bootstrap_form form %} + +
+{% endblock %} diff --git a/src/character/urls.py b/src/character/urls.py index e22cc7e..8bb8625 100644 --- a/src/character/urls.py +++ b/src/character/urls.py @@ -7,6 +7,7 @@ urlpatterns = [ path("", views.characters_list, name="list"), path("create/", views.character_create, name="create"), path("/", views.character_view, name="view"), + path("/change/", views.character_change, name="change"), path( "/health_change/", views.character_health_change, name="health_change" ), diff --git a/src/character/views.py b/src/character/views.py index af99733..da711b7 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -1,9 +1,10 @@ +from django.contrib import messages from django.contrib.auth.decorators import login_required from django.http import HttpResponse from django.shortcuts import get_object_or_404, redirect, render from django_htmx.http import trigger_client_event -from character.forms import AddPathForm, EquipmentForm +from character.forms import AddPathForm, CharacterCreateForm, EquipmentForm from character.models import Capability, Character, HarmfulState, Path from character.templatetags.character_extras import modifier from party.models import Party @@ -22,7 +23,38 @@ def characters_list(request): @login_required def character_create(request): - return redirect("admin:character_character_add") + if request.method == "POST": + form = CharacterCreateForm(request.POST) + if form.is_valid(): + character = form.save(commit=False) + character.player = request.user + character.recovery_points_remaining = character.recovery_points_max + character.luck_points_remaining = character.luck_points_max + character.mana_remaining = character.mana_max + character.health_remaining = character.health_max + character.save() + form.save_m2m() + messages.success(request, f"{character.name} a été créé.") + return redirect("character:list") + else: + form = CharacterCreateForm() + context = {"form": form} + return render(request, "character/character_form.html", context) + + +@login_required +def character_change(request, pk: int): + character = get_object_or_404(Character.objects.managed_by(request.user), pk=pk) + if request.method == "POST": + form = CharacterCreateForm(request.POST, instance=character) + if form.is_valid(): + character = form.save() + messages.success(request, f"{character.name} a été enregistré.") + return redirect(character.get_absolute_url()) + else: + form = CharacterCreateForm(instance=character) + context = {"form": form} + return render(request, "character/character_form.html", context) @login_required