X-Git-Url: https://git.yukkurigames.com/?p=mlpccg-meta.git;a=blobdiff_plain;f=meta%2Fmanagement%2Fcommands%2Fcsvimport.py;fp=meta%2Fmanagement%2Fcommands%2Fcsvimport.py;h=0e91c82034a7289ac1b27ee93fec3451f5f76b91;hp=0000000000000000000000000000000000000000;hb=f5f267a53e90455937409ad2bd15324e717c8e04;hpb=5d20db255bfeff5d47c0a7fd9aef9839f57f6d77 diff --git a/meta/management/commands/csvimport.py b/meta/management/commands/csvimport.py new file mode 100644 index 0000000..0e91c82 --- /dev/null +++ b/meta/management/commands/csvimport.py @@ -0,0 +1,24 @@ +from django.core.management.base import BaseCommand, CommandError +from meta.models import DeckListModel, TournamentModel, RecordModel +from datetime import datetime + +import csv + +class Command(BaseCommand): + args = '' + help = 'Import tournament data from CSV' + + def handle(self, *args, **options): + with open('data/tournaments.csv') as f: + for row in csv.DictReader(f, delimiter=','): + try: + tournament = TournamentModel.objects.get(name__iexact=row['name']) + except TournamentModel.DoesNotExist: + tournament = TournamentModel(name=row['name'], date=datetime.strptime(row['date'], '%Y-%m-%d')) + tournament.save() + + decklist = DeckListModel(name=row['decklist_name'], url=row['decklist_url']) + decklist.save() + + record = RecordModel(placement=int(row['placement']), tournament=tournament, decklist=decklist, verified=True) + record.save()