Skip to content

Commit

Permalink
feat(backend): Add cli command to import parameter qr codes from CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
barnslig committed Jun 4, 2022
1 parent 6af2bdb commit 7996642
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Empty file.
Empty file.
44 changes: 44 additions & 0 deletions backend/dpt_app/trails/management/commands/import-codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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.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', 'parameter', 'parameter_value']:
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']
}
)

Action.objects.update_or_create(
code=code,
defaults={
'action_type': ActionType.PARAMETER,
'parameter': row['parameter'],
'value': int(row['parameter_value'])
}
)

0 comments on commit 7996642

Please sign in to comment.