def remove_broadcast_area(service_id, broadcast_message_id, area_slug):
    BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    ).remove_area(area_slug)
    return redirect(
        url_for(
            '.preview_broadcast_areas',
            service_id=current_service.id,
            broadcast_message_id=broadcast_message_id,
        ))
def cancel_broadcast_message(service_id, broadcast_message_id):
    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )

    if broadcast_message.status != 'broadcasting':
        return redirect(
            url_for(
                '.view_broadcast_message',
                service_id=current_service.id,
                broadcast_message_id=broadcast_message.id,
            ))

    if request.method == 'POST':
        broadcast_message.cancel_broadcast()
        return redirect(
            url_for(
                '.view_broadcast_message',
                service_id=current_service.id,
                broadcast_message_id=broadcast_message.id,
            ))

    flash(['Are you sure you want to stop this broadcast now?'],
          'stop broadcasting')

    return render_template(
        'views/broadcast/view-message.html',
        broadcast_message=broadcast_message,
        hide_stop_link=True,
    )
def choose_broadcast_area(service_id, broadcast_message_id, library_slug):
    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )
    library = BroadcastMessage.libraries.get(library_slug)

    if library.is_group:
        return render_template(
            'views/broadcast/areas-with-sub-areas.html',
            search_form=SearchByNameForm(),
            show_search_form=(len(library) > 7),
            library=library,
            page_title=f'Choose a {library.name_singular.lower()}',
            broadcast_message=broadcast_message,
        )

    form = BroadcastAreaForm.from_library(library)
    if form.validate_on_submit():
        broadcast_message.add_areas(*form.areas.data)
        return redirect(url_for(
            '.preview_broadcast_areas',
            service_id=current_service.id,
            broadcast_message_id=broadcast_message.id,
        ))
    return render_template(
        'views/broadcast/areas.html',
        form=form,
        search_form=SearchByNameForm(),
        show_search_form=(len(form.areas.choices) > 7),
        page_title=f'Choose {library.name.lower()}',
        broadcast_message=broadcast_message,
    )
def approve_broadcast_message(service_id, broadcast_message_id):

    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )

    if broadcast_message.status != 'pending-approval':
        return redirect(url_for(
            '.view_current_broadcast',
            service_id=current_service.id,
            broadcast_message_id=broadcast_message.id,
        ))

    broadcast_message.approve_broadcast()

    if current_service.trial_mode:
        return redirect(url_for(
            '.broadcast_tour',
            service_id=current_service.id,
            step_index=6,
        ))

    return redirect(url_for(
        '.view_current_broadcast',
        service_id=current_service.id,
        broadcast_message_id=broadcast_message.id,
    ))
def preview_broadcast_areas(service_id, broadcast_message_id):
    return render_template(
        'views/broadcast/preview-areas.html',
        broadcast_message=BroadcastMessage.from_id(
            broadcast_message_id,
            service_id=current_service.id,
        ),
    )
def choose_broadcast_library(service_id, broadcast_message_id):
    return render_template(
        'views/broadcast/libraries.html',
        libraries=BroadcastMessage.libraries,
        broadcast_message=BroadcastMessage.from_id(
            broadcast_message_id,
            service_id=current_service.id,
        ),
    )
def view_broadcast_message(service_id, broadcast_message_id):
    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )
    if broadcast_message.status == 'draft':
        abort(404)
    return render_template(
        'views/broadcast/view-message.html',
        broadcast_message=broadcast_message,
    )
def choose_broadcast_sub_area(service_id, broadcast_message_id, library_slug,
                              area_slug):
    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )
    area = BroadcastMessage.libraries.get_areas(area_slug)[0]

    back_link = _get_broadcast_sub_area_back_link(service_id,
                                                  broadcast_message_id,
                                                  library_slug)

    is_county = any(sub_area.sub_areas for sub_area in area.sub_areas)

    form = BroadcastAreaFormWithSelectAll.from_library(
        [] if is_county else area.sub_areas,
        select_all_choice=(area.id, f'All of {area.name}'),
    )
    if form.validate_on_submit():
        broadcast_message.add_areas(*form.selected_areas)
        return redirect(
            url_for(
                '.preview_broadcast_areas',
                service_id=current_service.id,
                broadcast_message_id=broadcast_message.id,
            ))

    if is_county:
        # area = county. sub_areas = districts. they have wards, so link to individual district pages
        return render_template(
            'views/broadcast/counties.html',
            form=form,
            search_form=SearchByNameForm(),
            show_search_form=(len(area.sub_areas) > 7),
            library_slug=library_slug,
            page_title=f'Choose an area of {area.name}',
            broadcast_message=broadcast_message,
            county=area,
            back_link=back_link,
        )

    return render_template(
        'views/broadcast/sub-areas.html',
        form=form,
        search_form=SearchByNameForm(),
        show_search_form=(len(form.areas.choices) > 7),
        library_slug=library_slug,
        page_title=f'Choose an area of {area.name}',
        broadcast_message=broadcast_message,
        back_link=back_link,
    )
def preview_broadcast_message(service_id, broadcast_message_id):
    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )
    if request.method == 'POST':
        broadcast_message.request_approval()
        return redirect(url_for(
            '.view_current_broadcast',
            service_id=current_service.id,
            broadcast_message_id=broadcast_message.id,
        ))

    return render_template(
        'views/broadcast/preview-message.html',
        broadcast_message=broadcast_message,
    )
def preview_broadcast_message(service_id, broadcast_message_id):
    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )
    form = ChooseBroadcastDurationForm()

    if form.validate_on_submit():
        broadcast_message.request_approval(until=form.finishes_at.data)
        return redirect(
            url_for(
                '.broadcast_dashboard',
                service_id=current_service.id,
            ))

    return render_template(
        'views/broadcast/preview-message.html',
        broadcast_message=broadcast_message,
        form=form,
    )
def reject_broadcast_message(service_id, broadcast_message_id):

    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )

    if broadcast_message.status != 'pending-approval':
        return redirect(url_for(
            '.view_current_broadcast',
            service_id=current_service.id,
            broadcast_message_id=broadcast_message.id,
        ))

    broadcast_message.reject_broadcast()

    return redirect(url_for(
        '.broadcast_dashboard',
        service_id=current_service.id,
    ))
def view_broadcast(service_id, broadcast_message_id):
    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )
    if broadcast_message.status == 'draft':
        abort(404)

    if (
        broadcast_message.status in {'completed', 'cancelled', 'rejected'}
        and request.endpoint != 'main.view_previous_broadcast'
    ):
        return redirect(url_for(
            '.view_previous_broadcast',
            service_id=current_service.id,
            broadcast_message_id=broadcast_message.id,
        ))

    if (
        broadcast_message.status in {'broadcasting', 'pending-approval'}
        and request.endpoint != 'main.view_current_broadcast'
    ):
        return redirect(url_for(
            '.view_current_broadcast',
            service_id=current_service.id,
            broadcast_message_id=broadcast_message.id,
        ))

    back_link_endpoint = {
        'main.view_current_broadcast': '.broadcast_dashboard',
        'main.view_previous_broadcast': '.broadcast_dashboard_previous',
    }[request.endpoint]

    return render_template(
        'views/broadcast/view-message.html',
        broadcast_message=broadcast_message,
        back_link=url_for(
            back_link_endpoint,
            service_id=current_service.id,
        ),
    )
def preview_broadcast_areas(service_id, broadcast_message_id):
    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )
    if broadcast_message.template_id:
        back_link = url_for(
            '.view_template',
            service_id=current_service.id,
            template_id=broadcast_message.template_id,
        )
    else:
        back_link = url_for(
            '.write_new_broadcast',
            service_id=current_service.id,
        )

    return render_template(
        'views/broadcast/preview-areas.html',
        broadcast_message=broadcast_message,
        back_link=back_link,
    )
def approve_broadcast_message(service_id, broadcast_message_id):

    broadcast_message = BroadcastMessage.from_id(
        broadcast_message_id,
        service_id=current_service.id,
    )

    if broadcast_message.status != 'pending-approval':
        return redirect(
            url_for(
                '.view_broadcast_message',
                service_id=current_service.id,
                broadcast_message_id=broadcast_message.id,
            ))

    broadcast_message.approve_broadcast()

    return redirect(
        url_for(
            '.view_broadcast_message',
            service_id=current_service.id,
            broadcast_message_id=broadcast_message.id,
        ))