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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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-03 22:06

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, related_name='skill_set', to='db.race')),

Choose a reason for hiding this comment

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

The ForeignKey reference 'to='db.race'' seems incorrect. The 'to' argument should be in the format 'app_label.ModelName'. If 'db' is not the app label, this should be corrected to 'app_label.Race'.

Copy link
Author

Choose a reason for hiding this comment

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

This is a module with migrations, they are created automatically

Choose a reason for hiding this comment

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

The ForeignKey for the 'race' field in the 'Skill' model references 'db.race'. Ensure that the app label 'db' is correct. Typically, it should be the name of the app where the 'Race' model is defined.

],
),
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.DateTimeField(auto_now_add=True)),
('guild', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='player_guild', to='db.guild')),

Choose a reason for hiding this comment

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

The ForeignKey reference 'to='db.guild'' seems incorrect. The 'to' argument should be in the format 'app_label.ModelName'. If 'db' is not the app label, this should be corrected to 'app_label.Guild'.

Copy link
Author

Choose a reason for hiding this comment

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

This is a module with migrations, they are created automatically

Choose a reason for hiding this comment

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

The ForeignKey for the 'guild' field in the 'Player' model references 'db.guild'. Ensure that the app label 'db' is correct. Typically, it should be the name of the app where the 'Guild' model is defined.

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

Choose a reason for hiding this comment

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

The ForeignKey reference 'to='db.race'' seems incorrect. The 'to' argument should be in the format 'app_label.ModelName'. If 'db' is not the app label, this should be corrected to 'app_label.Race'.

Copy link
Author

Choose a reason for hiding this comment

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

This is a module with migrations, they are created automatically

Choose a reason for hiding this comment

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

The ForeignKey for the 'race' field in the 'Player' model references 'db.race'. Ensure that the app label 'db' is correct. Typically, it should be the name of the app where the 'Race' model is defined.

],
),
]
18 changes: 18 additions & 0 deletions db/migrations/0002_alter_race_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.2 on 2024-11-03 22:16

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AlterField(
model_name='race',
name='name',
field=models.CharField(choices=[('Elf', 'Elf race'), ('Dwarf', 'Dwarf race'), ('Human', 'Human race'), ('Ork', 'Ork race')], max_length=255, unique=True),
),
]
39 changes: 39 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
from django.db import models
from django.db.models import SET_NULL


class Race(models.Model):
RACE_TYPE = (
("Elf", "Elf race"),
("Dwarf", "Dwarf race"),
("Human", "Human race"),
("Ork", "Ork race"),
)
name = models.CharField(max_length=255, unique=True, choices=RACE_TYPE)
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,
related_name="skill_set")
YaroslavBordovoy marked this conversation as resolved.
Show resolved Hide resolved


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,
related_name="player_race")
YaroslavBordovoy marked this conversation as resolved.
Show resolved Hide resolved
guild = models.ForeignKey(Guild,
on_delete=SET_NULL,
null=True,
related_name="player_guild")
created_at = models.DateTimeField(auto_now_add=True)
45 changes: 43 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
import init_django_orm # noqa: F401
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 source_file:
players = json.load(source_file)

for player, data in players.items():
# player data
nickname = player
email = data["email"]
biography = data["bio"]
YaroslavBordovoy marked this conversation as resolved.
Show resolved Hide resolved
# race data
race_name = data["race"]["name"]
race_description = data["race"]["description"]
# skill data
skills = data["race"]["skills"]
# guild data
guild_data = data.get("guild")

race, _ = Race.objects.get_or_create(
name=race_name,
description=race_description
)

for skill in skills:
current_skill, _ = Skill.objects.get_or_create(
name=skill["name"],
bonus=skill["bonus"],
race=race
)

if guild_data:
guild, _ = Guild.objects.get_or_create(
name=guild_data.get("name"),
description=guild_data.get("description")
)

Player.objects.get_or_create(
nickname=nickname,
email=email,
bio=biography,
race=race,
guild=guild if guild_data else None
)

Choose a reason for hiding this comment

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

The variable 'guild' might not be defined if 'guild_data' is None. Ensure 'guild' is initialized to None before this line to avoid a NameError.

Copy link
Author

Choose a reason for hiding this comment

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

There will be no NameError since I do the check before assigning

Choose a reason for hiding this comment

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

The variable 'guild' is used here, but it is only defined within the 'if guild_data:' block. If 'guild_data' is None, 'guild' will not be defined, leading to a potential UnboundLocalError. Consider initializing 'guild' to None before the 'if guild_data:' block to avoid this issue.



if __name__ == "__main__":
Expand Down
Loading