from flask import render_template, abort, request, redirect, flash
from flask_login import current_user

from ..helpers import login_required
from ..helpers.briefs import (
    get_brief,
    ensure_supplier_is_eligible_for_brief,
    send_brief_clarification_question,
)
from ..helpers.frameworks import get_framework_and_lot
from ...main import main, content_loader
from ... import data_api_client


@main.route('/opportunities/<int:brief_id>/ask-a-question', methods=['GET', 'POST'])
@login_required
def ask_brief_clarification_question(brief_id):
    brief = get_brief(data_api_client, brief_id)
    ensure_supplier_is_eligible_for_brief(brief, current_user.supplier_id)

    error_message = None
    clarification_question_value = None

    if request.method == 'POST':
        clarification_question = request.form.get('clarification-question', '').strip()
        if not clarification_question:
            error_message = "Question cannot be empty"
        elif len(clarification_question) > 5000:
            clarification_question_value = clarification_question
            error_message = "Question cannot be longer than 5000 characters"
        else:
            send_brief_clarification_question(brief, clarification_question)
            flash('message_sent', 'success')

    return render_template(
        "briefs/clarification_question.html",
        brief=brief,
        error_message=error_message,
        clarification_question_name='clarification-question',
        clarification_question_value=clarification_question_value
    ), 200 if not error_message else 400


@main.route('/opportunities/<int:brief_id>/responses/create', methods=['GET'])
@login_required
def submit_brief_response(brief_id):

    brief = data_api_client.get_brief(brief_id)['briefs']
    if brief['status'] != 'live':
        abort(404)

    framework_slug = brief['frameworkSlug']
    lot_slug = brief['lotSlug']

    framework, lot = get_framework_and_lot(data_api_client, framework_slug, lot_slug, allowed_statuses=['live'])

    content = content_loader.get_manifest(framework_slug, 'edit_brief_response').filter(
        {'lot': lot_slug}
    )
    section = content.get_section(content.get_next_editable_section_id())

    # pass all of the brief yes/no questions into the ContentQuestion
    for question in section.questions:
        if question.type == 'boolean_list':
            question.boolean_list_questions = brief[question.id]

    return render_template(
        "services/edit_submission_section.html",
        framework=framework,
        service_data={},
        section=section,
        **dict(main.config['BASE_TEMPLATE_DATA'])
    ), 200

@@ -41,22 +41,31 @@
 , re
-quest, redirec
+direct, url_for, reques
 t, f
@@ -107,16 +107,51 @@
 t_user%0A%0A
+from dmapiclient import HTTPError%0A%0A
 from ..h
@@ -2694,8 +2694,1749 @@
  ), 200%0A
+%0A%0A# Add a create route%0A@main.route('/opportunities/%3Cint:brief_id%3E/responses/create', methods=%5B'POST'%5D)%0A@login_required%0Adef create_new_brief_response(brief_id):%0A    %22%22%22Hits up the data API to create a new brief response.%22%22%22%0A%0A    brief = data_api_client.get_brief(brief_id)%5B'briefs'%5D%0A    if brief%5B'status'%5D != 'live':%0A        abort(404)%0A%0A    framework_slug = brief%5B'frameworkSlug'%5D%0A    lot_slug = brief%5B'lotSlug'%5D%0A%0A    # need the check that the framework is live%0A    framework, lot = get_framework_and_lot(data_api_client, framework_slug, lot_slug, allowed_statuses=%5B'live'%5D)%0A%0A    content = content_loader.get_manifest(framework_slug, 'edit_brief_response').filter(%0A        %7B'lot': lot_slug%7D%0A    )%0A    section = content.get_section(content.get_next_editable_section_id())%0A    response_data = section.get_data(request.form)%0A%0A    try:%0A        data_api_client.create_brief_response(%0A            brief_id, current_user.supplier_id, response_data, current_user.email_address%0A        )%5B'briefResponses'%5D%0A    except HTTPError as e:%0A        errors = section.get_error_messages(e.message, lot_slug)%0A%0A        # pass all of the brief yes/no questions into the ContentQuestion%0A        for question in section.questions:%0A            if question.type == 'boolean_list':%0A                question.boolean_list_questions = brief%5Bquestion.id%5D%0A%0A        return render_template(%0A            %22services/edit_submission_section.html%22,%0A            framework=framework,%0A            service_data=request.form,%0A            section=section,%0A            errors=errors,%0A            **dict(main.config%5B'BASE_TEMPLATE_DATA'%5D)%0A        ), 400%0A%0A    flash('Your response to &lsquo;%7B%7D&rsquo; has been submitted.'.format(brief%5B'title'%5D))%0A%0A    return redirect(url_for(%22.dashboard%22))%0A

Create route that nearly works