Fix capabilities import

This commit is contained in:
Gabriel Augendre 2022-11-01 09:13:34 +01:00
parent d17a5bba70
commit ba4c5457d0
5 changed files with 1616 additions and 865 deletions

File diff suppressed because it is too large Load diff

View file

@ -34,7 +34,7 @@ class Command(BaseCommand):
) )
name = title[0].replace("", "'").strip() name = title[0].replace("", "'").strip()
rank = int(title[1].replace("rang ", "")) rank = int(title[1].replace("rang ", ""))
path = self.get_path(card, name) paths = self.get_paths(card, name)
limited = False limited = False
if "(L)" in name: if "(L)" in name:
limited = True limited = True
@ -49,46 +49,39 @@ class Command(BaseCommand):
.text.strip() .text.strip()
.replace("", "'") .replace("", "'")
) )
for path in paths:
try: try:
capability, _ = Capability.objects.update_or_create( capability, _ = Capability.objects.update_or_create(
name=name, rank=rank,
path=path,
defaults={ defaults={
"rank": rank, "name": name,
"path": path,
"limited": limited, "limited": limited,
"spell": spell, "spell": spell,
"description": description, "description": description,
"url": "https://www.co-drs.org/fr/jeu/capacites", "url": "https://www.co-drs.org/fr/jeu/capacites",
}, },
) )
self.stdout.write(self.style.SUCCESS(f"Created/updated cap {capability}")) self.stdout.write(
self.style.SUCCESS(f"Created/updated cap {capability}")
)
except Exception as e: except Exception as e:
self.stdout.write( self.stdout.write(
self.style.ERROR(f"Couldn't create/update cap {name}: {e}") self.style.ERROR(f"Couldn't create/update cap {name}: {e}")
) )
def get_path(self, card: WebElement, name: str) -> Path | None: def get_paths(self, card: WebElement, name: str) -> list[Path]:
paths = []
try: try:
path_name = ( for elem in card.find_elements(By.CSS_SELECTOR, ".card-back .paths a"):
card.find_element(By.CSS_SELECTOR, ".card-back .paths a") path_name = elem.text.replace("", "'").strip()
.text.replace("", "'") paths.append(Path.objects.get(name__iexact=path_name))
.strip()
)
except Exception: except Exception:
self.stdout.write( self.stdout.write(
self.style.WARNING(f"Couldn't find path in card for cap '{name}'.") self.style.WARNING(f"Couldn't find path in card for cap '{name}'.")
) )
return None return []
try: return paths
path = Path.objects.get(name__iexact=path_name)
return path
except Exception:
self.stdout.write(
self.style.WARNING(
f"Couldn't find path name '{path_name}' for cap '{name}'."
)
)
return None
def setup_selenium(self): def setup_selenium(self):
options = webdriver.FirefoxOptions() options = webdriver.FirefoxOptions()

View file

@ -0,0 +1,18 @@
# Generated by Django 4.1.2 on 2022-11-01 08:12
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("character", "0025_alter_capability_path"),
]
operations = [
migrations.AlterField(
model_name="capability",
name="name",
field=models.CharField(max_length=100, verbose_name="nom"),
),
]

View file

@ -1 +1 @@
0025_alter_capability_path 0026_alter_capability_name

View file

@ -76,7 +76,13 @@ class Path(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.Model):
return False return False
class Capability(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.Model): class CapabilityManager(models.Manager):
def get_by_natural_key(self, path_id, rank):
return self.get(path_id=path_id, rank=rank)
class Capability(DocumentedModel, TimeStampedModel, models.Model):
name = models.CharField(max_length=100, blank=False, null=False, verbose_name="nom")
path = models.ForeignKey( path = models.ForeignKey(
"character.Path", "character.Path",
on_delete=models.CASCADE, on_delete=models.CASCADE,
@ -94,6 +100,8 @@ class Capability(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.M
) )
description = models.TextField(verbose_name="description") description = models.TextField(verbose_name="description")
objects = CapabilityManager
class Meta: class Meta:
constraints = [models.UniqueConstraint("path", "rank", name="unique_path_rank")] constraints = [models.UniqueConstraint("path", "rank", name="unique_path_rank")]
verbose_name = "Capacité" verbose_name = "Capacité"
@ -105,6 +113,9 @@ class Capability(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.M
description += f" ({self.path.related_to.name})" description += f" ({self.path.related_to.name})"
return description return description
def natural_key(self):
return (self.path_id, self.rank)
class RacialCapabilityManager(models.Manager): class RacialCapabilityManager(models.Manager):
def get_by_natural_key(self, name: str, race_id: int): def get_by_natural_key(self, name: str, race_id: int):