def choice_add(request, question_id): pregunta = Pregunta.objects.get(id = question_id) try: choice = Respuesta.objects.get(question = pregunta, correct = 1) except Respuesta.DoesNotExist: if request.method =='POST': form = ChoiceForm(request.POST) if form.is_valid(): choice = form.save(commit = False) choice.question = pregunta choice.vote = 0 choice.save() #form.save() else: form = ChoiceForm() return render(request, 'polls/choice_new.html', {'title':'Pregunta:'+ pregunta.question_text,'form': form, 'question': pregunta}) if request.method =='POST': form = ChoiceForm1(request.POST) if form.is_valid(): choice = form.save(commit = False) choice.question = pregunta choice.correct = False choice.vote = 0 choice.save() #form.save() else: form = ChoiceForm1() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render(request, 'polls/choice_new.html', {'title':'Pregunta:'+ pregunta.question_text,'form': form, 'question': pregunta})
def choice_add(request, question_id): question = Question.objects.get(id=question_id) total_choice = Choice.objects.filter(question=question_id).count() mensaje = '' tieneRespuestaCorrecta = 0 if request.method == 'POST': form = ChoiceForm(request.POST) if form.is_valid(): if total_choice < 4: TrueChoice = form.cleaned_data['correct_answer'] if TrueChoice is True: opciones = Choice.objects.filter(question=question_id) tieneRespuestaCorrecta = opciones.filter( correct_answer=True).count() if tieneRespuestaCorrecta == 0: opcion = form.save(commit=False) opcion.question = question opcion.vote = 0 opcion.save() mensaje = 'Respuesta correctamente añadida' else: mensaje = 'Ya existe una respuesta correcta' else: mensaje = 'No se permiten más respuestas' else: form = ChoiceForm() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'mensaje': mensaje, 'form': form })
def choice_add(request, question_id): question1 = Question.objects.get(id=question_id) qs = Choice.objects.filter(question=question1) i = 0 hayCorrecta = False mensaje = '' for obj in qs: i = i + 1 if obj.IsCorrect == True: hayCorrecta = True if request.method == 'POST' and i < 4: form = ChoiceForm(request.POST) if form.is_valid(): choice = form.save(commit=False) choice.question = question1 choice.vote = 0 if not (choice.IsCorrect == True and hayCorrecta == True): choice.save() else: mensaje = 'ERROR: Ya hay una respuesta correcta para esta pregunta.' #form.save() else: if i >= 4: mensaje = 'ERROR: Se ha alcanzado el numero maximo de respuestas para esta pregunta. No puede haber mas de cuatro respuestas.' form = ChoiceForm() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question1.question_text, 'form': form, 'error_message': mensaje })
def choice_add(request, question_id): question = Question.objects.get(id=question_id) cantidadChoice = Choice.objects.filter(question=question_id).count() message = '' hayTrue = 0 if request.method == "POST": form = ChoiceForm(request.POST) if form.is_valid(): if cantidadChoice < 4: choiceTrue = form.cleaned_data['correct'] if choiceTrue is True: choices = Choice.objects.filter(question=question_id) hayTrue = choices.filter(correct=True).count() if hayTrue == 0: choice = form.save(commit=False) choice.question = question choice.vote = 0 choice.save() message = 'Respuesta añadida con éxito.' else: message = 'Ya hay una respuesta correcta.' else: message = 'Máximo de respuestas alcanzadas.' else: form = ChoiceForm() return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'message': message, 'form': form })
def choice_add(request, question_id): question = Question.objects.get(id=question_id) choice_list = Choice.objects.filter(question=question) choice_count = 0 for c_choice in choice_list: choice_count += 1 if choice_count < 4: if request.method == 'POST': form = ChoiceForm(request.POST) if form.is_valid(): choice = form.save(commit=False) choice.question = question choice.vote = 0 choice.save() #form.save() else: form = ChoiceForm() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render(request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form }) else: form = ChoiceForm() form.fields['choice_text'].widget.attrs['readonly'] = True return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'message': "ERROR: Número máximo de opciones alcanzado.", })
def choice_add(request, question_id): form = ChoiceForm() message = "" question = Question.objects.get(id=question_id) numChoices = Choice.objects.filter(question=question_id).count() if numChoices >= question.num_choices: message = "Se han insertado el número máximo de respuestas posibles" form = None elif request.method == 'POST': form = ChoiceForm(request.POST) if form.is_valid(): choice = form.save(commit=False) choice.question = question choice.vote = 0 choice.save() message = "Respuesta añadida!" #form.save() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'message': message })
def choice_add(request, question_id): question = Question.objects.get(id = question_id) if request.method =='POST': form = ChoiceForm(request.POST) if form.is_valid(): choice = form.save(commit = False) choice.question = question choice.vote = 0 choice.save() #form.save() else: form = ChoiceForm() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render(request, 'polls/choice_new.html', {'title':'Pregunta:'+ question.question_text,'form': form})
def choice_add(request, question_id): question = Question.objects.get(id = question_id) if request.method =='POST': form = ChoiceForm(request.POST) if form.is_valid(): choice = form.save(commit = False) print(form) choice.question = question choice.vote = 0 completed = request.POST.get('correct', '') == 'on' print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"+str(completed)) if completed is True: choice.correct=True; else: choice.correct=False; choice.save() else: form = ChoiceForm() return render(request, 'polls/choice_new.html', {'title':'Pregunta:'+ question.question_text,'form': form})
def choice_add(request, question_id): q = Question.objects.get(id=question_id) choices = Choice.objects.filter(question=q) num = len(choices) if request.method == 'POST': form = ChoiceForm(request.POST) choice = form.save(commit=False) if form.is_valid(): if num == 4: return render(request, 'polls/detail.html', { 'question': q, 'error_message': "ERROR: Ya hay 4 respuestas", }) haycorrecto = False for c in choices: if c.correcto: haycorrecto = True if not haycorrecto and num == 3: choice.correcto = True if haycorrecto and choice.correcto: return render( request, 'polls/detail.html', { 'question': q, 'error_message': "ERROR: ya hay una respuesta correcta", }) choice.question = q choice.vote = 0 choice.save() #form.save() else: form = ChoiceForm() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render(request, 'polls/choice_new.html', { 'title': 'Pregunta:' + q.question_text, 'form': form })
def choice_add(request, question_id): question = Question.objects.get(id=question_id) listaRespuestas = Choice.objects.filter(question=question) if (len(listaRespuestas) < 4): if request.method == 'POST': form = ChoiceForm(request.POST) if form.is_valid(): choice = form.save(commit=False) choice.question = question choice.vote = 0 choice.save() #form.save() return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'message': "Respuesta insertada." }) else: form = ChoiceForm() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render(request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form }) else: form = ChoiceForm() form.fields['choice_text'].widget.attrs['disabled'] = True return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'message': "Error, numero maximo de opciones alcanzado" })
def choice_add(request, question_id): question = Question.objects.get(id=question_id) if request.method == 'POST': form = ChoiceForm(request.POST) if form.is_valid(): choice = form.save(commit=False) numChoices = Choice.objects.filter(question=question).count() existsCorrectAnswer = Choice.objects.filter( question=question, correct=True).count() == 1 if numChoices == 4: message = 'Ya hay 4 respuestas asignadas a esta pregunta.' elif existsCorrectAnswer and choice.correct: message = 'Ya hay una respuesta correcta para esta pregunta.' elif numChoices == 3 and not existsCorrectAnswer and not choice.correct: message = 'Debe haber al menos un respuesta correcta.' else: choice.question = question choice.vote = 0 choice.save() message = 'La respuesta se ha guardado correctamente.' #form.save() else: message = '' form = ChoiceForm() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'result': message })
def choice_add(request, question_id): #Lista de opciones choices = Choice.objects.filter(question_id=question_id) cont = choices.count() question = Question.objects.get(id=question_id) if request.method == 'POST': form = ChoiceForm(request.POST) if form.is_valid(): choice = form.save(commit=False) #Si ya hay una opcion correcta if (choices.filter(correct='True').count() == 1 and choice.correct): return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'cont': cont, 'allIncorrect': 'No puede haber 2 respuestas correctas' }) #Si se han introducido 3 opciones incorrectas if (Choice.objects.filter(question_id=question_id).filter( correct='False').count() == 3 and not choice.correct): return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'cont': cont, 'allIncorrect': 'La respuesta tiene que ser correcta' }) print(choice.correct) choice.question = question choice.vote = 0 choice.save() cont += 1 #form.save() else: form = ChoiceForm() #return render_to_response ('choice_new.html', {'form': form, #'poll_id': poll_id,}, context_instance = RequestContext(request),) if (choices.count() < 2): return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'cont': cont, 'allIncorrect': 'Tiene que haber al menos 2 respuestas' }) if cont >= 4: return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'mtf': 'Ya hay mas de 4 posibles respuestas' }) return render(request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'cont': cont })
def choice_add(request, question_id): question = Question.objects.get(id=question_id) error_message = '' if request.method == 'POST': form = ChoiceForm(request.POST) if form.is_valid() and Choice.objects.filter( question_id=question_id).count() < 4: choice = form.save(commit=False) choice.question = question choice.vote = 0 if choice.correct and Choice.objects.filter( question_id=question_id, correct=1).count() == 1: error_message = { 'texto': 'La pregunta ya tiene una opcion correcta', 'color': 'red' } #return render(request, 'polls/choice_new.html', #{'title':'Pregunta:' + question.question_text,'form': #form}) elif Choice.objects.filter(question_id=question_id).count() == 3: if (choice.correct and Choice.objects.filter( question_id=question_id, correct=1).count() == 0) or (not choice.correct and Choice.objects.filter( question_id=question_id, correct=1).count() == 1): choice.save() else: error_message = { 'texto': 'La pregunta carece de opciones correctas', 'color': 'red' } #return render(request, 'polls/choice_new.html', #{'title':'Pregunta:' + question.question_text,'form': #form}) else: choice.save() error_message = { 'texto': 'La opcion se ha añadido correctamente.', 'color': 'green' } else: form = ChoiceForm() contador = Choice.objects.filter(question_id=question_id).count() if (contador < 2): valid = { 'texto': 'La pregunta no es valida todavia, ha de tener como minimo 2 opciones.', 'color': 'red' } elif (contador > 3): valid = { 'texto': 'La pregunta ya tiene 4 opciones y es valida.', 'color': 'green' } return render(request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'valid': valid }) elif (Choice.objects.filter(question_id=question_id, correct=1).count() == 0): valid = { 'texto': 'La pregunta no es valida todavia, carece de opcion correcta.', 'color': 'red' } else: valid = { 'texto': 'La pregunta es valida, pero puedes añadir mas opciones incorrectas.', 'color': 'green' } return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'error_message': error_message, 'valid': valid })
def choice_add(request, question_id): question = Question.objects.get(id=question_id) if request.method == 'POST': form = ChoiceForm(request.POST) choices = Choice.objects.filter(question=question_id) counter = count(choices) print("Opciones = ", counter, "\n") correctCounter = count(choices.filter(isCorrect=True)) print("Correctas = ", correctCounter, "\n") if form.is_valid(): if counter == 4: print("HAY 4 OPCIONES\n") return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'message': 'Esta pregunta ya tiene 4 opciones' }) elif counter < 1: print("HAY MENOS DE 2 OPCIONES") choice = form.save(commit=False) choice.question = question choice.vote = 0 choice.save() return render( request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form, 'message': 'Debe tener 2 opciones como mínimo!!' }) elif counter == 3 and correctCounter == 0: print("HAY 3 OPCIONES Y NO HAY CORRECTA\n") #tiene que ser correcta choice = form.save(commit=False) choice.question = question choice.isCorrect = True choice.vote = 0 choice.save() elif counter < 4 and correctCounter == 1: print("HAY MENOS DE 4 OPCIONES Y HAY CORRECTA\n") #tiene que ser INcorrecta choice = form.save(commit=False) choice.question = question choice.isCorrect = False choice.vote = 0 choice.save() else: print("HAY MENOS DE 4 OPCIONES Y NO HAY CORRECTA\n") choice = form.save(commit=False) choice.question = question choice.vote = 0 choice.save() form.save() else: form = ChoiceForm() #return render_to_response ('choice_new.html', {'form': form, 'poll_id': poll_id,}, context_instance = RequestContext(request),) return render(request, 'polls/choice_new.html', { 'title': 'Pregunta:' + question.question_text, 'form': form })