diff --git a/db/migrations/0001_initial.py b/db/migrations/0001_initial.py new file mode 100644 index 00000000..897fbe9b --- /dev/null +++ b/db/migrations/0001_initial.py @@ -0,0 +1,52 @@ +# Generated by Django 4.0.2 on 2023-08-28 17:05 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Guild', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, unique=True)), + ('description', models.TextField(null=True)), + ], + ), + migrations.CreateModel( + name='Race', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, unique=True)), + ('description', models.TextField(blank=True)), + ], + ), + migrations.CreateModel( + name='Skill', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, unique=True)), + ('bonus', models.CharField(max_length=255)), + ('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')), + ], + ), + migrations.CreateModel( + name='Player', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('nickname', models.CharField(max_length=255, unique=True)), + ('email', models.EmailField(max_length=255)), + ('bio', models.CharField(max_length=255)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('guild', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.guild')), + ('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')), + ], + ), + ] diff --git a/db/models.py b/db/models.py index 137941ff..be8607c7 100644 --- a/db/models.py +++ b/db/models.py @@ -1 +1,31 @@ from django.db import models + + +class Race(models.Model): + name = models.CharField(max_length=255, unique=True) + description = models.TextField(blank=True) + + +class Skill(models.Model): + name = models.CharField(max_length=255, unique=True) + bonus = models.CharField(max_length=255) + race = models.ForeignKey(Race, on_delete=models.CASCADE) + + +class Guild(models.Model): + name = models.CharField(max_length=255, unique=True) + description = models.TextField(null=True) + + +class Player(models.Model): + nickname = models.CharField(max_length=255, unique=True) + email = models.EmailField(max_length=255) + bio = models.CharField(max_length=255) + race = models.ForeignKey(Race, on_delete=models.CASCADE) + guild = models.ForeignKey( + Guild, + on_delete=models.SET_NULL, + null=True, + blank=True + ) + created_at = models.DateTimeField(auto_now_add=True) diff --git a/main.py b/main.py index e71f55d8..1036b978 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,47 @@ import init_django_orm # noqa: F401 - +import json from db.models import Race, Skill, Player, Guild def main() -> None: - pass + with open("players.json", "r") as json_file: + data = json.load(json_file) + + for player_name, entry in data.items(): + race_data = entry.get("race", {}) + race, _ = Race.objects.get_or_create( + name=race_data.get("name", ""), + description=race_data.get("description", "") + ) + + guild_data = entry.get("guild", {}) + if guild_data: + guild, _ = Guild.objects.get_or_create( + name=guild_data.get("name", ""), + description=guild_data.get("description", "") + ) + else: + guild = None + + player_data = { + "nickname": player_name, + "email": entry.get("email", ""), + "bio": entry.get("bio", ""), + "race": race, + "guild": guild, + } + + player, _ = Player.objects.get_or_create( + nickname=player_name, + defaults=player_data + ) + + for skill_entry in race_data.get("skills", []): + skill, _ = Skill.objects.get_or_create( + name=skill_entry.get("name", ""), + bonus=skill_entry.get("bonus", 0), + race=race + ) if __name__ == "__main__":