<commit_before>from optparse import make_option
from json import dump

from django.core.management.base import BaseCommand, CommandError

from student.models import TestCenterRegistration


class Command(BaseCommand):

    args = '<output JSON file>'
    help = """
    Dump information as JSON from TestCenterRegistration tables, including username and status.
    """

    option_list = BaseCommand.option_list + (
        make_option('--course_id',
                    action='store',
                    dest='course_id',
                    help='Specify a particular course.'),
        make_option('--exam_series_code',
                    action='store',
                    dest='exam_series_code',
                    default=None,
                    help='Specify a particular exam, using the Pearson code'),
        make_option('--accommodation_pending',
                    action='store_true',
                    dest='accommodation_pending',
                    default=False,
                    ),
    )

    def handle(self, *args, **options):
        if len(args) < 1:
            raise CommandError("Missing single argument: output JSON file")

        # get output location:            
        outputfile = args[0]
        
        # construct the query object to dump:
        registrations = TestCenterRegistration.objects.all()
        if 'course_id' in options and options['course_id']:
            registrations = registrations.filter(course_id=options['course_id'])
        if 'exam_series_code' in options and options['exam_series_code']:
            registrations = registrations.filter(exam_series_code=options['exam_series_code'])

        # collect output:        
        output = []
        for registration in registrations:
            if 'accommodation_pending' in options and options['accommodation_pending'] and not registration.accommodation_is_pending:
                continue
            record = {'username' : registration.testcenter_user.user.username,
                      'email' : registration.testcenter_user.email,
                      'first_name' : registration.testcenter_user.first_name,
                      'last_name' : registration.testcenter_user.last_name,
                      'client_candidate_id' : registration.client_candidate_id,
                      'client_authorization_id' : registration.client_authorization_id,
                      'course_id' : registration.course_id,
                      'exam_series_code' : registration.exam_series_code,
                      'accommodation_request' : registration.accommodation_request,
                      'accommodation_code' : registration.accommodation_code,
                      'registration_status' : registration.registration_status(),
                      'demographics_status' : registration.demographics_status(),
                      'accommodation_status' : registration.accommodation_status(),
                      }
            if len(registration.upload_error_message) > 0:
                record['registration_error'] = registration.upload_error_message
            if registration.needs_uploading:
                record['needs_uploading'] = True

            output.append(record)
            
        # dump output:
        with open(outputfile, 'w') as outfile:
            dump(output, outfile)
<commit_msg>pearson_dump: prettify, add missing error, provide default filename<commit_after>from optparse import make_option
from json import dump
from datetime import datetime

from django.core.management.base import BaseCommand, CommandError

from student.models import TestCenterRegistration


class Command(BaseCommand):

    args = '<output JSON file>'
    help = """
    Dump information as JSON from TestCenterRegistration tables, including username and status.
    """

    option_list = BaseCommand.option_list + (
        make_option('--course_id',
                    action='store',
                    dest='course_id',
                    help='Specify a particular course.'),
        make_option('--exam_series_code',
                    action='store',
                    dest='exam_series_code',
                    default=None,
                    help='Specify a particular exam, using the Pearson code'),
        make_option('--accommodation_pending',
                    action='store_true',
                    dest='accommodation_pending',
                    default=False,
                    ),
    )

    def handle(self, *args, **options):
        if len(args) < 1:
            outputfile = datetime.utcnow().strftime("pearson-dump-%Y%m%d-%H%M%S.json")
        else:
            outputfile = args[0]
        
        # construct the query object to dump:
        registrations = TestCenterRegistration.objects.all()
        if 'course_id' in options and options['course_id']:
            registrations = registrations.filter(course_id=options['course_id'])
        if 'exam_series_code' in options and options['exam_series_code']:
            registrations = registrations.filter(exam_series_code=options['exam_series_code'])

        # collect output:        
        output = []
        for registration in registrations:
            if 'accommodation_pending' in options and options['accommodation_pending'] and not registration.accommodation_is_pending:
                continue
            record = {'username' : registration.testcenter_user.user.username,
                      'email' : registration.testcenter_user.email,
                      'first_name' : registration.testcenter_user.first_name,
                      'last_name' : registration.testcenter_user.last_name,
                      'client_candidate_id' : registration.client_candidate_id,
                      'client_authorization_id' : registration.client_authorization_id,
                      'course_id' : registration.course_id,
                      'exam_series_code' : registration.exam_series_code,
                      'accommodation_request' : registration.accommodation_request,
                      'accommodation_code' : registration.accommodation_code,
                      'registration_status' : registration.registration_status(),
                      'demographics_status' : registration.demographics_status(),
                      'accommodation_status' : registration.accommodation_status(),
                      }
            if len(registration.upload_error_message) > 0:
                record['registration_error'] = registration.upload_error_message
            if len(registration.testcenter_user.upload_error_message) > 0:
                record['demographics_error'] = registration.testcenter_user.upload_error_message
            if registration.needs_uploading:
                record['needs_uploading'] = True

            output.append(record)
            
        # dump output:
        with open(outputfile, 'w') as outfile:
            dump(output, outfile, indent=2)

{'fraction_non_alphanumeric': 0.08372827804107424, 'fraction_numerical': 0.0011489300588826655, 'mean_word_length': 2.0896184560780835, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3400931', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1680, 'n_words': 342}