From 3d0df4f16e84e2efef1cca0e7a87d5be1f26666a Mon Sep 17 00:00:00 2001 From: "mark.shapka" Date: Wed, 2 Aug 2023 21:32:43 +0300 Subject: [PATCH 1/4] Solution game-models --- db/migrations/0001_initial.py | 52 +++++++++++++++++++++++++++++++++ db/models.py | 55 +++++++++++++++++++++++++++++++++++ main.py | 47 +++++++++++++++++++++++++++++- 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 db/migrations/0001_initial.py diff --git a/db/migrations/0001_initial.py b/db/migrations/0001_initial.py new file mode 100644 index 00000000..2bf06942 --- /dev/null +++ b/db/migrations/0001_initial.py @@ -0,0 +1,52 @@ +# Generated by Django 4.0.2 on 2023-07-30 11:47 + +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(null=True, on_delete=django.db.models.deletion.SET_NULL, 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')), + ('race', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.race')), + ], + ), + ] diff --git a/db/models.py b/db/models.py index 137941ff..a3c614fd 100644 --- a/db/models.py +++ b/db/models.py @@ -1 +1,56 @@ from django.db import models + + +class Race(models.Model): + name = models.CharField(max_length=255, unique=True) + description = models.TextField(blank=True) + + def __str__(self) -> str: + return ( + f"Race: name = {self.name}, " + f"description = {self.description}" + ) + + +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.SET_NULL, null=True) + + def __str__(self) -> str: + return ( + f"Skill: name = {self.name}, " + f"bonus = {self.bonus}, " + f"race = {self.race}" + ) + + +class Guild(models.Model): + name = models.CharField(max_length=255, unique=True) + description = models.TextField(null=True) + + def __str__(self) -> str: + return ( + f"Guild: name = {self.name}, " + f"description = {self.description}" + ) + + +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.SET_NULL, null=True) + guild = models.ForeignKey(Guild, on_delete=models.SET_NULL, null=True) + created_at = models.DateField(auto_now_add=True) + + def __str__(self) -> str: + return ( + f""" + Player: nickname = {self.nickname}, + email = {self.email}, + bio = {self.bio}, + race = {self.race}, + guild = {self.guild} + """ + ) diff --git a/main.py b/main.py index e71f55d8..90724280 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,55 @@ 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 source_file: + players = json.load(source_file) + + for key, data in players.items(): + new_player = Player( + nickname=key, + email=data.get("email"), + bio=data.get("bio") + ) + + guild = data.get("guild") + if guild is not None: + guild_name = guild.get("name") + if not Guild.objects.filter(name=guild_name).exists(): + Guild.objects.create( + name=guild_name, + description=guild.get("description") + ) + guild_from_db = Guild.objects.get(name=guild_name) + new_player.guild = guild_from_db + + race_ = data.get("race") + race_name = race_.get("name") + if not Race.objects.filter(name=race_name).exists(): + Race.objects.create( + name=race_name, + description=race_.get("description") + ) + race_from_db = Race.objects.get(name=race_name) + + skills = race_.get("skills") + if skills is not None: + for skill in skills: + if not Skill.objects.filter(name=skill.get("name")).exists(): + Skill.objects.create( + name=skill.get("name"), + bonus=skill.get("bonus"), + race=race_from_db + ) + + new_player.race = race_from_db + if not Player.objects.filter(nickname=new_player.nickname).exists(): + new_player.save() + print(new_player) + print("----------------") if __name__ == "__main__": From 75ee150b55c898a3ac8e8fb1866618dd4893b033 Mon Sep 17 00:00:00 2001 From: "mark.shapka" Date: Wed, 2 Aug 2023 21:35:52 +0300 Subject: [PATCH 2/4] Solution game-models --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 90724280..1eb7d487 100644 --- a/main.py +++ b/main.py @@ -49,7 +49,7 @@ def main() -> None: if not Player.objects.filter(nickname=new_player.nickname).exists(): new_player.save() print(new_player) - print("----------------") + print("---------------") if __name__ == "__main__": From 816c859e04fb22395e3bfe33477ea3f8eb9e2ecc Mon Sep 17 00:00:00 2001 From: "mark.shapka" Date: Sat, 12 Aug 2023 22:00:00 +0300 Subject: [PATCH 3/4] added else condition and addition variable --- main.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 1eb7d487..6ffa08f1 100644 --- a/main.py +++ b/main.py @@ -16,24 +16,28 @@ def main() -> None: ) guild = data.get("guild") + guild_from_db = None if guild is not None: guild_name = guild.get("name") if not Guild.objects.filter(name=guild_name).exists(): - Guild.objects.create( + guild_from_db = Guild.objects.create( name=guild_name, description=guild.get("description") ) - guild_from_db = Guild.objects.get(name=guild_name) - new_player.guild = guild_from_db + else: + guild_from_db = Guild.objects.get(name=guild_name) + new_player.guild = guild_from_db race_ = data.get("race") race_name = race_.get("name") + race_from_db = None if not Race.objects.filter(name=race_name).exists(): Race.objects.create( name=race_name, description=race_.get("description") ) - race_from_db = Race.objects.get(name=race_name) + else: + race_from_db = Race.objects.get(name=race_name) skills = race_.get("skills") if skills is not None: @@ -48,8 +52,6 @@ def main() -> None: new_player.race = race_from_db if not Player.objects.filter(nickname=new_player.nickname).exists(): new_player.save() - print(new_player) - print("---------------") if __name__ == "__main__": From a9c3b70e65da0ab8e58348eb166bfe69d0859413 Mon Sep 17 00:00:00 2001 From: "mark.shapka" Date: Sun, 13 Aug 2023 13:43:01 +0300 Subject: [PATCH 4/4] added variable and deleted print() --- main.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 1eb7d487..67ed2eba 100644 --- a/main.py +++ b/main.py @@ -18,22 +18,26 @@ def main() -> None: guild = data.get("guild") if guild is not None: guild_name = guild.get("name") + guild_from_db = None if not Guild.objects.filter(name=guild_name).exists(): - Guild.objects.create( + guild_from_db = Guild.objects.create( name=guild_name, description=guild.get("description") ) - guild_from_db = Guild.objects.get(name=guild_name) + else: + guild_from_db = Guild.objects.get(name=guild_name) new_player.guild = guild_from_db race_ = data.get("race") race_name = race_.get("name") + race_from_db = None if not Race.objects.filter(name=race_name).exists(): - Race.objects.create( + race_from_db = Race.objects.create( name=race_name, description=race_.get("description") ) - race_from_db = Race.objects.get(name=race_name) + else: + race_from_db = Race.objects.get(name=race_name) skills = race_.get("skills") if skills is not None: @@ -48,8 +52,6 @@ def main() -> None: new_player.race = race_from_db if not Player.objects.filter(nickname=new_player.nickname).exists(): new_player.save() - print(new_player) - print("---------------") if __name__ == "__main__":