Esempio n. 1
0
def poll(request):
    """Appelée lorsque l'on souhaite répondre à un sondage"""

    ctx=context(request)
    id=int(request.GET.get("poll"))
    #print request.POST
    if id:

        try:
            poll=Poll.objects.get(id=id)
        except:
            return redirect("/liste")
        
        if not poll.anonymous and not ctx["connected"]:
            return redirect("/liste")



        ctx['poll']=poll


        lesQuestions=poll.question_set.all()
        ctx['lesQuestions']=[]

        #si on a des arguments en POST c'est que l'on a une réponse, sinon on crée les formulaires.

        #print type(request.session.get('username'))
        #print poll.alreadyAnwsered(request.session.get('username'))
        
        if poll.alreadyAnwsered(request.session.get('username')) or poll.endDate < datetime.today():
            return redirect('/poll?poll='+str(id))

        if request.method=="POST":
            lesReponses = []
            valid=True
            for i in range(0,len(lesQuestions)):
                q=lesQuestions[i]
                if hasattr(q,"textualquestion"):
                    af = TextualAnswerForm(request.POST,prefix="Anwser"+str(i))
                    q=q.textualquestion
                    
                elif hasattr(q,"mcquestion"):
                    af = MCQAnswerForm(q.mcquestion,request.POST,prefix="Answer"+str(i))
                    q=q.mcquestion
                valid = valid and af.is_valid()
                ctx['lesQuestions'].append((q,af))
                if valid:
                    answer=checkQuestion(af,q,poll,request)
                    lesReponses.append(answer)
            if valid:
                sig = None
                if(not poll.anonymous):
                    sig = Signature()
                    userldap=ldapINSA.rechercheLDAPUid(request.session.get('username'))
                    user,created = INSAUser.objects.get_or_create(uid=request.session.get('username'),defaults={"fullname":userldap.get('nom')+' '+userldap.get('prenom')})
                    sig.user=user
                    sig.poll=poll
                    sig.save()

                for i in range(0,len(lesReponses)):
                    saveQuestion(lesReponses[i],poll,request,sig)
                poll.answered()
                poll.save()
                return redirect('/liste')
           #on précise que le sondage a été répondu.
        else:
            for i in range(0,len(lesQuestions)):
                q=lesQuestions[i]
                if hasattr(q,"textualquestion"):
                    ctx['lesQuestions'].append((q.textualquestion,TextualAnswerForm(prefix="Anwser"+str(i))))
                elif hasattr(q,"mcquestion"):
                    mcqaf=MCQAnswerForm(q.mcquestion,prefix="Answer"+str(i))
                    #mcqaf.setChoice(q.mcquestion)
                    ctx['lesQuestions'].append((q.mcquestion,mcqaf))
        return render_to_response("sondages/votePoll.html",ctx)
    else:
        return redirect("/liste")
Esempio n. 2
0
def newpollsave(request):
    """Appelée lorsque l'utilisateur a rentré les informations globales du sondage (titre, date de fin...) et veut y ajouter de nouvelles questions"""

    ctx=context(request)

    pollform = PollForm(request.POST,prefix="newpoll")
    #print request.POST
    if pollform.is_valid():
        if request.session.get('newPoll')==None:
            userldap=ldapINSA.rechercheLDAPUid(request.session.get('username'))
            user,created = INSAUser.objects.get_or_create(uid=request.session.get('username'),defaults={"fullname":userldap.get('nom')+' '+userldap.get('prenom')})
            poll=pollform.save(commit=False)
            poll.pubDate=datetime.date.today()
            poll.pollster = user
            request.session['newPoll']=poll
        else:
            poll = request.session.get('newPoll')
            p2 = pollform.save(commit=False)
            poll.title=p2.title
            poll.endDate=p2.endDate
            poll.anonymous=p2.anonymous
            
    poll=request.session.get('newPoll')
    updatePoll(request,ctx,poll)
    
    
    
    if request.POST.get('button')=="envoyer" and poll != None :
        poll.editable=False
        poll.save()
        if(saveQuestions(request,ctx,poll)):
            del request.session['newPoll']
            del request.session['lesQuestions']
            del request.session['lesChoix']
            return redirect("/liste")

    elif request.POST.get('buttonAddQ')!=None:#"ajouter une question normale": 
        ctx['questionforms'].append((QuestionForm(prefix="newQuestion"),[]))

    elif request.POST.get('buttonAddMCQ')!=None:#"ajouter une question à choix multiple":
        ctx['questionforms'].append((MCQuestionForm(prefix="newMCQuestion"),[]))



    else:
        trouve=False
        #print ctx['questionforms']
        for i in range(0,len(ctx['questionforms'])):
            if not trouve:
                if request.POST.get('buttonAddTextualChoicequestion'+str(i))!=None:#ajouter un choix à une question multiple:
                    ctx['questionforms'][i][1].append(TextualChoiceForm(prefix="question"+str(i)+"newchoice"))
                    trouve=True
                if request.POST.get('buttonAddImageChoicequestion'+str(i))!=None:#ajouter un choix à une question multiple:
                    ctx['questionforms'][i][1].append(ImageChoiceForm(prefix="question"+str(i)+"newchoice"))
                    trouve=True
                if request.POST.get('buttonAddFileChoicequestion'+str(i))!=None:#ajouter un choix à une question multiple:
                    ctx['questionforms'][i][1].append(FileChoiceForm(prefix="question"+str(i)+"newchoice"))
                    trouve=True
        if(len(ctx['questionforms'])>0):
            if request.POST.get('buttonAddTextualChoicenewMCQuestion')!=None:#ajouter un choix à une question multiple:
                ctx['questionforms'][max(0,len(ctx['questionforms'])-1)][1].append(TextualChoiceForm(prefix="question"+str(len(ctx['questionforms'])-1)+"newchoice"))
                trouve=True
            if request.POST.get('buttonAddImageChoicenewMCQuestion')!=None:#ajouter un choix à une question multiple:
                ctx['questionforms'][max(0,len(ctx['questionforms'])-1)][1].append(ImageChoiceForm(prefix="question"+str(len(ctx['questionforms'])-1)+"newchoice"))
                trouve=True
            if request.POST.get('buttonAddFileChoicenewMCQuestion')!=None:#ajouter un choix à une question multiple:
                ctx['questionforms'][max(0,len(ctx['questionforms'])-1)][1].append(FileChoiceForm(prefix="question"+str(len(ctx['questionforms'])-1)+"newchoice"))
                trouve=True

    ctx['targetForm']="/add"
    ctx['pollform']=pollform
    return render_to_response("sondages/newpoll.html",ctx)