mirror of
https://github.com/Crocmagnon/charasheet.git
synced 2024-12-22 13:51:49 +01:00
Partial path import
This commit is contained in:
parent
ff3cbcd225
commit
9aeaceaab3
5 changed files with 116 additions and 2 deletions
|
@ -38,11 +38,18 @@ class RacialCapabilityAdmin(admin.ModelAdmin):
|
|||
search_fields = ["name", "description"]
|
||||
|
||||
|
||||
class PathInline(admin.TabularInline):
|
||||
model = models.Path
|
||||
fields = ["name"]
|
||||
extra = 0
|
||||
|
||||
|
||||
@admin.register(models.Profile)
|
||||
class ProfileAdmin(admin.ModelAdmin):
|
||||
list_display = ["name", "life_dice", "magical_strength"]
|
||||
list_filter = ["life_dice", "magical_strength"]
|
||||
search_fields = ["name"]
|
||||
inlines = [PathInline]
|
||||
|
||||
|
||||
class RacialCapabilityInline(admin.TabularInline):
|
||||
|
@ -54,7 +61,7 @@ class RacialCapabilityInline(admin.TabularInline):
|
|||
class RaceAdmin(admin.ModelAdmin):
|
||||
list_display = ["name"]
|
||||
search_fields = ["name"]
|
||||
inlines = [RacialCapabilityInline]
|
||||
inlines = [RacialCapabilityInline, PathInline]
|
||||
|
||||
|
||||
@admin.register(models.Character)
|
||||
|
|
80
src/character/management/commands/import_paths.py
Normal file
80
src/character/management/commands/import_paths.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
from django.core.management import BaseCommand
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.common.by import By
|
||||
|
||||
from character.models import Path, Profile, Race
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
url = "https://www.co-drs.org/fr/jeu/voies"
|
||||
self.setup_selenium()
|
||||
self.selenium.get(url)
|
||||
anchors = self.selenium.find_elements(
|
||||
By.CSS_SELECTOR, ".card-body .card-title a"
|
||||
)
|
||||
urls = [anchor.get_attribute("href") for anchor in anchors]
|
||||
for url in urls:
|
||||
try:
|
||||
self.import_path(url)
|
||||
except Exception as e:
|
||||
print(f"{type(e)}: {e}")
|
||||
self.stdout.write(f"Finished processing {len(urls)} paths.")
|
||||
|
||||
def import_path(self, url: str):
|
||||
self.selenium.get(url)
|
||||
name = self.selenium.find_element(By.TAG_NAME, "h1").text
|
||||
category = self.get_category(name)
|
||||
profile = None
|
||||
if category == Path.Category.PROFILE:
|
||||
profile = self.get_profile()
|
||||
race = None
|
||||
if category == Path.Category.RACE:
|
||||
profile = self.get_race(name)
|
||||
|
||||
path, _ = Path.objects.update_or_create(
|
||||
name=name,
|
||||
defaults={"category": category, "profile": profile, "race": race},
|
||||
)
|
||||
self.stdout.write(self.style.SUCCESS(f"Created/updated path {path}"))
|
||||
|
||||
def get_category(self, name: str) -> Path.Category | None:
|
||||
try:
|
||||
category = (
|
||||
self.selenium.find_element(
|
||||
By.CSS_SELECTOR, ".field--name-type .field__item"
|
||||
)
|
||||
.text.lower()
|
||||
.strip()
|
||||
)
|
||||
except Exception:
|
||||
self.stdout.write(
|
||||
self.style.WARNING(
|
||||
f"Couldn't find category for {name}. Defaulting to profile."
|
||||
)
|
||||
)
|
||||
return Path.Category.PROFILE
|
||||
|
||||
if category == "personnage":
|
||||
return Path.Category.PROFILE
|
||||
if category == "créature":
|
||||
return Path.Category.CREATURE
|
||||
return Path.Category(category)
|
||||
|
||||
def get_profile(self) -> Profile:
|
||||
profile_name = self.selenium.find_element(
|
||||
By.CSS_SELECTOR, ".field--name-type + strong + a"
|
||||
).text
|
||||
return Profile.objects.get_by_natural_key(profile_name)
|
||||
|
||||
def get_race(self, path_name: str) -> Race:
|
||||
return None
|
||||
to_remove = [""]
|
||||
for text in to_remove:
|
||||
path_name = path_name.replace(text, "")
|
||||
return Race.objects.get(name__iexact=path_name)
|
||||
|
||||
def setup_selenium(self):
|
||||
options = webdriver.FirefoxOptions()
|
||||
options.add_argument("-headless")
|
||||
self.selenium = webdriver.Firefox(options=options)
|
26
src/character/migrations/0006_alter_path_category.py
Normal file
26
src/character/migrations/0006_alter_path_category.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
# Generated by Django 4.1.2 on 2022-10-29 08:49
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("character", "0005_profile_notes_alter_character_mana_consumed_and_more"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="path",
|
||||
name="category",
|
||||
field=models.CharField(
|
||||
choices=[
|
||||
("profile", "Profile"),
|
||||
("race", "Race"),
|
||||
("prestige", "Prestige"),
|
||||
("creature", "Creature"),
|
||||
],
|
||||
max_length=20,
|
||||
),
|
||||
),
|
||||
]
|
|
@ -1 +1 @@
|
|||
0005_profile_notes_alter_character_mana_consumed_and_more
|
||||
0006_alter_path_category
|
||||
|
|
|
@ -25,6 +25,7 @@ class Path(UniquelyNamedModel, TimeStampedModel, models.Model):
|
|||
PROFILE = "profile", "Profile"
|
||||
RACE = "race", "Race"
|
||||
PRESTIGE = "prestige", "Prestige"
|
||||
CREATURE = "creature", "Creature"
|
||||
|
||||
category = models.CharField(max_length=20, choices=Category.choices)
|
||||
notes = models.TextField(blank=True)
|
||||
|
|
Loading…
Reference in a new issue