From 702b5f036eb2902cf2dd109f2610dcbd814c7e7f Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 30 Oct 2022 10:00:37 +0100 Subject: [PATCH] Import racial capabilities --- .../management/commands/import_races.py | 21 +++++++++++++++- ...11_alter_racialcapability_name_and_more.py | 24 +++++++++++++++++++ src/character/migrations/max_migration.txt | 2 +- src/character/models/capabilities.py | 19 ++++++++++++--- 4 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/character/migrations/0011_alter_racialcapability_name_and_more.py diff --git a/src/character/management/commands/import_races.py b/src/character/management/commands/import_races.py index c51343f..ea4f71a 100644 --- a/src/character/management/commands/import_races.py +++ b/src/character/management/commands/import_races.py @@ -2,7 +2,7 @@ from django.core.management import BaseCommand from selenium import webdriver from selenium.webdriver.common.by import By -from character.models import Race +from character.models import Race, RacialCapability class Command(BaseCommand): @@ -26,6 +26,25 @@ class Command(BaseCommand): race, _ = Race.objects.update_or_create(name=name, defaults={"url": url}) self.stdout.write(self.style.SUCCESS(f"Created/updated race {race}")) + racial_cap = self.selenium.find_element( + By.CSS_SELECTOR, ".field--name-abilities" + ) + racial_name = ( + racial_cap.find_element(By.TAG_NAME, "strong") + .text.strip() + .removesuffix(":") + .strip() + ) + description = ( + racial_cap.text.replace(racial_name, "").strip().removeprefix(":").strip() + ) + racial, _ = RacialCapability.objects.update_or_create( + name=racial_name, + race=race, + defaults={"description": description, "url": url}, + ) + self.stdout.write(self.style.SUCCESS(f"Created/updated racial cap {racial}")) + def fix_name(self, name: str) -> str: if name == "Elfe, haut": return "Haut-Elfe" diff --git a/src/character/migrations/0011_alter_racialcapability_name_and_more.py b/src/character/migrations/0011_alter_racialcapability_name_and_more.py new file mode 100644 index 0000000..fc6f88f --- /dev/null +++ b/src/character/migrations/0011_alter_racialcapability_name_and_more.py @@ -0,0 +1,24 @@ +# Generated by Django 4.1.2 on 2022-10-30 08:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("character", "0010_racialcapability_url"), + ] + + operations = [ + migrations.AlterField( + model_name="racialcapability", + name="name", + field=models.CharField(max_length=100), + ), + migrations.AddConstraint( + model_name="racialcapability", + constraint=models.UniqueConstraint( + models.F("name"), models.F("race"), name="unique_name_race" + ), + ), + ] diff --git a/src/character/migrations/max_migration.txt b/src/character/migrations/max_migration.txt index 3350a6b..ae05ec1 100644 --- a/src/character/migrations/max_migration.txt +++ b/src/character/migrations/max_migration.txt @@ -1 +1 @@ -0010_racialcapability_url +0011_alter_racialcapability_name_and_more diff --git a/src/character/models/capabilities.py b/src/character/models/capabilities.py index 79b640a..5fea011 100644 --- a/src/character/models/capabilities.py +++ b/src/character/models/capabilities.py @@ -45,11 +45,24 @@ class Capability(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.M verbose_name_plural = "Capabilities" -class RacialCapability( - DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.Model -): +class RacialCapabilityManager(models.Manager): + def get_by_natural_key(self, name: str, race_id: int): + return self.get(name=name, race_id=race_id) + + +class RacialCapability(DocumentedModel, TimeStampedModel, models.Model): + name = models.CharField(max_length=100, blank=False, null=False) race = models.ForeignKey("character.Race", on_delete=models.CASCADE) description = models.TextField() + objects = RacialCapabilityManager() + class Meta: verbose_name_plural = "Racial capabilities" + constraints = [models.UniqueConstraint("name", "race", name="unique_name_race")] + + def __str__(self): + return f"{self.name} ({self.race})" + + def natural_key(self): + return (self.name, self.race_id)