-
Notifications
You must be signed in to change notification settings - Fork 918
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 game-models #439
base: master
Are you sure you want to change the base?
Solution game-models #439
Changes from 2 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 2023-07-30 11:47 | ||
|
||
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(null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.race')), | ||
], | ||
), | ||
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.DateField(auto_now_add=True)), | ||
('guild', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.guild')), | ||
('race', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.race')), | ||
], | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,56 @@ | ||
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 ( | ||
f"Race: name = {self.name}, " | ||
f"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.SET_NULL, null=True) | ||
|
||
def __str__(self) -> str: | ||
return ( | ||
f"Skill: name = {self.name}, " | ||
f"bonus = {self.bonus}, " | ||
f"race = {self.race}" | ||
) | ||
|
||
|
||
class Guild(models.Model): | ||
name = models.CharField(max_length=255, unique=True) | ||
description = models.TextField(null=True) | ||
|
||
def __str__(self) -> str: | ||
return ( | ||
f"Guild: name = {self.name}, " | ||
f"description = {self.description}" | ||
) | ||
|
||
|
||
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.SET_NULL, null=True) | ||
guild = models.ForeignKey(Guild, on_delete=models.SET_NULL, null=True) | ||
created_at = models.DateField(auto_now_add=True) | ||
|
||
def __str__(self) -> str: | ||
return ( | ||
f""" | ||
Player: nickname = {self.nickname}, | ||
email = {self.email}, | ||
bio = {self.bio}, | ||
race = {self.race}, | ||
guild = {self.guild} | ||
""" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,55 @@ | ||
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 source_file: | ||
players = json.load(source_file) | ||
|
||
for key, data in players.items(): | ||
new_player = Player( | ||
nickname=key, | ||
email=data.get("email"), | ||
bio=data.get("bio") | ||
) | ||
|
||
guild = data.get("guild") | ||
if guild is not None: | ||
guild_name = guild.get("name") | ||
if not Guild.objects.filter(name=guild_name).exists(): | ||
Guild.objects.create( | ||
name=guild_name, | ||
description=guild.get("description") | ||
) | ||
guild_from_db = Guild.objects.get(name=guild_name) | ||
new_player.guild = guild_from_db | ||
|
||
race_ = data.get("race") | ||
race_name = race_.get("name") | ||
if not Race.objects.filter(name=race_name).exists(): | ||
Race.objects.create( | ||
name=race_name, | ||
description=race_.get("description") | ||
) | ||
race_from_db = Race.objects.get(name=race_name) | ||
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. There is no need to query the database additionally in case you create a record, |
||
|
||
skills = race_.get("skills") | ||
if skills is not None: | ||
for skill in skills: | ||
if not Skill.objects.filter(name=skill.get("name")).exists(): | ||
Skill.objects.create( | ||
name=skill.get("name"), | ||
bonus=skill.get("bonus"), | ||
race=race_from_db | ||
) | ||
|
||
new_player.race = race_from_db | ||
if not Player.objects.filter(nickname=new_player.nickname).exists(): | ||
new_player.save() | ||
print(new_player) | ||
print("---------------") | ||
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. Delete these prints, they are not needed. |
||
|
||
|
||
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.
There is no need to query the database additionally in case you create a record,
.create()
will return the required object. You need to write it to a variable. Execute.get()
in theelse
statement to achieve that.