コード例 #1
0
async def answer_create(request):
    """
    Answer form
    """
    id = int(request.query_params['next'].split('/')[2])
    next = request.query_params['next']
    results = (
        await Question.get(id=id)
        .prefetch_related("user", "tags")
    )
    session_user = request.user.username
    data = await request.form()
    form = AnswerForm(data)
    result = await User.get(username=session_user)
    if request.method == "POST" and form.validate():
        query = Answer(
            content=form.content.data,
            created=datetime.datetime.now(),
            answer_like=0,
            is_accepted_answer=0,
            question_id=results.id,
            ans_user_id=result.id,
        )
        await query.save()
        return RedirectResponse(BASE_HOST + next, status_code=302)
    return templates.TemplateResponse(
        "questions/answer_create.html", {
            "request": request,
            "form": form,
            "next": next
        }
    )
コード例 #2
0
async def answer_create(request):
    """
    Answer form
    """
    request_path_id = int(request.query_params["next"].split("/")[2])
    request_query_next = request.query_params["next"]
    data = await request.form()
    form = AnswerForm(data)
    u = User
    session_user = (
        await u.select(u.id, u.username)
        .where(u.username == request.user.username)
        .first()
        .run()
    )
    if request.method == "POST" and form.validate():
        query = Answer(
            content=form.content.data,
            created_at=datetime.datetime.now(),
            answer_like=0,
            is_accepted_answer=0,
            question=request_path_id,
            ans_user=session_user["id"],
        )
        await query.save().run()
        return RedirectResponse(
            BASE_HOST + request_query_next, status_code=302
        )
    return templates.TemplateResponse(
        "questions/answer_create.html",
        {"request": request, "form": form, "next": request_query_next},
    )