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

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
51 changes: 51 additions & 0 deletions db/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
18 changes: 18 additions & 0 deletions db/migrations/0002_alter_player_created_at.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
20 changes: 20 additions & 0 deletions db/migrations/0003_player_race.py
Original file line number Diff line number Diff line change
@@ -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,
),
]
33 changes: 33 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -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)
38 changes: 37 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -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")
)

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")
)
else:
guild = None

Player.objects.create(
nickname=player_nickname,
email=email,
bio=bio,
guild=guild,
race=race
)


if __name__ == "__main__":
Expand Down
Loading