From a886322bc86528ee35ca50fa6d3eb7df3f178c10 Mon Sep 17 00:00:00 2001 From: Jonathan Weth Date: Mon, 17 Jun 2024 20:32:59 +0200 Subject: [PATCH] [JSON import] Add management command for import --- evap/staff/management/commands/json_import.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 evap/staff/management/commands/json_import.py diff --git a/evap/staff/management/commands/json_import.py b/evap/staff/management/commands/json_import.py new file mode 100644 index 0000000000..24ccec5384 --- /dev/null +++ b/evap/staff/management/commands/json_import.py @@ -0,0 +1,34 @@ +import json +import logging + +from django.core.management.base import BaseCommand + +from evap.evaluation.management.commands.tools import log_exceptions +from evap.evaluation.models import Semester +from evap.staff.importers.json import JSONImporter + +logger = logging.getLogger(__name__) + + +@log_exceptions +class Command(BaseCommand): + help = "Import enrollments from JSON file." + + def add_arguments(self, parser): + # Positional arguments + parser.add_argument("semester", type=int) + parser.add_argument("file", type=str) + + def handle(self, *args, **options): + print(args, options) + try: + semester = Semester.objects.get(pk=options["semester"]) + except Semester.DoesNotExist: + self.stdout.write(self.style.ERROR("Semester does not exist.")) + return + + with open(options["file"]) as file: + + import_dict = json.load(file) + importer = JSONImporter(semester) + importer.import_json(import_dict)