From 76261f49791a3cd8dc4bb89328b3c17979619d0a Mon Sep 17 00:00:00 2001 From: Dmytro Kolodii Date: Tue, 18 Jul 2023 18:51:28 +0300 Subject: [PATCH 1/2] Solution --- db/migrations/0001_initial.py | 51 +++++++++++++++++++ db/migrations/0002_alter_player_created_at.py | 18 +++++++ db/migrations/0003_player_race.py | 20 ++++++++ db/models.py | 33 ++++++++++++ main.py | 38 +++++++++++++- 5 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 db/migrations/0001_initial.py create mode 100644 db/migrations/0002_alter_player_created_at.py create mode 100644 db/migrations/0003_player_race.py diff --git a/db/migrations/0001_initial.py b/db/migrations/0001_initial.py new file mode 100644 index 00000000..f82eed77 --- /dev/null +++ b/db/migrations/0001_initial.py @@ -0,0 +1,51 @@ +# Generated by Django 4.0.2 on 2023-07-16 13:39 + +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.DateField(auto_now_add=True)), + ('guild', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.guild')), + ], + ), + ] diff --git a/db/migrations/0002_alter_player_created_at.py b/db/migrations/0002_alter_player_created_at.py new file mode 100644 index 00000000..9deb32a6 --- /dev/null +++ b/db/migrations/0002_alter_player_created_at.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.2 on 2023-07-16 13:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('db', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='player', + name='created_at', + field=models.DateTimeField(auto_now_add=True), + ), + ] diff --git a/db/migrations/0003_player_race.py b/db/migrations/0003_player_race.py new file mode 100644 index 00000000..7aa6dc80 --- /dev/null +++ b/db/migrations/0003_player_race.py @@ -0,0 +1,20 @@ +# Generated by Django 4.0.2 on 2023-07-16 21:11 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('db', '0002_alter_player_created_at'), + ] + + operations = [ + migrations.AddField( + model_name='player', + name='race', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race'), + preserve_default=False, + ), + ] diff --git a/db/models.py b/db/models.py index 137941ff..91c5b92d 100644 --- a/db/models.py +++ b/db/models.py @@ -1 +1,34 @@ 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) + created_at = models.DateTimeField(auto_now_add=True) diff --git a/main.py b/main.py index e71f55d8..a75e2ce7 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,46 @@ +import json import init_django_orm # noqa: F401 from db.models import Race, Skill, Player, Guild def main() -> None: - pass + with open("players.json", "r") as file: + players = json.load(file) + for player_nickname, player_info in players.items(): + email = player_info.get("email") + bio = player_info.get("bio") + race_ = player_info.get("race") + skills_ = race_.get("skills") + guild_ = player_info.get("guild") + + race = Race.objects.get_or_create( + name=race_.get("name"), + description=race_.get("description") + )[0] + + for skill in skills_: + Skill.objects.get_or_create( + name=skill.get("name"), + bonus=skill.get("bonus"), + race_id=race.id + ) + + if guild_: + guild = Guild.objects.get_or_create( + name=guild_.get("name"), + description=guild_.get("description") + )[0] + else: + guild = None + + Player.objects.create( + nickname=player_nickname, + email=email, + bio=bio, + guild=guild, + race_id=race.id + ) if __name__ == "__main__": From 0b86a7394726a4ebe0f8434c038945e83d47f7e2 Mon Sep 17 00:00:00 2001 From: Dmytro Kolodii Date: Tue, 18 Jul 2023 21:18:28 +0300 Subject: [PATCH 2/2] solution 2 --- main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index a75e2ce7..2d189408 100644 --- a/main.py +++ b/main.py @@ -14,10 +14,10 @@ def main() -> None: skills_ = race_.get("skills") guild_ = player_info.get("guild") - race = Race.objects.get_or_create( + race, _ = Race.objects.get_or_create( name=race_.get("name"), description=race_.get("description") - )[0] + ) for skill in skills_: Skill.objects.get_or_create( @@ -27,10 +27,10 @@ def main() -> None: ) if guild_: - guild = Guild.objects.get_or_create( + guild, _ = Guild.objects.get_or_create( name=guild_.get("name"), description=guild_.get("description") - )[0] + ) else: guild = None @@ -39,7 +39,7 @@ def main() -> None: email=email, bio=bio, guild=guild, - race_id=race.id + race=race )