From eeebbb4ca7a8d77b42d5ce127720c1e17f84a462 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 31 Oct 2022 23:23:28 +0100 Subject: [PATCH] Allow adding new paths from sheet --- src/character/forms.py | 14 ++++++++++- src/character/models/capabilities.py | 6 +++++ .../character/paths_and_capabilities.html | 23 +++++++++++++++++++ src/character/templates/character/view.html | 7 ++---- src/character/urls.py | 1 + src/character/views.py | 20 ++++++++++++++-- 6 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 src/character/templates/character/paths_and_capabilities.html diff --git a/src/character/forms.py b/src/character/forms.py index f96cbe1..2bc8c42 100644 --- a/src/character/forms.py +++ b/src/character/forms.py @@ -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" diff --git a/src/character/models/capabilities.py b/src/character/models/capabilities.py index 1bc0eea..20c94dd 100644 --- a/src/character/models/capabilities.py +++ b/src/character/models/capabilities.py @@ -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"] diff --git a/src/character/templates/character/paths_and_capabilities.html b/src/character/templates/character/paths_and_capabilities.html new file mode 100644 index 0000000..e997d8b --- /dev/null +++ b/src/character/templates/character/paths_and_capabilities.html @@ -0,0 +1,23 @@ +
+
+ {% csrf_token %} +
+
+
+ {{ add_path_form.path }} + +
+
+
+
+
+ {% for path, capabilities in character.get_capabilities_by_path.items %} + {% include "character/path.html" %} + {% endfor %} +
+
diff --git a/src/character/templates/character/view.html b/src/character/templates/character/view.html index 850ae1d..eb63d64 100644 --- a/src/character/templates/character/view.html +++ b/src/character/templates/character/view.html @@ -1,4 +1,5 @@ {% extends "common/base.html" %} +{% load django_bootstrap5 %} {% load character_extras %} {% block title %}{{ character.name }}{% endblock %} @@ -336,10 +337,6 @@

Voies & Capacités

-
- {% for path, capabilities in character.get_capabilities_by_path.items %} - {% include "character/path.html" %} - {% endfor %} -
+ {% include "character/paths_and_capabilities.html" %} {% include "character/notes_display.html" %} {% endblock %} diff --git a/src/character/urls.py b/src/character/urls.py index e2287de..7a0e8e9 100644 --- a/src/character/urls.py +++ b/src/character/urls.py @@ -48,4 +48,5 @@ urlpatterns = [ views.remove_last_in_path, name="remove_last_in_path", ), + path("/add_path/", views.add_path, name="add_path"), ] diff --git a/src/character/views.py b/src/character/views.py index 3d956ca..3c732e7 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -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(