From 6f0cb0dd881c186f18b5ea38940a547d14243794 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Mon, 16 Dec 2019 15:24:46 +0100 Subject: [PATCH] Add command for exporting assignments --- .../management/commands/export_assignments.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 server/core/management/commands/export_assignments.py diff --git a/server/core/management/commands/export_assignments.py b/server/core/management/commands/export_assignments.py new file mode 100644 index 00000000..1015e6a6 --- /dev/null +++ b/server/core/management/commands/export_assignments.py @@ -0,0 +1,29 @@ +from django.core.management import BaseCommand + +from assignments.models import Assignment, StudentSubmission +import random +import json +from django.db.models import Q + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument('jsonfile', type=str) + + def handle(self, *args, **options): + jsonfile = options['jsonfile'] + self.stdout.write("Exporting assignments") + assignment_list = [] + for assignment in Assignment.objects.filter(user_created=False): + assignment_dict = { + "title": assignment.title, + "id": assignment.id, + "text": assignment.assignment, + } + submissions = assignment.submissions.filter(text__isnull=False).filter(~Q(text='')) + if submissions.count() > 0: + example = random.choice(submissions) + assignment_dict['example'] = example.text + assignment_list.append(assignment_dict) + + with open(jsonfile, 'w') as f: + f.write(json.dumps(assignment_list, indent=2, sort_keys=True))