Beispiel #1
0
def get_object_form(model, exclude_fields=None):  
    model_class = get_model_class(model)
    class _ObjectForm(forms.ModelForm):
        class Meta:
            model = model_class
            exclude = exclude_fields
    return _ObjectForm
Beispiel #2
0
def get_object_form(model, exclude_fields=None):  
    model_class = get_model_class(model)
    class _ObjectForm(forms.ModelForm):
        class Meta:
            model = model_class
            exclude = exclude_fields
    return _ObjectForm
Beispiel #3
0
def edit_question(request, question_id=None):
    """To add a new question in the database.
    Create a new question and store it."""
    user = request.user
    ci = RequestContext(request)
    if not question_id:
        raise Http404('No Question Found')

    question_instance = Question.objects.get(id=question_id)

    if request.method == "POST" and 'save_question' in request.POST:
        question_form = QuestionForm(request.POST, instance=question_instance)
        if question_form.is_valid():
            new_question = question_form.save(commit=False)
            test_case_type = question_form.cleaned_data.get('test_case_type')
            test_case_form_class = get_object_form(model=test_case_type, exclude_fields=['question'])
            test_case_model_class = get_model_class(test_case_type)
            TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1)
            test_case_formset = TestCaseInlineFormSet(request.POST, request.FILES, instance=new_question)
            if test_case_formset.is_valid():
                new_question.save()
                test_case_formset.save()
            return my_redirect("/exam/manage/addquestion/{0}".format(new_question.id))
        else:
            test_case_type = question_form.cleaned_data.get('test_case_type')
            test_case_form_class = get_object_form(model=test_case_type, exclude_fields=['question'])
            test_case_model_class = get_model_class(test_case_type)
            TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1)
            test_case_formset = TestCaseInlineFormSet(request.POST, request.FILES, instance=question_instance)
            return my_render_to_response('yaksh/add_question.html',
                                         {'form': question_form,
                                         'test_case_formset': test_case_formset,
                                         'question_id': question_id},
                                         context_instance=ci)
    else:
        question_form = QuestionForm(instance=question_instance)
        test_case_type = question_instance.test_case_type
        test_case_form_class = get_object_form(model=test_case_type, exclude_fields=['question'])
        test_case_model_class = get_model_class(test_case_type)
        TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1)
        test_case_formset = TestCaseInlineFormSet(instance=question_instance)

        return my_render_to_response('yaksh/add_question.html',
                                     {'form': question_form,
                                     'test_case_formset': test_case_formset,
                                     'question_id': question_id},
                                     context_instance=ci)