Display whole paths at first rank

This commit is contained in:
Gabriel Augendre 2022-11-09 20:49:57 +01:00
parent 467c582a83
commit 7b4d379f43
4 changed files with 36 additions and 20 deletions

View file

@ -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,

View file

@ -1,6 +1,7 @@
{% with character_capability.capability as capability %}
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button"
<button class="accordion-button collapsed {% if not character_capability.known %}text-bg-light{% endif %}" type="button"
data-bs-toggle="collapse" data-bs-target="#cap-{{ capability.pk }}">
{{ capability.rank }}.
{{ capability.name }}
@ -14,3 +15,4 @@
</div>
</div>
</div>
{% endwith %}

View file

@ -25,7 +25,7 @@
<div class="alert alert-primary">{{ path.notes }}</div>
{% endif %}
<div class="accordion capabilities">
{% for capability in capabilities %}
{% for character_capability in character_capabilities %}
{% include "character/snippets/character_details/capability.html" %}
{% endfor %}
</div>

View file

@ -22,7 +22,7 @@
</form>
{% endif %}
<div class="row mt-2 gy-3">
{% 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 %}
</div>