def new_flow():
    '''Create a new flow

    :status 200: Render the new flow template
    :status 302: Try to create a new flow using the
        :py:class:`~purchasing.conductor.forms.NewFlowForm`, redirect
        to the flows list view if successful
    '''
    stages = Stage.choices_factory()
    form = NewFlowForm(stages=stages)
    if form.validate_on_submit():
        stage_order = []
        for entry in form.stage_order.entries:
            # try to evaluate the return value as an ID
            try:
                stage_id = int(entry.data)
            # otherwise it's a new stage
            except ValueError:
                new_stage = Stage.create(name=entry.data)
                stage_id = new_stage.id
            stage_order.append(stage_id)

        Flow.create(flow_name=form.flow_name.data, stage_order=stage_order)
        flash('Flow created successfully!', 'alert-success')
        return redirect(url_for('conductor.flows_list'))

    return render_template('conductor/flows/new.html', stages=stages, form=form)