From 7b4d379f43b8ce8aad90e6c1c95992a3a979477d Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Wed, 9 Nov 2022 20:49:57 +0100 Subject: [PATCH] Display whole paths at first rank --- src/character/models/character.py | 22 +++++++++++--- .../character_details/capability.html | 30 ++++++++++--------- .../snippets/character_details/path.html | 2 +- .../paths_and_capabilities.html | 2 +- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/character/models/character.py b/src/character/models/character.py index 90d65a9..239fc04 100644 --- a/src/character/models/character.py +++ b/src/character/models/character.py @@ -1,5 +1,6 @@ import collections from collections.abc import Iterable +from dataclasses import dataclass import markdown from django.db import models @@ -105,6 +106,12 @@ DEFAULT_NOTES = """ """.lstrip() +@dataclass +class CharacterCapability: + capability: Capability + known: bool = False + + class Character(models.Model): class Gender(models.TextChoices): MALE = "M", "Mâle" @@ -333,15 +340,22 @@ class Character(models.Model): } return modifier_map.get(Weapon.Category(weapon.category), 0) + self.level - def get_capabilities_by_path(self) -> dict[Path, list[Capability]]: + def get_capabilities_by_path(self) -> dict[Path, list[CharacterCapability]]: capabilities_by_path = collections.defaultdict(list) - for capability in self.capabilities.all(): - capabilities_by_path[capability.path].append(capability) + character_capabilities = self.capabilities.all() + character_paths = {capability.path for capability in character_capabilities} + for path in character_paths: + for capability in path.capabilities.all(): + capabilities_by_path[capability.path].append( + CharacterCapability( + capability, known=capability in character_capabilities + ) + ) return dict( sorted( ( - (path, sorted(capabilities, key=lambda x: x.rank)) + (path, sorted(capabilities, key=lambda x: x.capability.rank)) for path, capabilities in capabilities_by_path.items() ), key=lambda x: x[0].name, diff --git a/src/character/templates/character/snippets/character_details/capability.html b/src/character/templates/character/snippets/character_details/capability.html index cbbfd0f..792c1ce 100644 --- a/src/character/templates/character/snippets/character_details/capability.html +++ b/src/character/templates/character/snippets/character_details/capability.html @@ -1,16 +1,18 @@ -
-

- -

-
-
- {{ capability.description }} +{% with character_capability.capability as capability %} +
+

+ +

+
+
+ {{ capability.description }} +
-
+{% endwith %} diff --git a/src/character/templates/character/snippets/character_details/path.html b/src/character/templates/character/snippets/character_details/path.html index 0b80b17..6a547c1 100644 --- a/src/character/templates/character/snippets/character_details/path.html +++ b/src/character/templates/character/snippets/character_details/path.html @@ -25,7 +25,7 @@
{{ path.notes }}
{% endif %}
- {% for capability in capabilities %} + {% for character_capability in character_capabilities %} {% include "character/snippets/character_details/capability.html" %} {% endfor %}
diff --git a/src/character/templates/character/snippets/character_details/paths_and_capabilities.html b/src/character/templates/character/snippets/character_details/paths_and_capabilities.html index 223f999..25b9333 100644 --- a/src/character/templates/character/snippets/character_details/paths_and_capabilities.html +++ b/src/character/templates/character/snippets/character_details/paths_and_capabilities.html @@ -22,7 +22,7 @@ {% endif %}
- {% for path, capabilities in character.get_capabilities_by_path.items %} + {% for path, character_capabilities in character.get_capabilities_by_path.items %} {% include "character/snippets/character_details/path.html" %} {% endfor %}