From 1488b16a1be990890628e312edbaf4afbdb64398 Mon Sep 17 00:00:00 2001 From: David Morris Date: Wed, 8 Nov 2023 08:36:41 +0000 Subject: [PATCH] Adding soldiers --- cmp/models.py | 2 +- railway.json | 2 +- scripts/delete-all-soldiers.py | 5 +++++ scripts/insert-all-soldiers.py | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 scripts/delete-all-soldiers.py create mode 100644 scripts/insert-all-soldiers.py diff --git a/cmp/models.py b/cmp/models.py index c461441..4498943 100644 --- a/cmp/models.py +++ b/cmp/models.py @@ -52,7 +52,7 @@ class Soldier(models.Model): surname = models.CharField(max_length=255, unique=False, default='') initials = models.CharField(max_length=255, unique=False, default='') army_number = models.CharField(max_length=255, unique=False, default='') - rank = models.ForeignKey('Rank', on_delete=models.CASCADE) + rank = models.ForeignKey('Rank', on_delete=models.CASCADE, related_name='ranks') notes = models.CharField(max_length=255, unique=False, default='') def __str__(self): diff --git a/railway.json b/railway.json index 391c50f..b6db447 100644 --- a/railway.json +++ b/railway.json @@ -4,7 +4,7 @@ "builder": "NIXPACKS" }, "deploy": { - "startCommand": "python manage.py makemigrations && python manage.py migrate && python manage.py runscript delete-all-countries && python manage.py runscript insert-all-countries && python manage.py runscript add-flags-to-countries && python manage.py runscript insert-all-pow-camps && python manage.py runscript insert-all-decorations && python manage.py runscript insert-all-companies && python manage.py collectstatic && python manage.py runserver 0.0.0.0:$PORT", + "startCommand": "python manage.py makemigrations && python manage.py migrate && python manage.py runscript delete-all-countries && python manage.py runscript insert-all-countries && python manage.py runscript add-flags-to-countries && python manage.py runscript insert-all-pow-camps && python manage.py runscript insert-all-decorations && python manage.py runscript insert-all-companies python manage.py runscript insert-all-soldiers && python manage.py collectstatic && python manage.py runserver 0.0.0.0:$PORT", "restartPolicyType": "ON_FAILURE", "restartPolicyMaxRetries": 10 } diff --git a/scripts/delete-all-soldiers.py b/scripts/delete-all-soldiers.py new file mode 100644 index 0000000..0b91098 --- /dev/null +++ b/scripts/delete-all-soldiers.py @@ -0,0 +1,5 @@ +from cmp.models import Soldier + +def run(): + Ranks = Soldier.objects.all() + Ranks.delete() diff --git a/scripts/insert-all-soldiers.py b/scripts/insert-all-soldiers.py new file mode 100644 index 0000000..cdd79c4 --- /dev/null +++ b/scripts/insert-all-soldiers.py @@ -0,0 +1,33 @@ + +def run(): + + import sys + import urllib3 + import csv + from cmp.models import Soldier + + ref_data_url = "https://raw.githubusercontent.com/gm3dmo/old-cmp/main/data/soldier.csv" + http = urllib3.PoolManager() + r = http.request('GET', ref_data_url) + print(r.status) + # load the response into a csv dictionary reader + reader = csv.DictReader(r.data.decode('utf-8').splitlines()) + # breakpoint() + print(reader) + # print(reader.fieldnames) + for row in reader: + # id,surname,initials,army_number,rank_id,notes + print(f"""{row['id']} {row['surname']}""") + try: + Soldier.objects.create( + id = row['id'], + surname = row['surname'], + initials = row['initials'], + army_number = row['army_number'], + rank_id = row['rank_id'], + notes = row['notes'] + ) + except Exception as e: + print("Error with: " + row['surname']) + + raise e