Exemplo n.º 1
0
def question(request, question_id):
    try:
        question = Question.objects.get_single(int(question_id))
    except Question.DoesNotExist:
        raise Http404()

    # answers = question.answer_set.all()

    # question_ = get_object_or_404(Question, pk=question_id)
    answers = question.answer_set.all()

    likes = getQuestionPageLikes(question, answers, request.user)

    page = get_page(answers, request, 3)
    tags = Tag.objects.count_popular()
    form = AnswerForm()

    if request.method == "POST":
        form = AnswerForm(request.POST)
        if form.is_valid():
            ans = form.save(question, request.user)
            return HttpResponseRedirect('%s?page=%d' % (reverse(
                'question', kwargs={'question_id': question.id}), ans.id))
        else:
            form = AnswerForm()
    return render(
        request, 'question.html', {
            'question': question,
            'answers': page,
            'tags': tags,
            'form': form,
            'likes': likes[0],
            'dislikes': likes[1],
            'islike': likes[2]
        })
Exemplo n.º 2
0
def question(request, id):
    if request.method == "GET":
        form = AnswerForm(initial={
            'id_question': id,
        })

    if request.method == "POST":
        form = AnswerForm(data=request.POST)
        if form.is_valid():
            answer = form.save(commit=False)
            answer.question_id = form.cleaned_data['id_question']
            answer.author = request.user.users
            answer.save()
            return redirect(
                reverse('question',
                        kwargs={'id': form.cleaned_data['id_question']}))

    data = paginator(Answers.objects.answers_by_question(id), request)
    info = QuestionsLikes.objects.likes([Questions.objects.one_question(id)],
                                        request.user.pk)[0]
    vote_answer = AnswersLikes.objects.likes(data, request.user.pk)

    return render(
        request, 'answer.html', {
            'id': id,
            'one_question': Questions.objects.one_question(id),
            'data': data,
            'ans_data': zip(data, vote_answer),
            'info': info,
            'form': form
        })
Exemplo n.º 3
0
def question(request, qid):
    try:
        question = question_counter(Question.objects.get(id=qid))
    except:
        raise Http404("No matches Questions the given query.")

    if request.method == 'POST':
        form = AnswerForm(data=request.POST)
        if form.is_valid() and request.user.is_authenticated:
            answer = Answer.objects.create(author=request.user.profile,
                                           text=form.cleaned_data['text'],
                                           question=question)
    else:
        form = AnswerForm()
    page_obj, _ = paginate(Answer.objects.get_answer_by_likes(question),
                           request)
    tags = Tag.objects.get_popular()
    return render(
        request, 'question.html', {
            'user': request.user,
            'form': form,
            'question': question,
            'page_obj': page_obj,
            'tags': tags,
        })
Exemplo n.º 4
0
def question(request, id):
    last_id = Question.objects.latest('id').pk
    if id > last_id:
        id = last_id

    question = get_object_or_404(Question, id=id)
    answers = pagination(Answer.objects.answers(id), request, per_page=3)

    answers_votes = {}
    question_vote = {}
    if request.user.is_authenticated:
        answers_votes = VoteAnswer.objects.reaction(alist=list(answers.object_list.values_list('id', flat=True)),
                                                    aid=request.user.profile)

        question_vote = VoteQuestion.objects.reaction(qlist=[question.id, ],
                                                      aid=request.user.profile)

    if request.method == 'POST':
        form = AnswerForm(data=request.POST, profile=request.user.profile, question=question)
        if form.is_valid():
            form.save()
            # return redirect(reverse('question', kwargs={'id': id}) + '?page=last')
            return redirect(reverse('question', kwargs={'id': id}))

    else:
        form = AnswerForm(None, None)

    return render(request, 'question_page.html', {
        'title': f'Question {id}',
        'answers_votes': answers_votes,
        'votes': question_vote,
        'question': question,
        'answers': answers,
        'form': form
    })
Exemplo n.º 5
0
def choice_add(request, question_id):
    question = QuestionQuiz.objects.get(id=question_id)
    if request.method == 'POST':
        form = AnswerForm(request.POST)
        if form.is_valid():
            if question.answers_set.count() < 4:
                choice = form.save(commit=False)
                choice.question = question
                choice.vote = 0
                choice.save()
                #form.save()
            else:
                #messages.info(request, 'Esta pregunta ya tiene 4 respuestas.')
                return render(
                    request, 'polls/detail.html', {
                        'title': 'Respuestas asociadas a la pregunta:',
                        'question': question,
                        'max_answ': 'Esta pregunta ya tiene 4 respuestas.',
                    })
    else:
        form = AnswerForm()
    #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,
            'ans_count': question.answers_set.count(),
        })
Exemplo n.º 6
0
def question_answer(request, pk):
    last_id = Question.objects.latest('id').pk
    if pk > last_id:
        pk = last_id

    question = get_object_or_404(Question, id=pk)
    if request.method == 'POST':
        form = AnswerForm(data=request.POST,
                          author=request.user.author,
                          question=question)
        if form.is_valid():
            form.save()
            return redirect(
                reverse('question', kwargs={'pk': pk}) + '?page=last')

    else:
        form = AnswerForm(None, None)
    return render(
        request, "question_answer.html", {
            'questions': Question.objects.one_question(pk),
            'style': False,
            'comments': pagination(
                Answer.objects.answers(pk), request, per_page=3),
            'form': form
        })
Exemplo n.º 7
0
def question(request, qid):
    question = get_object_or_404(Question, id=qid)
    if request.method == "GET":
        form = AnswerForm()
    else:
        form = AnswerForm(data=request.POST)
        if form.is_valid() and request.user.is_authenticated:
            answer = Answer.objects.create(text=form.cleaned_data["field"],
                                           author=request.user,
                                           question=question)

            CL.publish(
                f"question{question.id}", {
                    "answer":
                    render_to_string("inc/answer.html", {
                        "answer": answer,
                    })
                })

    answers = Answer.objects.get_by_question(question)
    token = jwt.encode({"sub": "some_name"}, HMAC_TOKEN)
    return render(
        request, "question.html", {
            "form": form,
            "question": question,
            "answers": answers,
            "is_owner": (question.author == request.user),
            "TOKEN": token.decode(),
        })
Exemplo n.º 8
0
def deal_with_answer(request, question):
    form = AnswerForm(request.POST)
    print('arrive deal_with_answer')
    if form.is_valid():
        answer = form.cleaned_data['answer']
        c = Answer(answerer=request.user.profile, belong_to_question=question)
        c.content = answer
        c.save()
        return AnswerForm
    else:
        return form
Exemplo n.º 9
0
    def post(self, request, pk):
        if not request.user.is_authenticated:
            return HttpResponseRedirect(reverse('login'))

        form = AnswerForm(request.POST)
        if not form.is_valid():
            return render(request, 'question.html', {'form': form})
        new_answer = form.save(commit=False)
        new_answer.author = request.user.user
        new_answer.question_id = pk
        new_answer.save()
        return HttpResponseRedirect(reverse('question', args=(pk, )))
Exemplo n.º 10
0
def question(request, qid):
    context['question'] = Question.objects.get(pk=qid)
    form = AnswerForm(request.POST)

    if request.POST:
        if form.is_valid():
            answer = Answer.objects.create(
                question=context['question'],
                text=form.cleaned_data.get('textarea'),
                author=request.user.profile)

    context['answers'] = Answer.objects.hottest(qid)
    context['form'] = form
    return render(request, 'question.html', context)
Exemplo n.º 11
0
def question(request, qid):
    q = get_object_or_404(Question, pk=qid)
    if request.method == 'POST':
        form = AnswerForm(request.user.profile, q, data=request.POST)
        if form.is_valid():
            form.save()
    content, page = pagination(
        Answer.objects.question_answers(q),
        request,
        per_page=5,
    )
    return render(request, 'question.html', {
        'question': q,
        'answers': content,
        'page': page,
        **tags_and_users,
    })
Exemplo n.º 12
0
def question_page(request, no):
    question_ = Question.objects.filter(identificator=no).first()
    if request.method == 'GET':
        form = AnswerForm()
    else:
        form = AnswerForm(request.POST)
        if form.is_valid():
            answer = form.save(commit=False)
            answer.author = request.user.author
            form.instance.question = Question.objects.get(identificator=no)
            answer.save()
            question_.answers_count += 1
            question_.save(update_fields=['answers_count'])
            page = int(question_.answer_set.count() / 5) + 1
        return redirect(request.path + '?page=' + str(page) + '#' + str(answer.id))
    answers = Answer.objects.filter(question=question_)
    answers = paginate(request, answers)
    return render(request, 'question.html',
                  {'question': question_, 'answers': answers, 'page_obj': answers, 'form': form})
Exemplo n.º 13
0
def question(request, qid):
    question = Question.objects.select_related().get(id=qid)
    if request.method == 'GET':
        form = AnswerForm()
    elif request.method == 'POST':
        form = AnswerForm(data=request.POST)
        if form.is_valid():
            answer = form.save(commit=False)
            answer.author = request.user.profile
            answer.question = question
            answer.save()
            form = AnswerForm()
    answers = question.answer_set.order_by('-rating').all()
    answers_per_page = paginate(answers, request)
    return render(request, 'question.html', {
        'question': question,
        'answers': answers_per_page,
        'form': form,
    })
Exemplo n.º 14
0
def ask(request, pk):
    question = Question.objects.get(pk=pk)
    key = str(int(pk) + 1)
    if request.method == "POST":
        form = AnswerForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)
            post.question = question
            post.user = request.session['user']
            post.save()
            context = {
                'pk': pk,
                'key': key,
            }
            return render(request, 'app/select.html', context)
    else:
        form = AnswerForm()
        context = {'form': form, 'question': question, 'key': key}
        return render(request, 'app/ask.html', context)
Exemplo n.º 15
0
def one_question_page(request, num_quest):
    question = Question.objects.one_question(int(num_quest))

    answers = Answer.objects.answers_by_que(int(num_quest))

    page_obj, page = paginate(answers, request)

    best_members = UserProfile.objects.best_members()
    popular_tags = Tag.objects.popular_tags()

    if request.method == 'GET':
        form = AnswerForm()
    else:
        form = AnswerForm(data=request.POST)
        if form.is_valid() and request.user.is_authenticated:
            que = Question.objects.get(id=num_quest)
            answer = Answer.objects.create(author=request.user.userprofile,
                                           question=que,
                                           text=form.cleaned_data['text'])
            que.answers += 1
            que.save()
            answer.save()
            print(page)
            return redirect(
                reverse('one_question', kwargs={'num_quest': que.pk}) +
                f"?page={len(page_obj)}")

    return render(
        request, 'one_question_page.html', {
            'question': question[0],
            'answers': page_obj,
            'page': page,
            'form': form,
            'tags': tags,
            'num_q': num_quest,
            'popular_tags': popular_tags,
            'best_members': best_members
        })