Skip to content

Commit

Permalink
Merge pull request #81 from gm3dmo/ranks
Browse files Browse the repository at this point in the history
Adding Ranks model, deleter and loader for #80
  • Loading branch information
gm3dmo authored Oct 23, 2023
2 parents 0b125f7 + 50489f0 commit d99bc8f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class CustomUser(AbstractUser):
def __str__(self):
return self.email

class Rank(models.Model):
RankTypes = (('OR','Other Rank'),('NC','Non Commisioned Officer'),('OF','Officer'))
Name = models.CharField(max_length=50, unique=True)
Abbreviation = models.CharField(max_length=50, blank=True)
Class = models.CharField(max_length=2, blank=True, choices=RankTypes,default='Other Rank')

def __str__(self):
return self.Name


class Country(models.Model):
name_common = models.CharField(max_length=255, unique=False, default='')
Expand Down
5 changes: 5 additions & 0 deletions scripts/delete-all-ranks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from cmp.models import Rank

def run():
Ranks = Rank.objects.all()
Ranks.delete()
38 changes: 38 additions & 0 deletions scripts/insert-all-ranks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

def run():

import urllib3
import csv
from cmp.models import Rank

ref_data_url = "https://raw.githubusercontent.com/gm3dmo/old-cmp/main/data/ranks.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())

# add a country model for each row in the csv file
for row in reader:
print(row['name'])
try:
Rank.objects.create(
id=row['id'],
Name=row['name'],
Abbreviation=row['abbr'],
Class=row['class']
)
except Exception as e:
print("Error with: " + row['name'])
raise e

for rank in Rank.objects.all():
print(f"""{rank.id} {rank.Name}""")








0 comments on commit d99bc8f

Please sign in to comment.