mirror of
https://github.com/Crocmagnon/charasheet.git
synced 2024-11-04 22:03:56 +01:00
Fix capabilities import
This commit is contained in:
parent
d17a5bba70
commit
ba4c5457d0
5 changed files with 1616 additions and 865 deletions
File diff suppressed because it is too large
Load diff
|
@ -34,7 +34,7 @@ class Command(BaseCommand):
|
|||
)
|
||||
name = title[0].replace("’", "'").strip()
|
||||
rank = int(title[1].replace("rang ", ""))
|
||||
path = self.get_path(card, name)
|
||||
paths = self.get_paths(card, name)
|
||||
limited = False
|
||||
if "(L)" in name:
|
||||
limited = True
|
||||
|
@ -49,46 +49,39 @@ class Command(BaseCommand):
|
|||
.text.strip()
|
||||
.replace("’", "'")
|
||||
)
|
||||
try:
|
||||
capability, _ = Capability.objects.update_or_create(
|
||||
name=name,
|
||||
defaults={
|
||||
"rank": rank,
|
||||
"path": path,
|
||||
"limited": limited,
|
||||
"spell": spell,
|
||||
"description": description,
|
||||
"url": "https://www.co-drs.org/fr/jeu/capacites",
|
||||
},
|
||||
)
|
||||
self.stdout.write(self.style.SUCCESS(f"Created/updated cap {capability}"))
|
||||
except Exception as e:
|
||||
self.stdout.write(
|
||||
self.style.ERROR(f"Couldn't create/update cap {name}: {e}")
|
||||
)
|
||||
for path in paths:
|
||||
try:
|
||||
capability, _ = Capability.objects.update_or_create(
|
||||
rank=rank,
|
||||
path=path,
|
||||
defaults={
|
||||
"name": name,
|
||||
"limited": limited,
|
||||
"spell": spell,
|
||||
"description": description,
|
||||
"url": "https://www.co-drs.org/fr/jeu/capacites",
|
||||
},
|
||||
)
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(f"Created/updated cap {capability}")
|
||||
)
|
||||
except Exception as e:
|
||||
self.stdout.write(
|
||||
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:
|
||||
path_name = (
|
||||
card.find_element(By.CSS_SELECTOR, ".card-back .paths a")
|
||||
.text.replace("’", "'")
|
||||
.strip()
|
||||
)
|
||||
for elem in card.find_elements(By.CSS_SELECTOR, ".card-back .paths a"):
|
||||
path_name = elem.text.replace("’", "'").strip()
|
||||
paths.append(Path.objects.get(name__iexact=path_name))
|
||||
except Exception:
|
||||
self.stdout.write(
|
||||
self.style.WARNING(f"Couldn't find path in card for cap '{name}'.")
|
||||
)
|
||||
return None
|
||||
try:
|
||||
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
|
||||
return []
|
||||
return paths
|
||||
|
||||
def setup_selenium(self):
|
||||
options = webdriver.FirefoxOptions()
|
||||
|
|
18
src/character/migrations/0026_alter_capability_name.py
Normal file
18
src/character/migrations/0026_alter_capability_name.py
Normal 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"),
|
||||
),
|
||||
]
|
|
@ -1 +1 @@
|
|||
0025_alter_capability_path
|
||||
0026_alter_capability_name
|
||||
|
|
|
@ -76,7 +76,13 @@ class Path(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.Model):
|
|||
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(
|
||||
"character.Path",
|
||||
on_delete=models.CASCADE,
|
||||
|
@ -94,6 +100,8 @@ class Capability(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.M
|
|||
)
|
||||
description = models.TextField(verbose_name="description")
|
||||
|
||||
objects = CapabilityManager
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint("path", "rank", name="unique_path_rank")]
|
||||
verbose_name = "Capacité"
|
||||
|
@ -105,6 +113,9 @@ class Capability(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.M
|
|||
description += f" ({self.path.related_to.name})"
|
||||
return description
|
||||
|
||||
def natural_key(self):
|
||||
return (self.path_id, self.rank)
|
||||
|
||||
|
||||
class RacialCapabilityManager(models.Manager):
|
||||
def get_by_natural_key(self, name: str, race_id: int):
|
||||
|
|
Loading…
Reference in a new issue