def create_or_modify(topic_name, question, answer, user): """ Creates or modifies a topic based if we have topics with the name entered and the user is the creator. """ query_ids = topics_by_id(topic_name) topic_com = None for topic_id in query_ids: topic = Topic.objects.get(id=topic_id) if topic.creator == user: topic_com = topic break if not topic_com: topic_to_add = Topic() topic_to_add.name = parsing.scrub_name(topic_name) topic_to_add.color = random_color() topic_to_add.creator = user topic_to_add.created_flag = False topic_to_add.save() topic_com = topic_to_add # In case the user wants to add questions to an existing topic. question_to_add = Question() question_to_add.topic = topic_com question_to_add.question = markdown.markdown(question) question_to_add.answer = markdown.markdown(answer) question_to_add.added_flag = False question_to_add.save() return topic_com
def respond_question(request, pk_meeting, pk_quiz): meeting = get_object_or_404(Meeting, pk=pk_meeting) quiz = get_object_or_404(Quiz, pk=pk_quiz) list_questions = quiz.question_questionnaires.all() BaseQuestionFormSet = formset_factory(QuestionCompleteForm, extra=len(list_questions)) options = {'NÃO', 'SIM'} cont = 1 questions = [] if request.method == 'POST' or request.method == 'post': question_form = BaseQuestionFormSet(request.POST) if question_form.is_valid(): for auxQuestion in list_questions: questions.append(auxQuestion) for aux in range(len(list_questions)): question = Question() question_id = request.POST.get('question_' + str(cont)) answer = request.POST.get('answer_' + str(question_id)) question_remove = Question.objects.get(id=questions[aux].id) cont += 1 question.question = questions[aux].question question.answer = answer if answer != None: quiz.question_questionnaires.remove(question_remove) question_remove.delete() question.save() quiz.question_questionnaires.add(question) questions_json(pk_quiz) messages.success(request, 'Questionário Respondido Com Sucesso!') return redirect('meeting_show', pk=meeting.id) else: question_form = BaseQuestionFormSet() return render( request, 'questions/respond_question.html', { 'formset': question_form, 'meeting': meeting, 'quiz': quiz, 'list_questions': list_questions, 'options': options })
def thumbs_up(request): answer_id = request.GET.get('answer_id') question = request.GET.get('q') qs = Question.objects.filter(question=question) if qs.count() == 0: new_question = Question() new_question.question = question answer = Answer.objects.get(pk=answer_id) new_question.answer = answer new_question.save() return redirect('/search')
def include_questions(q_a, topic_name, user): """ Inserting the questions and answers in the db. """ topics = list(Topic.objects.all()) if topic_name not in [topic.name for topic in topics]: topic = Topic() topic.name = topic_name topic.color = random_color() topic.creator = user topic.created_flag = False topic.save() else: topic = Topic.objects.get(name=topic_name) for question_el, answer_el in q_a.items(): if not same_questions(question_el, topic_name) and not same_answers( answer_el, topic_name ): question = Question() question.topic = topic question.question = question_el question.answer = answer_el question.added_flag = False question.save() return topic.id