Ejemplo n.º 1
0
class CodeSubmission(Submission):
    tokens = JSONField()
    results = JSONField()

    show_answer = False
    show_detail = True

    @property
    def is_compile_error(self):
        for result in self.results:
            if result['status']['id'] != 6:
                return False
        return True

    @property
    def status(self):
        if self.in_progress:
            self.calculate_grade()
        return super().status

    @property
    def in_progress(self):
        for result in self.results:
            if result['status']['id'] == 1 or result['status']['id'] == 2:
                return True
        return False

    def submit(self):
        self.question.grader.submit(self)
class CodeSubmission(Submission):
    tokens = JSONField()
    results = JSONField()

    show_answer = False
    show_detail = True

    @property
    def is_compile_error(self):
        for result in self.results:
            if result['status']['id'] != 6:
                return False
        return True

    @property
    def status(self):
        if self.in_progress:
            self.calculate_grade()
        return super().status

    @property
    def in_progress(self):
        for result in self.results:
            if result['status']['id'] == 1 or result['status']['id'] == 2:
                return True
        return False

    def submit(self):
        self.question.grader.submit(self)

    def get_decoded_stderr(self):
        return base64.b64decode(self.results[0]['stderr']
                                or "").decode('utf-8')

    def get_decoded_results(self):
        stdout = base64.b64decode(self.results[0]['stdout']
                                  or "").decode('utf-8')
        return parse_junit_xml(stdout)

    def get_formatted_test_results(self):
        return str(len(self.get_passed_test_results())) + "/" + str(
            self.get_num_tests())

    def get_passed_test_results(self):
        all_tests = self.get_decoded_results()
        return list(filter(lambda test: test["status"] == "PASS", all_tests))

    def get_failed_test_results(self):
        all_tests = self.get_decoded_results()
        return list(filter(lambda test: test["status"] == "FAIL", all_tests))

    def get_num_tests(self):
        return len(self.get_decoded_results())

    def get_answer_files(self):
        raise NotImplementedError()

    def get_embed_files(self):
        raise NotImplementedError()
class JavaQuestion(VariableQuestion):
    input_files = JSONField()
    """
        [{
            "name": string
            "compile": boolean // whether to compile this input as a separate file or not
            "template": string
        }...]
    """
    junit_template = models.TextField()

    grader = JunitGrader()

    def should_compile(self, file_name):
        for input_file in self.input_files:
            if input_file['name'] == file_name:
                return input_file['compile']
        return False

    def get_input_file_names(self):
        input_files = [x['name'] for x in self.input_files if x['compile']]
        return " ".join(input_files)

    def get_input_files(self):
        return self.input_files
class MultipleChoiceQuestion(VariableQuestion):
    choices = JSONField()
    visible_distractor_count = models.IntegerField()
    grader = MultipleChoiceGrader()

    @property
    def is_checkbox(self):
        return ',' in self.answer
class ParsonsQuestion(VariableQuestion):
    lines = JSONField(default='[]')
    junit_template = models.TextField()
    additional_file_name = models.CharField(max_length=100,
                                            null=True,
                                            blank=True,
                                            default=None)

    grader = JunitGrader()
class JavaSubmission(CodeSubmission):
    answer_files = JSONField()
    """
        {
            file_name: string
            ...
        }
    """
    def get_answer_files(self):
        return {
            file_name: code
            for file_name, code in self.answer_files.items()
            if self.question.should_compile(file_name)
        }

    def get_embed_files(self):
        return self.answer_files
class VariableQuestion(Question):
    variables = JSONField()
Ejemplo n.º 8
0
class MultipleChoiceQuestion(VariableQuestion):
    choices = JSONField()
    visible_distractor_count = models.IntegerField()

    grader = MultipleChoiceGrader()