예제 #1
0
def question_details(request, question_key):
    ctx = {}

    question = get_object_or_404(Question, key=question_key)

    if request.method == 'POST':
        user = users.get_current_user()
        if user is None:
            return redirect(users.create_login_url(request.path))

        initial = {
            'author': users.get_current_user(),
            'question': question,
        }
        form = AnswerForm(request.POST, initial=initial)
        if form.is_valid():
            # XXX FIXME - why initial data does not exist in cleaned data?
            form.cleaned_data.update(initial)

            form.save()

            return redirect(reverse('questions_question_list'))
        ctx['answer_form'] = form

    else:
        ctx['answer_form'] = AnswerForm()


    ctx['question'] = question
    return render(request, 'questions/question_details.html', ctx)
예제 #2
0
def question_new(request):
    # TODO - user has to be logged in
    ctx = {}

    if request.method == 'POST':
        initial = {'author': users.get_current_user()}
        form = NewQuestionForm(request.POST, initial=initial)

        if form.is_valid():

            # XXX FIXME - why author property does not exist in cleaned data?
            form.cleaned_data['author'] = users.get_current_user()

            question = form.save()
            QuestionTag.increment_tags(question.tags)
            redirect_url = reverse('questions_question_details',
                    kwargs={'question_key': question.key()})
            return redirect(redirect_url)

        ctx['form'] = form

    else:
        ctx['form'] = NewQuestionForm()

    return render(request, 'questions/question_new.html', ctx)
예제 #3
0
def question_list(request, tag=None):
    ctx = {
        'active_tags': [],
    }

    # TODO - pagination
    questions = Question.all().order('-score')
    # TODO - multiple tags filter support
    if tag:
        questions = questions.filter('tags =', tag)
        ctx['active_tags'].append(tag)

    tags = QuestionTag.all().order('-ref_count')

    ctx['questions'] = questions
    ctx['tags'] = tags
    return render(request, 'questions/question_list.html', ctx)