Пример #1
0
def EditQuestion(request, question_slug='', **kwargs):

#def inline_formset(request, form_class, template):
    template = 'edit_question.html'
    
    MultipleChoiceAnswerItemFormset = inlineformset_factory(MultipleChoiceQuestion, MultipleChoiceAnswerItem,  form=MultipleChoiceAnswerItemModelForm, formset=MultipleChoiceAnswerItemModelFormset, extra=1, can_delete=True)
    theQuestion = MultipleChoiceQuestion.objects.get(slug=question_slug)
   
    # if POST the user submitted changes to the question. So validate and save to DB
    if request.method == 'POST':
        
        form = MultipleChoiceQuestionModelForm( request.POST, prefix="Question", instance=theQuestion)
        formset = MultipleChoiceAnswerItemFormset(request.POST, prefix="Answer", instance=theQuestion)
        
        is_question_form_valid = form.is_valid()
        is_answer_formset_valid = formset.is_valid()
        
        if is_question_form_valid and is_answer_formset_valid:
            
            try:
                form.save()
                formset.save()
            except (DatabaseError, IntegrityError) as e:
                return DefaultErrorPage(e, mesg="Could not save your changes to the question due to a database error. Try again later.")
            
            #redirect to the show the updated question
                
            return  HttpResponseRedirect(reverse('ShowQuestionDetails', kwargs={'question_slug' : question_slug}))
        else: #form is invalid, re-render form with errors
           
            return render_to_response(template, {'form': form, 'formset': formset, 'question': theQuestion, 'add_or_edit' : 'edit'}, context_instance=RequestContext(request))
    #handle GET
    #render the form
    else:
        form = MultipleChoiceQuestionModelForm(prefix="Question", instance=theQuestion)
        formset = MultipleChoiceAnswerItemFormset(prefix="Answer", instance=theQuestion)
    return render_to_response(template, {'form': form, 'formset': formset, 'question': theQuestion, 'add_or_edit': 'edit' },
        context_instance=RequestContext(request))
Пример #2
0
def AddQuestion(request, **kwargs):
    
    template = 'edit_question.html'
    
    theUser = request.user
    
    #inline formset is a good tool to use to get answers that have a foreign key to a question
    
    MultipleChoiceAnswerItemFormset = inlineformset_factory(MultipleChoiceQuestion, MultipleChoiceAnswerItem, form=MultipleChoiceAnswerItemModelForm,  formset=MultipleChoiceAnswerItemModelFormset, extra=2, can_delete=True)
    
    #POST means user has submitted the form with new question and answer options

    if request.method == 'POST':
        
        #prefix helps distinguish forms from each other
        form = MultipleChoiceQuestionModelForm(request.POST, prefix='Question')
        formset = MultipleChoiceAnswerItemFormset(request.POST, prefix='Answer')
        
        
        if form.is_valid() and formset.is_valid():
            
            
            
            
            try:
                #create new question but don't commit to DB yet as we have to 
                #create the survey object first as the question has a foreign key to the survey object
                
                new_question=form.save(commit=False)
                
                #create new survey, for now each question has a survey object. no more than 1 question per survey for now
                
                new_survey = MultipleChoiceSurvey(create_date= datetime.datetime.now(), author = theUser)
                new_survey.save()
                
                the_new_id = new_survey.id
                
                new_question.linked_survey = MultipleChoiceSurvey.objects.get(id=the_new_id)
                
                new_question.save()
                
                new_question_in_db = MultipleChoiceQuestion.objects.get(id=new_question.id)
                
                new_answers = formset.save(commit=False)
                
                
                for a in new_answers:
                    a.linked_question = new_question_in_db
                    a.save()
            
            #To-do: need to actually do a rollback of all changes here upon getting an unlikely DB error
            #should use Django's atomic.transaction probably
            except (DatabaseError, IntegrityError) as e:
                return DefaultErrorPage(e, "Your question could not be saved due to some problem with the database. Please try again later.")
            
            #redirect to ShowQuestionDetails when done adding a question
            return HttpResponseRedirect(reverse('ShowQuestionDetails', kwargs={'question_slug' : new_question_in_db.slug}))
        else:
        
             #if form is not valid, i.e. the new question has a problem, just rerender the form with errors
            return render_to_response(template, {'form': form, 'formset': formset,  'add_or_edit' : 'add'}, context_instance=RequestContext(request))
            
    
    # if method is GET show a blank form       
    else:
        form = MultipleChoiceQuestionModelForm(prefix='Question')
        formset = MultipleChoiceAnswerItemFormset(prefix='Answer')
    return render_to_response(template, {'form': form, 'formset': formset, 'add_or_edit': 'add'},
        context_instance=RequestContext(request))
Пример #3
0
def AddQuestion(request, **kwargs):

    template = 'edit_question.html'

    theUser = request.user

    #inline formset is a good tool to use to get answers that have a foreign key to a question

    MultipleChoiceAnswerItemFormset = inlineformset_factory(
        MultipleChoiceQuestion,
        MultipleChoiceAnswerItem,
        form=MultipleChoiceAnswerItemModelForm,
        formset=MultipleChoiceAnswerItemModelFormset,
        extra=2,
        can_delete=True)

    #POST means user has submitted the form with new question and answer options

    if request.method == 'POST':

        #prefix helps distinguish forms from each other
        form = MultipleChoiceQuestionModelForm(request.POST, prefix='Question')
        formset = MultipleChoiceAnswerItemFormset(request.POST,
                                                  prefix='Answer')

        if form.is_valid() and formset.is_valid():

            try:
                #create new question but don't commit to DB yet as we have to
                #create the survey object first as the question has a foreign key to the survey object

                new_question = form.save(commit=False)

                #create new survey, for now each question has a survey object. no more than 1 question per survey for now

                new_survey = MultipleChoiceSurvey(
                    create_date=datetime.datetime.now(), author=theUser)
                new_survey.save()

                the_new_id = new_survey.id

                new_question.linked_survey = MultipleChoiceSurvey.objects.get(
                    id=the_new_id)

                new_question.save()

                new_question_in_db = MultipleChoiceQuestion.objects.get(
                    id=new_question.id)

                new_answers = formset.save(commit=False)

                for a in new_answers:
                    a.linked_question = new_question_in_db
                    a.save()

            #To-do: need to actually do a rollback of all changes here upon getting an unlikely DB error
            #should use Django's atomic.transaction probably
            except (DatabaseError, IntegrityError) as e:
                return DefaultErrorPage(
                    e,
                    "Your question could not be saved due to some problem with the database. Please try again later."
                )

            #redirect to ShowQuestionDetails when done adding a question
            return HttpResponseRedirect(
                reverse('ShowQuestionDetails',
                        kwargs={'question_slug': new_question_in_db.slug}))
        else:

            #if form is not valid, i.e. the new question has a problem, just rerender the form with errors
            return render_to_response(template, {
                'form': form,
                'formset': formset,
                'add_or_edit': 'add'
            },
                                      context_instance=RequestContext(request))

    # if method is GET show a blank form
    else:
        form = MultipleChoiceQuestionModelForm(prefix='Question')
        formset = MultipleChoiceAnswerItemFormset(prefix='Answer')
    return render_to_response(template, {
        'form': form,
        'formset': formset,
        'add_or_edit': 'add'
    },
                              context_instance=RequestContext(request))
Пример #4
0
def EditQuestion(request, question_slug='', **kwargs):

    #def inline_formset(request, form_class, template):
    template = 'edit_question.html'

    MultipleChoiceAnswerItemFormset = inlineformset_factory(
        MultipleChoiceQuestion,
        MultipleChoiceAnswerItem,
        form=MultipleChoiceAnswerItemModelForm,
        formset=MultipleChoiceAnswerItemModelFormset,
        extra=1,
        can_delete=True)
    theQuestion = MultipleChoiceQuestion.objects.get(slug=question_slug)

    # if POST the user submitted changes to the question. So validate and save to DB
    if request.method == 'POST':

        form = MultipleChoiceQuestionModelForm(request.POST,
                                               prefix="Question",
                                               instance=theQuestion)
        formset = MultipleChoiceAnswerItemFormset(request.POST,
                                                  prefix="Answer",
                                                  instance=theQuestion)

        is_question_form_valid = form.is_valid()
        is_answer_formset_valid = formset.is_valid()

        if is_question_form_valid and is_answer_formset_valid:

            try:
                form.save()
                formset.save()
            except (DatabaseError, IntegrityError) as e:
                return DefaultErrorPage(
                    e,
                    mesg=
                    "Could not save your changes to the question due to a database error. Try again later."
                )

            #redirect to the show the updated question

            return HttpResponseRedirect(
                reverse('ShowQuestionDetails',
                        kwargs={'question_slug': question_slug}))
        else:  #form is invalid, re-render form with errors

            return render_to_response(template, {
                'form': form,
                'formset': formset,
                'question': theQuestion,
                'add_or_edit': 'edit'
            },
                                      context_instance=RequestContext(request))
    #handle GET
    #render the form
    else:
        form = MultipleChoiceQuestionModelForm(prefix="Question",
                                               instance=theQuestion)
        formset = MultipleChoiceAnswerItemFormset(prefix="Answer",
                                                  instance=theQuestion)
    return render_to_response(template, {
        'form': form,
        'formset': formset,
        'question': theQuestion,
        'add_or_edit': 'edit'
    },
                              context_instance=RequestContext(request))