Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #1028

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions db/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 4.0.2 on 2024-11-04 14:48

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, verbose_name='This field describes what kind of bonus players can get from it.')),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key reference 'to='db.race'' should be 'to='app_name.Race'' where 'app_name' is the name of the Django app containing the Race model. Ensure the correct app name is used.

('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key reference 'to='db.race'' should be 'to='db.Race'' to match the model name 'Race'. Ensure that the model name is correctly referenced in the foreign key.

],
),
migrations.CreateModel(
name='PlayerMode',
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, verbose_name='It stores a short description provided by a user about himself/herself.')),
('created_at', models.DateTimeField(auto_now=True)),
('guild', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='db.guild')),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key reference 'to='db.guild'' should be 'to='app_name.Guild'' where 'app_name' is the name of the Django app containing the Guild model. Ensure the correct app name is used.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key reference 'to='db.guild'' should be 'to='db.Guild'' to match the model name 'Guild'. Ensure that the model name is correctly referenced in the foreign key.

('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key reference 'to='db.race'' should be 'to='app_name.Race'' where 'app_name' is the name of the Django app containing the Race model. Ensure the correct app name is used.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comment, the foreign key reference 'to='db.race'' should be 'to='db.Race'' to match the model name 'Race'.

],
),
]
29 changes: 29 additions & 0 deletions db/migrations/0002_player_delete_playermode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.0.2 on 2024-11-04 17:24

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('db', '0001_initial'),
]

operations = [
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, verbose_name='It stores a short description provided by a user about himself/herself.')),
('created_at', models.DateTimeField(auto_now=True)),
('guild', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.guild')),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key reference 'to='db.guild'' should be 'to='app_name.Guild'' where 'app_name' is the name of the Django app containing the Guild model. Ensure the correct app name is used.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key reference 'to='db.guild'' should be 'to='db.Guild'' to match the model name 'Guild'. Ensure that the model name is correctly referenced in the foreign key.

('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key reference 'to='db.race'' should be 'to='app_name.Race'' where 'app_name' is the name of the Django app containing the Race model. Ensure the correct app name is used.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comment, the foreign key reference 'to='db.race'' should be 'to='db.Race'' to match the model name 'Race'.

],
),
migrations.DeleteModel(
name='PlayerMode',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.0.2 on 2024-11-04 22:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('db', '0002_player_delete_playermode'),
]

operations = [
migrations.AlterField(
model_name='player',
name='bio',
field=models.CharField(max_length=255),
),
migrations.AlterField(
model_name='player',
name='created_at',
field=models.DateTimeField(auto_now_add=True),
),
migrations.AlterField(
model_name='skill',
name='bonus',
field=models.CharField(max_length=255),
),
]
25 changes: 25 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
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, unique=False)
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)
34 changes: 32 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
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_data = json.load(file)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for the JSON loading process. If the file is not found or the JSON is malformed, it could cause the program to crash.


for nickname, player_data in players_data.items():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good practice to validate the structure and content of 'players_data' before processing it. This can prevent runtime errors if the JSON structure is not as expected.

race_info = player_data.get("race")
race_player, created = Race.objects.get_or_create(
name=race_info.get("name"),
description=race_info.get("description")
)
skills = race_info.get("skills")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that 'skills' is not None before iterating over it. Consider adding a check like 'if skills:' to avoid potential errors if 'skills' is missing or None.

for skill in skills:
Skill.objects.get_or_create(
name=skill.get("name"),
race=race_player,
bonus=skill.get("bonus"),
)
guild_info = player_data.get("guild")
if guild_info:
guild_player, created = Guild.objects.get_or_create(
name=guild_info.get("name"),
description=guild_info.get("description")
)
else:
guild_player = None
Player.objects.get_or_create(
nickname=nickname,
email=player_data.get("email"),
bio=player_data.get("bio"),
race=race_player,
guild=guild_player

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'guild' field in the Player model is nullable, so it's safe to pass 'guild_player' even if it's None. However, ensure that the JSON data correctly reflects this possibility to avoid unexpected None values.

)


if __name__ == "__main__":
Expand Down
Loading