-
Notifications
You must be signed in to change notification settings - Fork 940
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-py-game-models #1020
base: master
Are you sure you want to change the base?
Solution-py-game-models #1020
Changes from 3 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Generated by Django 4.0.2 on 2024-11-03 01:48 | ||
|
||
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')), | ||
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 ForeignKey reference 'to='db.race'' should be 'to='app_name.Race'' where 'app_name' is the name of the Django app containing the models. Ensure that the app name is correctly specified to avoid import errors. |
||
], | ||
), | ||
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.DateTimeField(auto_now_add=True)), | ||
('guild', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.guild')), | ||
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 'to' parameter in the ForeignKey should match the correct app label. Ensure that 'db.guild' is the correct reference. If 'guild' is in the same app, it should be 'Guild' instead of 'db.guild'. 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 ForeignKey reference 'to='db.guild'' should be 'to='app_name.Guild'' where 'app_name' is the name of the Django app containing the models. Ensure that the app name is correctly specified to avoid import errors. |
||
('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')), | ||
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 'to' parameter in the ForeignKey should match the correct app label. Ensure that 'db.race' is the correct reference. If 'race' is in the same app, it should be 'Race' instead of 'db.race'. 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 ForeignKey reference 'to='db.race'' should be 'to='app_name.Race'' where 'app_name' is the name of the Django app containing the models. Ensure that the app name is correctly specified to avoid import errors. |
||
], | ||
), | ||
] |
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) | ||
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.
Your |
||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,67 @@ | ||
import os | ||
import json | ||
|
||
import init_django_orm # noqa: F401 | ||
|
||
from db.models import Race, Skill, Player, Guild | ||
|
||
|
||
def main() -> None: | ||
pass | ||
players = file_data("players.json") | ||
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 'players.json' file exists in the correct directory, or handle the potential FileNotFoundError exception in the 'file_data' function. |
||
create_db(players) | ||
|
||
|
||
def create_db(players: dict) -> None: | ||
for player, info in players.items(): | ||
email_info = info.get("email") | ||
bio_info = info.get("bio") | ||
|
||
if race_info := info.get("race"): | ||
race_name = race_info.get("name") | ||
race_description = race_info.get("description") | ||
|
||
race, race_created = Race.objects.get_or_create( | ||
name=race_name, | ||
description=race_description | ||
) | ||
|
||
if skills_info := race_info.get("skills"): | ||
for skill in skills_info: | ||
skill_name = skill.get("name") | ||
skill_bonus = skill.get("bonus") | ||
|
||
Skill.objects.get_or_create( | ||
name=skill_name, bonus=skill_bonus, race=race | ||
) | ||
else: | ||
race = None | ||
|
||
if guild_info := info.get("guild"): | ||
guild_name = guild_info.get("name") | ||
guild_description = guild_info.get("description") | ||
|
||
guild, guild_created = Guild.objects.get_or_create( | ||
name=guild_name, | ||
description=guild_description | ||
) | ||
else: | ||
guild = None | ||
|
||
Player.objects.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. You should use |
||
nickname=player, | ||
email=email_info, | ||
bio=bio_info, | ||
race=race, | ||
guild=guild | ||
) | ||
|
||
|
||
def file_data(path: str) -> dict: | ||
file_path = os.path.join(path) | ||
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 use of |
||
with open(file_path) as f: | ||
players = json.load(f) | ||
|
||
return players | ||
|
||
|
||
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 'to' parameter in the ForeignKey should match the correct app label. Ensure that 'db.race' is the correct reference. If 'race' is in the same app, it should be 'Race' instead of 'db.race'.