Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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):
Comment on lines +8 to +9
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #6 violation: The context manager is overloaded. The with block should only handle file reading (line 9), but the entire data processing loop (lines 11-43) is inside it. According to the guidelines, you should read data within the context manager and then process it outside.

name = models.CharField(max_length=255, unique=True)
bonus = models.CharField(max_length=255)
race = models.ForeignKey('Race', on_delete=models.CASCADE, related_name='skills')


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='players')
guild = models.ForeignKey('Guild', on_delete=models.SET_NULL, null=True, blank=True, related_name='players')
created_at = models.DateTimeField(auto_now_add=True)
44 changes: 40 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
import init_django_orm # noqa: F401

from db.models import Race, Skill, Player, Guild
import json
import init_django_orm # noqa: F401
from db.models import Race, Skill, Guild, Player


def main() -> None:
pass

with open("players.json", "r") as f:
player_data = json.load(f)

for nickname, data in player_data.items():

race_data = data["race"]
race, _ = Race.objects.get_or_create(
name=race_data["name"],
defaults={"description": race_data.get("description", "")}
)

for skill_data in race_data.get("skills", []):
Skill.objects.get_or_create(
name=skill_data["name"],
defaults={
"bonus": skill_data["bonus"],
"race": race
}
)

guild = None
guild_data = data.get("guild")
if guild_data:
guild, _ = Guild.objects.get_or_create(
name=guild_data["name"],
defaults={"description": guild_data.get("description")}
)
Player.objects.get_or_create(
nickname=nickname,
defaults={
"email": data["email"],
"bio": data.get("bio", ""),
"race": race,
"guild": guild
}
)


if __name__ == "__main__":
Expand Down
Loading