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

Open
wants to merge 1 commit 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
42 changes: 42 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
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 self.name


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)

def __str__(self) -> str:
return self.name


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

def __str__(self) -> str:
return self.name


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,
blank=True
)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self) -> str:
return self.nickname
37 changes: 35 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
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_name = data["race"]["name"]
race_desc = data["race"]["description"]
race, _ = Race.objects.get_or_create(
name=race_name,
description=race_desc
)

guild = None
if data["guild"]:

Choose a reason for hiding this comment

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

Ensure that data['guild'] is not None or an empty dictionary before accessing data['guild']['name'] and data['guild']['description']. This could raise a KeyError if data['guild'] is None or missing keys.

guild_name = data["guild"]["name"]
guild_desc = data["guild"]["description"]
guild, _ = Guild.objects.get_or_create(
name=guild_name,
description=guild_desc
)

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

Player.objects.create(

Choose a reason for hiding this comment

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

Consider using Player.objects.update_or_create() instead of Player.objects.create() to avoid creating duplicate players if the script is run multiple times with the same data.

nickname=nickname,
email=data["email"],
bio=data["bio"],
race=race,
guild=guild
)


if __name__ == "__main__":
Expand Down
Loading