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_MK_v1 #1137

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
41 changes: 41 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
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) -> any:

Choose a reason for hiding this comment

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

The return type of the __str__ method should be str instead of any. This method is expected to return a string representation of the object.

return f"{self.name}, description: {self.description}"


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")

def __str__(self) -> any:

Choose a reason for hiding this comment

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

The return type of the __str__ method should be str instead of any. This method is expected to return a string representation of the object.

return f"{self.name} (bonus: {self.bonus}), race: {self.race.name}"


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

def __str__(self) -> any:

Choose a reason for hiding this comment

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

The return type of the __str__ method should be str instead of any. This method is expected to return a string representation of the object.

return f"{self.name}, description: {self.description}"


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

Choose a reason for hiding this comment

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

Consider setting unique=True for the email field in the Player model to ensure that each player has a unique email address.

bio = models.CharField(max_length=255, blank=True)
race = models.ForeignKey(Race, on_delete=models.CASCADE,
related_name="players")
guild = models.ForeignKey(Guild, on_delete=models.SET_NULL,
null=True)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self) -> any:

Choose a reason for hiding this comment

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

The return type of the __str__ method should be str instead of any. This method is expected to return a string representation of the object.

return (f"{self.nickname}, email: {self.email}, "
f"bio: {self.bio}, created: {self.created_at}")
42 changes: 41 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
import json
import init_django_orm # noqa: F401

from db.models import Race, Skill, Player, Guild


def main() -> None:
pass
# Load data from the players.json file
with open("players.json", "r") as file:

Choose a reason for hiding this comment

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

Consider adding error handling for file operations. For example, use a try-except block to catch potential FileNotFoundError or json.JSONDecodeError exceptions.

players_data = json.load(file)

for nickname, player_info in players_data.items():
# Get or create Race
race_data = player_info["race"]
race, created_race = Race.objects.get_or_create(
name=race_data["name"],
defaults={"description": race_data["description"]}
)

# Get or create Skills for the Race
for skill_data in race_data["skills"]:
Skill.objects.get_or_create(
name=skill_data["name"],
race=race,
defaults={"bonus": skill_data["bonus"]}
)

# Get or create Guild
guild_data = player_info["guild"]
if guild_data:
guild, created_guild = Guild.objects.get_or_create(
name=guild_data["name"],
defaults={"description": guild_data.get("description")}
)
else:
guild = None

# Get or create Player
Player.objects.get_or_create(

Choose a reason for hiding this comment

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

Consider adding error handling for database operations. This can help manage exceptions that may arise from database constraints or connectivity issues.

nickname=nickname,
defaults={
"email": player_info["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 in the Player model is unique to prevent duplicate entries. This is especially important since the script uses get_or_create, which relies on unique fields to determine if an entry already exists.

"bio": player_info["bio"],
"race": race,
"guild": guild
}
)


if __name__ == "__main__":
Expand Down
Loading