-
Notifications
You must be signed in to change notification settings - Fork 1k
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
base: master
Are you sure you want to change the base?
Solution_MK_v1 #1137
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type of the |
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type of the |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider setting |
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type of the |
||
return (f"{self.nickname}, email: {self.email}, " | ||
f"bio: {self.bio}, created: {self.created_at}") |
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
"bio": player_info["bio"], | ||
"race": race, | ||
"guild": guild | ||
} | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
There was a problem hiding this comment.
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 bestr
instead ofany
. This method is expected to return a string representation of the object.