def _multiple_choice_question_edit_view(request, question):
    correct_answer_formset_class = formset_factory(ChoiceForm, extra=1, can_delete=True, max_num=1, min_num=1)
    distractor_answer_formset_class = formset_factory(ChoiceForm, extra=0, can_delete=True)

    if request.method == 'POST':
        correct_answer_formset = correct_answer_formset_class(request.POST, prefix='correct')
        distractor_answer_formset = distractor_answer_formset_class(request.POST, prefix='distractor')
        form = MultipleChoiceQuestionForm(request.user, request.POST)

        if correct_answer_formset.is_valid() and distractor_answer_formset.is_valid() and form.is_valid():
            try:
                create_multiple_choice_question(
                    pk=question.pk,
                    title=form.cleaned_data['title'],
                    text=form.cleaned_data['text'],
                    max_submission_allowed=question.max_submission_allowed,
                    author=question.author,
                    category=form.cleaned_data['category'],
                    difficulty=form.cleaned_data['difficulty'],
                    is_verified=question.is_verified,
                    variables=form.cleaned_data['variables'],
                    visible_distractor_count=form.cleaned_data['visible_distractor_count'],
                    answer_text=correct_answer_formset.forms[0].cleaned_data['text'],
                    distractors=[form.cleaned_data['text'] for form in distractor_answer_formset.forms if
                                 not form.cleaned_data['DELETE']],
                    course=form.cleaned_data['course'],
                    event=form.cleaned_data['event'],
                )
                messages.add_message(request, messages.SUCCESS, 'Problem saved successfully')
            except QuestionCreateException as e:
                messages.add_message(request, messages.ERROR, e.user_message)

    else:
        form = MultipleChoiceQuestionForm(request.user, instance=question)

        correct_answer_formset = correct_answer_formset_class(
            prefix='correct',
            initial=[{'text': question.choices[question.answer]}]
        )
        distractor_answer_formset = distractor_answer_formset_class(
            prefix='distractor',
            initial=[{'text': value} for name, value in question.choices.items() if name != question.answer]
        )

    return render(request, 'problem_create.html', {
        'form': form,
        'correct_answer_formset': correct_answer_formset,
        'distractor_answer_formset': distractor_answer_formset,
        'header': 'Edit Question',
    })
def _multiple_choice_question_create_view(request, header, question_form_class, correct_answer_formset_class,
                                          distractor_answer_formset_class):
    if request.method == 'POST':
        correct_answer_formset = correct_answer_formset_class(request.POST, prefix='correct')
        distractor_answer_formset = distractor_answer_formset_class(request.POST, prefix='distractor')
        form = question_form_class(request.user, request.POST)

        if correct_answer_formset.is_valid() and distractor_answer_formset.is_valid() and form.is_valid():

            try:
                create_multiple_choice_question(
                    title=form.cleaned_data['title'],
                    text=form.cleaned_data['text'],
                    author=request.user,
                    category=form.cleaned_data['category'],
                    difficulty=form.cleaned_data['difficulty'],
                    variables=form.cleaned_data['variables'],
                    visible_distractor_count=form.cleaned_data['visible_distractor_count'],
                    answer_text=correct_answer_formset.forms[0].cleaned_data['text'],
                    distractors=[form.cleaned_data['text'] for form in distractor_answer_formset.forms if
                                 not form.cleaned_data['DELETE']],
                    course=form.cleaned_data['course'],
                    event=form.cleaned_data['event'],
                )
                messages.add_message(request, messages.SUCCESS, 'Problem was created successfully')
                form = question_form_class(request.user)
                correct_answer_formset = correct_answer_formset_class(prefix='correct')
                distractor_answer_formset = distractor_answer_formset_class(prefix='distractor')

            except QuestionCreateException as e:
                messages.add_message(request, messages.ERROR, e.user_message)
    else:
        form = question_form_class(request.user)

        correct_answer_formset = correct_answer_formset_class(prefix='correct')
        distractor_answer_formset = distractor_answer_formset_class(prefix='distractor')

    return render(request, 'problem_create.html', {
        'form': form,
        'correct_answer_formset': correct_answer_formset,
        'distractor_answer_formset': distractor_answer_formset,
        'header': header,
    })
Beispiel #3
0
    def setUp(self):
        self.client = Client()

        MyUser.objects.create_user("test_user", "*****@*****.**",
                                   "aaaaaaaa")
        self.user = MyUser.objects.get()

        self.category = QuestionCategory(name="category",
                                         description="category")
        self.category.save()

        for i in range(10):
            create_multiple_choice_question(title="title",
                                            text='text',
                                            answer='a',
                                            max_submission_allowed=999,
                                            tutorial='tt',
                                            author=self.user,
                                            category=self.category,
                                            difficulty="EASY",
                                            is_verified=True,
                                            variables='[]',
                                            choices={
                                                'a': 'a',
                                                'b': 'b'
                                            },
                                            visible_distractor_count=3)
            create_java_question(title='title',
                                 text='text',
                                 max_submission_allowed=999,
                                 tutorial='tutorial',
                                 author=self.user,
                                 category=self.category,
                                 difficulty='EASY',
                                 is_verified=True,
                                 junit_template='',
                                 additional_file_name='a.java')

        MyUser.objects.create_user('test_user2', "*****@*****.**",
                                   "aaaaaaaa")
    def populate_multiple_choice_questions(self):
        MultipleChoiceQuestion.objects.all().delete()

        with open('import/multiple_choice_questions.json') as f:
            questions = json.loads(f.read())
            for question in questions:
                create_multiple_choice_question(
                    title=question['title'],
                    text=question['text'],
                    answer='a',
                    max_submission_allowed=4,
                    tutorial=None,
                    author=None,
                    category=QuestionCategory.objects.first() if QuestionCategory.objects.all().exists() else None,
                    difficulty="EASY",
                    is_verified=True,
                    variables=[],
                    choices=question['choices'],
                    visible_distractor_count=3,
                    answer_text=None,
                    distractors=None,
                )
    def setUp(self):
        self.client = Client()

        MyUser.objects.create_user("test_user", "*****@*****.**",
                                   "aaaaaaaa")
        self.user = MyUser.objects.get()

        self.category = QuestionCategory(name="category",
                                         description="category")
        self.category.save()

        self.course = CanvasCourse(
            mock=True,
            name="Test",
            url="http://canvas.ubc.ca",
            course_id=1,
            token="test token",
            allow_registration=True,
            visible_to_students=True,
            start_date=timezone.now(),
            end_date=timezone.now() + timezone.timedelta(days=10),
            verification_assignment_group_name="test",
            verification_assignment_name="test",
            bonus_assignment_group_name="test",
        )
        self.course.save()

        self.event = Event(name="test_event",
                           type="ASSIGNMENT",
                           course=self.course,
                           count_for_tokens=False,
                           start_date=timezone.now(),
                           end_date=timezone.now() +
                           timezone.timedelta(days=10))
        self.event.save()

        for i in range(10):
            create_multiple_choice_question(title="title",
                                            text='text',
                                            answer='a',
                                            max_submission_allowed=999,
                                            tutorial='tt',
                                            author=self.user,
                                            category=self.category,
                                            difficulty="EASY",
                                            is_verified=True,
                                            variables='[]',
                                            choices={
                                                'a': 'a',
                                                'b': 'b'
                                            },
                                            visible_distractor_count=3,
                                            event=self.event)
            create_java_question(title='title',
                                 text='text',
                                 max_submission_allowed=999,
                                 tutorial='tutorial',
                                 author=self.user,
                                 category=self.category,
                                 difficulty='EASY',
                                 is_verified=True,
                                 junit_template='',
                                 input_files=[{
                                     'name': 'A.java',
                                     'compile': False,
                                     'template': '',
                                 }, {
                                     'name': 'B.java',
                                     'compile': True,
                                     'template': '',
                                 }, {
                                     'name': 'C.java',
                                     'compile': False,
                                     'template': '',
                                 }],
                                 event=self.event)

        MyUser.objects.create_user('test_user2', "*****@*****.**",
                                   "aaaaaaaa")