From c667ed54c6a4b6e9aa58c02e73644e539e162171 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 19 Jun 2023 17:43:27 +0200 Subject: [PATCH] feat: add a path without capabilities (closes #28) --- src/character/admin.py | 2 +- .../migrations/0043_character_paths.py | 21 +++++++++++++++++++ src/character/migrations/max_migration.txt | 2 +- src/character/models/character.py | 7 +++++-- src/character/views.py | 21 +++++++++++++------ src/charasheet/settings.py | 15 ++++++++----- 6 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 src/character/migrations/0043_character_paths.py diff --git a/src/character/admin.py b/src/character/admin.py index f21d505..7c41cf9 100644 --- a/src/character/admin.py +++ b/src/character/admin.py @@ -171,7 +171,7 @@ class CharacterAdmin(admin.ModelAdmin): }, ), ("Race", {"fields": ["racial_capability"]}), - ("Capacités", {"fields": ["capabilities"]}), + ("Voies & capacités", {"fields": ["paths", "capabilities"]}), ("Chance", {"fields": [("luck_points_max", "luck_points_remaining")]}), ("Mana", {"fields": [("mana_max", "mana_remaining")]}), ( diff --git a/src/character/migrations/0043_character_paths.py b/src/character/migrations/0043_character_paths.py new file mode 100644 index 0000000..d494de3 --- /dev/null +++ b/src/character/migrations/0043_character_paths.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.2 on 2023-06-19 15:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("character", "0042_alter_pet_damage"), + ] + + operations = [ + migrations.AddField( + model_name="character", + name="paths", + field=models.ManyToManyField( + blank=True, + to="character.path", + verbose_name="voies", + ), + ), + ] diff --git a/src/character/migrations/max_migration.txt b/src/character/migrations/max_migration.txt index f728bc4..4f3b0c5 100644 --- a/src/character/migrations/max_migration.txt +++ b/src/character/migrations/max_migration.txt @@ -1 +1 @@ -0042_alter_pet_damage +0043_character_paths diff --git a/src/character/models/character.py b/src/character/models/character.py index ad2c4b2..dadb869 100644 --- a/src/character/models/character.py +++ b/src/character/models/character.py @@ -254,6 +254,7 @@ class Character(models.Model): blank=True, verbose_name="capacités", ) + paths = models.ManyToManyField("character.Path", blank=True, verbose_name="voies") equipment = models.TextField(blank=True, verbose_name="équipement") luck_points_remaining = models.PositiveSmallIntegerField( @@ -428,8 +429,10 @@ class Character(models.Model): def get_capabilities_by_path(self) -> dict[Path, list[CharacterCapability]]: capabilities_by_path = collections.defaultdict(list) - character_capabilities = self.capabilities.all() - character_paths = {capability.path for capability in character_capabilities} + character_capabilities = set(self.capabilities.all()) + character_paths = { + capability.path for capability in character_capabilities + } | set(self.paths.all()) for path in character_paths: for capability in path.capabilities.all(): capabilities_by_path[capability.path].append( diff --git a/src/character/views.py b/src/character/views.py index 1bb02d7..1e50b3b 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -103,8 +103,7 @@ def add_path(request, pk: int): path: Path = form.cleaned_data.get("character_path") or form.cleaned_data.get( "other_path", ) - cap = path.get_next_capability(character) - character.capabilities.add(cap) + character.paths.add(path) context["add_path_form"] = AddPathForm(character) else: context["add_path_form"] = form @@ -400,11 +399,21 @@ def remove_last_in_path(request, character_pk: int, path_pk: int): Character.objects.managed_by(request.user), pk=character_pk, ) - last_rank = max( - character.capabilities.filter(path_id=path_pk).values_list("rank", flat=True), + capabilities = character.capabilities.filter(path_id=path_pk).values_list( + "rank", + flat=True, ) - cap = Capability.objects.get(path_id=path_pk, rank=last_rank) - character.capabilities.remove(cap) + if len(capabilities) == 0: + character.paths.remove(path_pk) + else: + last_rank = max( + character.capabilities.filter(path_id=path_pk).values_list( + "rank", + flat=True, + ), + ) + cap = Capability.objects.get(path_id=path_pk, rank=last_rank) + character.capabilities.remove(cap) context = { "character": character, "add_path_form": AddPathForm(character), diff --git a/src/charasheet/settings.py b/src/charasheet/settings.py index 50a918e..9a3db4a 100644 --- a/src/charasheet/settings.py +++ b/src/charasheet/settings.py @@ -1,3 +1,4 @@ +import logging import os from pathlib import Path @@ -41,12 +42,16 @@ INTERNAL_IPS = [ ALLOWED_HOSTS = env("ALLOWED_HOSTS") if DEBUG: - import socket + try: + import socket + + hostname = socket.gethostname() + ip = socket.gethostbyname(hostname) + INTERNAL_IPS.append(ip) + ALLOWED_HOSTS.append(ip) + except Exception as e: + logging.info("couldn't setup allowed host in debug: %s", e) - hostname = socket.gethostname() - ip = socket.gethostbyname(hostname) - INTERNAL_IPS.append(ip) - ALLOWED_HOSTS.append(ip) CSRF_TRUSTED_ORIGINS = env("CSRF_TRUSTED_ORIGINS")