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 import collections
from collections.abc import Iterable from collections.abc import Iterable
from dataclasses import dataclass
import markdown import markdown
from django.db import models from django.db import models
@ -105,6 +106,12 @@ DEFAULT_NOTES = """
""".lstrip() """.lstrip()
@dataclass
class CharacterCapability:
capability: Capability
known: bool = False
class Character(models.Model): class Character(models.Model):
class Gender(models.TextChoices): class Gender(models.TextChoices):
MALE = "M", "Mâle" MALE = "M", "Mâle"
@ -333,15 +340,22 @@ class Character(models.Model):
} }
return modifier_map.get(Weapon.Category(weapon.category), 0) + self.level 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) capabilities_by_path = collections.defaultdict(list)
for capability in self.capabilities.all(): character_capabilities = self.capabilities.all()
capabilities_by_path[capability.path].append(capability) 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( return dict(
sorted( 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() for path, capabilities in capabilities_by_path.items()
), ),
key=lambda x: x[0].name, key=lambda x: x[0].name,

View file

@ -1,6 +1,7 @@
<div class="accordion-item"> {% with character_capability.capability as capability %}
<div class="accordion-item">
<h2 class="accordion-header"> <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 }}"> data-bs-toggle="collapse" data-bs-target="#cap-{{ capability.pk }}">
{{ capability.rank }}. {{ capability.rank }}.
{{ capability.name }} {{ capability.name }}
@ -13,4 +14,5 @@
{{ capability.description }} {{ capability.description }}
</div> </div>
</div> </div>
</div> </div>
{% endwith %}

View file

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

View file

@ -22,7 +22,7 @@
</form> </form>
{% endif %} {% endif %}
<div class="row mt-2 gy-3"> <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" %} {% include "character/snippets/character_details/path.html" %}
{% endfor %} {% endfor %}
</div> </div>