-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JSON import] Add management command for import
- Loading branch information
1 parent
4914f98
commit a886322
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
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,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) |