Add create and update character views

This commit is contained in:
Gabriel Augendre 2022-11-09 18:53:42 +01:00
parent ee2caf8a2f
commit 71faf4dbe1
5 changed files with 91 additions and 4 deletions

View file

@ -2,7 +2,7 @@ from django import forms
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db.models import Q from django.db.models import Q
from character.models import Character, Path from character.models import Character, Path, RacialCapability
class EquipmentForm(forms.ModelForm): class EquipmentForm(forms.ModelForm):
@ -45,3 +45,45 @@ class AddPathForm(forms.Form):
if len(list(filter(None, values))) != 1: if len(list(filter(None, values))) != 1:
raise ValidationError("Vous devez sélectionner une seule valeur.") raise ValidationError("Vous devez sélectionner une seule valeur.")
return cleaned_data 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",
]

View file

@ -17,7 +17,7 @@
{% endif %} {% endif %}
{% if character|managed_by:user %} {% if character|managed_by:user %}
<p> <p>
<a href="{% url "admin:character_character_change" object_id=character.pk %}">Edit</a> <a href="{% url "character:change" pk=character.pk %}">Edit</a>
</p> </p>
{% endif %} {% endif %}
<p> <p>

View file

@ -0,0 +1,12 @@
{% extends "common/base.html" %}
{% load django_bootstrap5 %}
{% block title %}Création de personnage{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{% bootstrap_form form %}
<button type="submit" class="btn btn-primary">Enregistrer</button>
</form>
{% endblock %}

View file

@ -7,6 +7,7 @@ urlpatterns = [
path("", views.characters_list, name="list"), path("", views.characters_list, name="list"),
path("create/", views.character_create, name="create"), path("create/", views.character_create, name="create"),
path("<int:pk>/", views.character_view, name="view"), path("<int:pk>/", views.character_view, name="view"),
path("<int:pk>/change/", views.character_change, name="change"),
path( path(
"<int:pk>/health_change/", views.character_health_change, name="health_change" "<int:pk>/health_change/", views.character_health_change, name="health_change"
), ),

View file

@ -1,9 +1,10 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django_htmx.http import trigger_client_event 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.models import Capability, Character, HarmfulState, Path
from character.templatetags.character_extras import modifier from character.templatetags.character_extras import modifier
from party.models import Party from party.models import Party
@ -22,7 +23,38 @@ def characters_list(request):
@login_required @login_required
def character_create(request): 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 @login_required