2024-01-20

Goal

Django Bulk Upload names into the names Django Model of the Fighters App.

Notes

Need to adjust the script to allow the bulk upload:

from fighters.models import FighterName
import json
from itertools import islice

with open(r"C:\Users\Billy\Documents\HitHub\bulk_uploads\Fighters\fighter_names.json", "r") as f:
    fighter_name_data = json.load(f)



# Build up the objects data
objs = []
for _, name_data in fighter_name_data.items():
    objs.append(FighterName(text = name_data["title"],type = name_data["type"],rarity = name_data["rarity"]))

FighterName.objects.bulk_create(objs)

First I had to access the Django Shell via the command

python manage.py shell

(After activating the Virtual Environment via ./.venv/Scripts/Activate of course).

When I tried running Docker I ran into an issue where numpy was not installed. This was an easy fix because the Dockerfile referenced the requirements.txt file, so all I had to do was

pip freeze > requirements.txt

For some reason this still didn't work, or for whatever reason the dockerfile didn't have access to the requirements.txt file for some reason. I found out I had to rebuild the Docker Container by running

docker-compose build

in order to re-build the environment with the new packages listed in requirements.txt. Now the app is back up and running!

I also identified the issue with my bulk upload script! the rarity feature should be a character (C R U E L) and not an integer. That's what the error message was trying to tell me. No worries though, with a quick mapping adjustment of

val_to_char = {1: "C", 2: "U", 3: "R", 4: "E", 5: "L"}

Which now means I can easily convert the rarity from an Integer to a String. This makes my bulk upload script

from fighters.models import FighterName
import json
from itertools import islice

with open(r"C:\Users\Billy\Documents\HitHub\bulk_uploads\Fighters\fighter_names.json", "r") as f:
    fighter_name_data = json.load(f)

val_to_char = {1: "C", 2: "U", 3: "R", 4: "E", 5: "L"}

# Build up the objects data
objs = []
for _, name_data in fighter_name_data.items():
    objs.append(FighterName(text = name_data["title"],type = name_data["type"],rarity = val_to_char[name_data["rarity"]]))

FighterName.objects.bulk_create(objs)

I consulted with ChatGPT and found that it was the __str__ representation of the FighterName Django Model wasn't quite right. It looks like I was Django Bulk Uploading these names but it wasn't being represented correctly in string format. I'll flush the model and try again. I also changed the name from FighterName to Name; I'll know it's a fighter name because it's referenced within the Fighters App. Either way I ran the following line in the Django Shell:

FighterName.objects.all().delete()

which deleted all of the names from the database. This is a handy feature of the Django QuerySet that I'll have to keep in mind in the future...

Anyways I re-ran the script to Django Bulk Upload the names with the final iteration of the script:

from fighters.models import Name
import json

with open(r"C:\Users\Billy\Documents\HitHub\bulk_uploads\Fighters\fighter_names.json", "r") as f:
    fighter_name_data = json.load(f)

val_to_char = {1: "C", 2: "U", 3: "R", 4: "E", 5: "L"}

# Build up the objects data
objs = []
for _, name_data in fighter_name_data.items():
    objs.append(Name(text = name_data["title"],type = name_data["type"],rarity = val_to_char[name_data["rarity"]]))

Name.objects.bulk_create(objs)

Which added all 5094 names to my database. Nice! Now I had both Names and SpiritFighters accessible in my app as Django Models, meaning I could access them via Django QuerySet objects. This would be essential for generating random fighters.

One mechanic I want to add into the HitHub App is the periodic risk of doing drugs correlated with other traits. A great resource for this might be the Drugs from A-Z site which offers a list of drugs and their side-effects.

Another mechanic is worrying about our Mini Martial Artists getting in trouble with the Law. Stats on Crime in Detroit can offer interesting crime rates to use in the app on a set interval basis (maybe ~weekly?). I spent some time looking at the Crime in Detroit stats and I think that will be very useful for running RNG on an hour by hour basis.

Anyways after getting distracted there I went back to the signals.py file of the Accounts App and was able to easily use the Django QuerySet object to randomly generate a name for the fighter. Nice!

I found out that I can simply import functions in the django shell instead of copying and pasting them which should save me a lot of time in the future (especially with bulk uploads). For instance, when checking whether I can create a random fighter I can simply import the function as

from accounts.signals import create_fighter

in order to test it out. This made it much easier to debug and test out different elements of the name. I was finally able to complete the first part of my fighter generator! I also made sure I could use different functions to set different attributes of the fighters.

Working on DIMMiN

I was thinking about how I haven't posted on my DIMMiN Blog in a while and decided to look into the Blog App. I remembered the CKeditor issue and decided to use ChatGPT to look into that too. Turns out all I needed to do to fix it was re-install ckeditor. Cool!

I also removed the safe flag from { post.body|safe } in the blog post's Django Template templates/post_detail.html. I did this because I know I'm the only one who will be posting so I'm not concerned with unintentionally using an XSS Attack on myself.

Started writing my blog post for Spending Waaaaaaaaaaaay Too Much Time at Bars

Results

  • Added a name generator to the Fighters App
  • Organized the Fighters App to prepare to automatically generate fighters and associate them with the correct information
  • Learned how to use the Django Shell to run functions instead of needing to copy and paste them every time

Next Time

  • Finish working on the determine_spirit_fighter function in the accounts/signals.py for the Fighters App

Previous Note 2024-01-19 Next Note 2024-01-21