コード例 #1
0
def new_question(request, politician_id=None):
    user = request.user
    request_info = get_request_information(request)

    question = Question(
        created_by=user,
        user_ip=request_info.client_ip,
        user_agent=request_info.client_user_agent,
        user_country=request_info.client_country
    )

    selected_politician = PoliticianInfo.active.filter(id=politician_id).first() if politician_id else None
    initial = {}

    if selected_politician:
        initial['politician'] = selected_politician

    new_question_form = NewQuestionForm(instance=question, initial=initial)
    success = None

    if request.method == 'POST':
        success = False
        new_question_form = NewQuestionForm(request.POST, instance=question)

        if new_question_form.is_valid():
            with reversion.create_revision():
                new_question_form.save()
            return redirect('question', question_id=question.id)

    return render(request, 'questions/new-question.html', {
        'new_question_form': new_question_form,
        'success': success
    })
コード例 #2
0
ファイル: views.py プロジェクト: kichawa/Archquestions
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 ask(request):
    if request.method == 'POST':
        form = NewQuestionForm(request.POST)
        if form.is_valid():
            pub = form.save(commit=False)
            pub.author = User.objects.get(username=request.user)
            pub.save()
            pub.tags.clear()

            tags = str(request.POST.get('tags'))
            tags = [tag.strip() for tag in tags.split(',')]
            for tag in tags:
                obj, create = Tag.objects.get_or_create(tagword=tag)
                pub.tags.add(obj)
            pub.save()
            return redirect('question', pub.id)
        else:
            print(form.errors)
    else:
        form = NewQuestionForm(instance=request.user)
    return render(request, 'questions/ask.html', {
        'form': form,
        'title': 'Ask',
        'trending': _get_trending(),
    })