-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): Add import-character-codes command
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
backend/dpt_app/trails/management/commands/import-character-codes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import argparse | ||
import csv | ||
from django.core.management.base import BaseCommand, CommandError, CommandParser | ||
from typing import Any, Optional | ||
|
||
from trails.enums import ActionType | ||
from trails.models import Character | ||
from trails.qr_models import Code, Action | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser: CommandParser) -> None: | ||
parser.add_argument('csv', help='The CSV file', | ||
type=argparse.FileType('r')) | ||
|
||
def handle(self, *args: Any, **options: Any) -> Optional[str]: | ||
data = csv.reader(options['csv']) | ||
|
||
# first row is the header | ||
header = next(data) | ||
|
||
if header != ['uuid', 'one_shot', 'name', 'character']: | ||
raise CommandError('Header is not correct') | ||
|
||
for row in data: | ||
# convert rows lists to dicts | ||
row = dict(zip(header, row)) | ||
|
||
code, created = Code.objects.update_or_create( | ||
uuid=row['uuid'], | ||
defaults={ | ||
'uuid': row['uuid'], | ||
'one_shot': row['one_shot'] == '1', | ||
'name': row['name'] | ||
} | ||
) | ||
|
||
character, created = Character.objects.update_or_create( | ||
character_class=row['character'] | ||
) | ||
|
||
Action.objects.update_or_create( | ||
code=code, | ||
defaults={ | ||
'action_type': ActionType.CHARACTER, | ||
'character': character | ||
} | ||
) |
File renamed without changes.