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 #1145

Open
wants to merge 5 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 5.1.1 on 2025-01-24 18:04

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


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='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)),

Choose a reason for hiding this comment

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

Consider making the email field unique to avoid duplicate entries. This can be done by adding unique=True to the EmailField.

('bio', models.CharField(max_length=255)),
('created_at', models.DateTimeField(auto_now_add=True)),
('guild', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='db.guild')),
('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')),
],
),
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)),

Choose a reason for hiding this comment

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

The bonus field is currently a CharField. If this field is meant to store numerical values, consider using an IntegerField or FloatField instead.

('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.1.1 on 2025-01-24 18:31

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


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.DO_NOTHING, related_name='members', to='db.guild'),
),
migrations.AlterField(
model_name='player',
name='race',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='players', to='db.race'),
),
migrations.AlterField(
model_name='skill',
name='race',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='skills', to='db.race'),
),
]
19 changes: 19 additions & 0 deletions db/migrations/0003_alter_player_guild.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.1.1 on 2025-01-24 18:57

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


class Migration(migrations.Migration):

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

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='members', to='db.guild'),
),
]
19 changes: 19 additions & 0 deletions db/migrations/0004_alter_skill_race.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.1.1 on 2025-01-24 18:58

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


class Migration(migrations.Migration):

dependencies = [
('db', '0003_alter_player_guild'),
]

operations = [
migrations.AlterField(
model_name='skill',
name='race',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race'),
),
]
32 changes: 32 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
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)

Choose a reason for hiding this comment

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

The bonus field is defined as a CharField. If this field is intended to store numerical values, consider using an IntegerField or FloatField instead.

Choose a reason for hiding this comment

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

The bonus field is currently a CharField. If this field is meant to store numerical values, consider using an IntegerField or FloatField instead.

race = models.ForeignKey(Race,
on_delete=models.CASCADE)


class Guild(models.Model):
name = models.CharField(max_length=255, unique=True)
description = models.TextField(blank=True, null=True)


class Player(models.Model):
nickname = models.CharField(max_length=255, unique=True)
email = models.EmailField(max_length=255)

Choose a reason for hiding this comment

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

Consider adding unique=True to the email field to ensure that each player has a unique email address.

Choose a reason for hiding this comment

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

Consider making the email field unique to avoid duplicate entries. This can be done by adding unique=True to the EmailField.

bio = models.CharField(max_length=255)
race = models.ForeignKey(Race,
on_delete=models.CASCADE,
related_name="players")
guild = models.ForeignKey(Guild,
on_delete=models.SET_NULL,
related_name="members",
null=True,
blank=True)
created_at = models.DateTimeField(auto_now_add=True)
35 changes: 33 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
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:
player_data = json.load(file)

for nickname, data in player_data.items():
race, created_race = Race.objects.get_or_create(
name=data["race"]["name"],
description=data["race"]["description"],
)

for skills_data in data["race"]["skills"]:
Skill.objects.get_or_create(
name=skills_data["name"],
bonus=skills_data["bonus"],
race=race,
)

if data["guild"]:
guild, created_guild = Guild.objects.get_or_create(
name=data["guild"]["name"],
description=data["guild"].get("description", "")

Choose a reason for hiding this comment

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

When using get_or_create, ensure that the default value for description is consistent with the model definition. In Guild, description allows null=True, so consider using None instead of an empty string if the description is not provided.

Choose a reason for hiding this comment

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

Consider using None instead of an empty string for description if null=True is allowed in the Guild model.

)
else:
guild = None

player, created = Player.objects.get_or_create(
nickname=nickname,
email=data.get("email"),

Choose a reason for hiding this comment

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

Ensure that the email field is always provided. If the email is missing in the JSON data, this could lead to an error since the email field in the Player model does not allow null values.

Choose a reason for hiding this comment

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

Ensure that the email field is always provided in the JSON data to prevent errors. Consider adding a check or default value if necessary.

bio=data.get("bio"),
race=race,
guild=guild,
)


if __name__ == "__main__":
Expand Down
Loading