From c4132c4d98b8371fa07a0169d552729690872d23 Mon Sep 17 00:00:00 2001 From: Max Savenya Date: Tue, 5 Nov 2024 17:59:59 +0200 Subject: [PATCH 1/4] Solution --- db/models.py | 31 +++++++++++++++++++++++++++++++ main.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/db/models.py b/db/models.py index 137941ff..deb3f309 100644 --- a/db/models.py +++ b/db/models.py @@ -1 +1,32 @@ 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, blank=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, blank=True) + race = models.ForeignKey(Race, on_delete=models.CASCADE) + guild = models.ForeignKey( + Guild, + on_delete=models.SET_NULL, + null=True, + blank=True, + related_name="players" + ) + created_at = models.DateTimeField(auto_now_add=True) diff --git a/main.py b/main.py index e71f55d8..dc89aa11 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,44 @@ 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 file: + players_data = json.load(file) + + for nickname, player_data in players_data.items(): + race_data = player_data["race"] + race, _ = Race.objects.get_or_create( + name=race_data["name"], defaults={ + "description": race_data.get("description", "") + } + ) + + for skill_data in race_data.get("skills", []): + Skill.objects.get_or_create( + name=skill_data["name"], + race=race, + defaults={"bonus": skill_data["bonus"]} + ) + + guild = None + if player_data.get("guild"): + guild_data = player_data["guild"] + guild, _ = Guild.objects.get_or_create( + name=guild_data["name"], + defaults={ + "description": guild_data.get("description", None) + } + ) + + Player.objects.get_or_create(nickname=nickname, defaults={ + "email": player_data["email"], + "bio": player_data["bio"], + "race": race, + "guild": guild + }) if __name__ == "__main__": From 4d3c373d922fa11c703f818258ef1fea392b641e Mon Sep 17 00:00:00 2001 From: Max Savenya Date: Tue, 5 Nov 2024 18:01:20 +0200 Subject: [PATCH 2/4] Solution --- db/migrations/0001_initial.py | 52 +++++++++++++++++++ ...player_guild_alter_player_race_and_more.py | 29 +++++++++++ 2 files changed, 81 insertions(+) create mode 100644 db/migrations/0001_initial.py create mode 100644 db/migrations/0002_alter_player_guild_alter_player_race_and_more.py diff --git a/db/migrations/0001_initial.py b/db/migrations/0001_initial.py new file mode 100644 index 00000000..ddc94eb2 --- /dev/null +++ b/db/migrations/0001_initial.py @@ -0,0 +1,52 @@ +# Generated by Django 4.0.2 on 2024-11-05 09:44 + +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(blank=True, 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, related_name='skills', 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(blank=True, 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, related_name='members', to='db.guild')), + ('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='players', to='db.race')), + ], + ), + ] diff --git a/db/migrations/0002_alter_player_guild_alter_player_race_and_more.py b/db/migrations/0002_alter_player_guild_alter_player_race_and_more.py new file mode 100644 index 00000000..5b0d19ec --- /dev/null +++ b/db/migrations/0002_alter_player_guild_alter_player_race_and_more.py @@ -0,0 +1,29 @@ +# Generated by Django 4.0.2 on 2024-11-05 09:59 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('db', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='player', + name='guild', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='players', to='db.guild'), + ), + migrations.AlterField( + model_name='player', + name='race', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race'), + ), + migrations.AlterField( + model_name='skill', + name='race', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race'), + ), + ] From 161c7718b504cd252cf76cd1899eb23d96157bc7 Mon Sep 17 00:00:00 2001 From: Max Savenya Date: Tue, 5 Nov 2024 18:03:56 +0200 Subject: [PATCH 3/4] Solution --- db/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/models.py b/db/models.py index deb3f309..4e64eabd 100644 --- a/db/models.py +++ b/db/models.py @@ -9,7 +9,7 @@ class Race(models.Model): 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) + race = models.ForeignKey(Race, on_delete=models.CASCADE) class Guild(models.Model): From 84413ee27426f05a05f8890b6ba8db310097dbe8 Mon Sep 17 00:00:00 2001 From: Max Savenya Date: Wed, 6 Nov 2024 17:00:47 +0200 Subject: [PATCH 4/4] Solution --- db/migrations/0001_initial.py | 6 +++--- .../0002_alter_player_guild_alter_player_race_and_more.py | 6 +++--- main.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/db/migrations/0001_initial.py b/db/migrations/0001_initial.py index ddc94eb2..27473ac6 100644 --- a/db/migrations/0001_initial.py +++ b/db/migrations/0001_initial.py @@ -34,7 +34,7 @@ class Migration(migrations.Migration): ('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, related_name='skills', to='db.race')), + ('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='skills', to='Race')), ], ), migrations.CreateModel( @@ -45,8 +45,8 @@ class Migration(migrations.Migration): ('email', models.EmailField(max_length=255)), ('bio', models.CharField(blank=True, 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, related_name='members', to='db.guild')), - ('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='players', to='db.race')), + ('guild', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='members', to='Guild')), + ('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='players', to='Race')), ], ), ] diff --git a/db/migrations/0002_alter_player_guild_alter_player_race_and_more.py b/db/migrations/0002_alter_player_guild_alter_player_race_and_more.py index 5b0d19ec..40f643bd 100644 --- a/db/migrations/0002_alter_player_guild_alter_player_race_and_more.py +++ b/db/migrations/0002_alter_player_guild_alter_player_race_and_more.py @@ -14,16 +14,16 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='player', name='guild', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='players', to='db.guild'), + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='players', to='Guild'), ), migrations.AlterField( model_name='player', name='race', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Race'), ), migrations.AlterField( model_name='skill', name='race', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Race'), ), ] diff --git a/main.py b/main.py index dc89aa11..9167ffdf 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,7 @@ def main() -> None: guild, _ = Guild.objects.get_or_create( name=guild_data["name"], defaults={ - "description": guild_data.get("description", None) + "description": guild_data.get("description", "") } )