from flask import abort, render_template
from flask_login import current_user

from app import data_api_client
from .. import buyers, content_loader
from ...helpers.search_helpers import get_template_data


@buyers.route('/buyers')
def buyer_dashboard():
    template_data = get_template_data(buyers, {})
    user_briefs = data_api_client.find_briefs(current_user.id).get('briefs', [])
    draft_briefs = [brief for brief in user_briefs if brief['status'] == 'draft']
    live_briefs = [brief for brief in user_briefs if brief['status'] == 'live']

    return render_template(
        'buyers/dashboard.html',
        draft_briefs=draft_briefs,
        live_briefs=live_briefs,
        **template_data
    )


@buyers.route('/buyers/frameworks/<framework_slug>/requirements/<lot_slug>/create', methods=['GET'])
def start_new_brief(framework_slug, lot_slug):
    """Page to kick off creation of a new brief."""

    framework = data_api_client.get_framework(framework_slug)['frameworks']
    if framework['status'] != 'live':
        abort(404)

    try:
        lot = next(lot for lot in framework['lots'] if lot['slug'] == lot_slug)
    except StopIteration:
        abort(404)
    if not lot['allowsBrief']:
        abort(404)

    content = content_loader.get_manifest(framework_slug, 'edit_brief').filter(
        {'lot': lot['slug']}
    )

    section = content.get_section(content.get_next_editable_section_id())

    return render_template(
        "buyers/edit_brief_section.html",
        framework=framework,
        data={},
        section=section,
        **dict(buyers.config['BASE_TEMPLATE_DATA'])
    ), 200

@@ -33,16 +33,25 @@
 template
+, request
 %0Afrom fl
@@ -208,16 +208,51 @@
 e_data%0A%0A
+from dmapiclient import HTTPError%0A%0A
 %0A@buyers
@@ -1662,8 +1662,1092 @@
  ), 200%0A
+%0A%0A@buyers.route('/buyers/frameworks/%3Cframework_slug%3E/requirements/%3Clot_slug%3E/create', methods=%5B'POST'%5D)%0Adef create_new_brief(framework_slug, lot_slug):%0A    framework = data_api_client.get_framework(framework_slug)%5B%22frameworks%22%5D%0A    if framework%5B%22status%22%5D != %22live%22:%0A        abort(404)%0A%0A    try:%0A        lot = next(lot for lot in framework%5B'lots'%5D if lot%5B'slug'%5D == lot_slug)%0A    except StopIteration:%0A        abort(404)%0A    if not lot%5B'allowsBrief'%5D:%0A        abort(404)%0A%0A    content = content_loader.get_manifest(framework_slug, 'edit_brief').filter(%0A        %7B'lot': lot%5B'slug'%5D%7D%0A    )%0A%0A    section = content.get_section(content.get_next_editable_section_id())%0A%0A    update_data = section.get_data(request.form)%0A%0A    try:%0A        brief = data_api_client.create_brief(%0A            framework_slug,%0A            lot_slug,%0A            current_user.id,%0A            update_data,%0A            updated_by=current_user.email_address,%0A            page_questions=update_data.keys()%0A        )%5B%22briefs%22%5D%0A    except HTTPError as e:%0A        print(%22API ERROR ERROR ERROR: %7B%7D%22.format(e))%0A        raise e%0A

Add first version of POST endpoint to create a brief