Allow adding new paths from sheet

This commit is contained in:
Gabriel Augendre 2022-10-31 23:23:28 +01:00
parent f43116c635
commit eeebbb4ca7
6 changed files with 63 additions and 8 deletions

View file

@ -1,9 +1,21 @@
from django import forms
from character.models import Character
from character.models import Character, Path
class EquipmentForm(forms.ModelForm):
class Meta:
model = Character
fields = ["equipment", "money_pp", "money_po", "money_pa", "money_pc"]
class AddPathForm(forms.Form):
path = forms.ModelChoiceField(Path.objects.none())
def __init__(self, character: Character, *args, **kwargs):
super().__init__(*args, **kwargs)
paths = {cap.path_id for cap in character.capabilities.all()}
self.fields["path"].queryset = Path.objects.exclude(pk__in=paths).order_by(
"profile__name", "race__name"
)
self.fields["path"].widget.attrs["class"] = "form-select"

View file

@ -40,6 +40,12 @@ class Path(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.Model):
verbose_name = "Voie"
verbose_name_plural = "Voies"
def __str__(self):
name = f"{self.display_name}"
if self.related_to:
name += f" ({self.related_to.name})"
return name
@property
def display_name(self) -> str:
to_remove = ["voie de la", "voie de l'", "voie du", "voie des", "voie de"]

View file

@ -0,0 +1,23 @@
<div id="paths-and-capabilities">
<form>
{% csrf_token %}
<div class="row">
<div class="col col-lg-6">
<div class="input-group">
{{ add_path_form.path }}
<button class="btn btn-primary"
hx-target="#paths-and-capabilities"
hx-swap="outerHTML"
hx-post="{% url "character:add_path" pk=character.pk %}">
Sélectionner
</button>
</div>
</div>
</div>
</form>
<div class="row mt-2 gy-3">
{% for path, capabilities in character.get_capabilities_by_path.items %}
{% include "character/path.html" %}
{% endfor %}
</div>
</div>

View file

@ -1,4 +1,5 @@
{% extends "common/base.html" %}
{% load django_bootstrap5 %}
{% load character_extras %}
{% block title %}{{ character.name }}{% endblock %}
@ -336,10 +337,6 @@
</div>
</div>
<h2>Voies & Capacités</h2>
<div class="row gy-3">
{% for path, capabilities in character.get_capabilities_by_path.items %}
{% include "character/path.html" %}
{% endfor %}
</div>
{% include "character/paths_and_capabilities.html" %}
{% include "character/notes_display.html" %}
{% endblock %}

View file

@ -48,4 +48,5 @@ urlpatterns = [
views.remove_last_in_path,
name="remove_last_in_path",
),
path("<int:pk>/add_path/", views.add_path, name="add_path"),
]

View file

@ -3,7 +3,7 @@ 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 EquipmentForm
from character.forms import AddPathForm, EquipmentForm
from character.models import Capability, Character, Path
@ -30,10 +30,26 @@ def character_view(request, pk: int):
.prefetch_related("capabilities__path", "weapons"),
pk=pk,
)
context = {"character": character}
add_path_form = AddPathForm(character)
context = {"character": character, "add_path_form": add_path_form}
return render(request, "character/view.html", context)
@login_required
def add_path(request, pk: int):
character = get_object_or_404(Character.objects.filter(player=request.user), pk=pk)
form = AddPathForm(character, request.POST)
context = {"character": character}
if form.is_valid():
path: Path = form.cleaned_data.get("path")
cap = path.get_next_capability(character)
character.capabilities.add(cap)
context["add_path_form"] = AddPathForm(character)
else:
context["add_path_form"] = form
return render(request, "character/paths_and_capabilities.html", context)
@login_required
def character_health_change(request, pk: int):
character = get_object_or_404(