示例#1
0
def _check_notification(service_id, template_id, exception=None):
    db_template = current_service.get_template_with_user_permission_or_403(
        template_id, current_user)
    email_reply_to = None
    sms_sender = None
    if db_template['template_type'] == 'email':
        email_reply_to = get_email_reply_to_address_from_session()
    elif db_template['template_type'] == 'sms':
        sms_sender = get_sms_sender_from_session()
    template = get_template(
        db_template,
        current_service,
        show_recipient=True,
        email_reply_to=email_reply_to,
        sms_sender=sms_sender,
        letter_preview_url=url_for(
            'no_cookie.check_notification_preview',
            service_id=service_id,
            template_id=template_id,
            filetype='png',
        ),
        page_count=get_page_count_for_letter(db_template),
    )

    placeholders = fields_to_fill_in(template)

    back_link = get_back_link(service_id, template, len(placeholders),
                              placeholders)

    if ((not session.get('recipient')
         and db_template['template_type'] != 'letter')
            or not all_placeholders_in_session(template.placeholders)):
        raise PermanentRedirect(back_link)

    template.values = get_recipient_and_placeholders_from_session(
        template.template_type)
    page_count = get_page_count_for_letter(db_template, template.values)
    template.page_count = page_count
    return dict(
        template=template,
        back_link=back_link,
        help=get_help_argument(),
        letter_too_long=is_letter_too_long(page_count),
        letter_max_pages=LETTER_MAX_PAGE_COUNT,
        page_count=page_count,
        **(get_template_error_dict(exception) if exception else {}),
    )
示例#2
0
def _check_notification(service_id, template_id, exception=None):
    db_template = current_service.get_template_with_user_permission_or_403(
        template_id, current_user)
    email_reply_to = None
    sms_sender = None
    if db_template["template_type"] == "email":
        email_reply_to = get_email_reply_to_address_from_session()
    elif db_template["template_type"] == "sms":
        sms_sender = get_sms_sender_from_session()
    template = get_template(
        db_template,
        current_service,
        show_recipient=True,
        email_reply_to=email_reply_to,
        sms_sender=sms_sender,
        letter_preview_url=url_for(
            ".check_notification_preview",
            service_id=service_id,
            template_id=template_id,
            filetype="png",
        ),
        page_count=get_page_count_for_letter(db_template),
    )

    step_index = len(
        fields_to_fill_in(
            template,
            prefill_current_user=(
                session.get("send_step") == "main.send_test_step"),
        ))
    back_link = get_back_link(service_id, template, step_index)

    if (not session.get("recipient")
            and db_template["template_type"] != "letter"
        ) or not all_placeholders_in_session(template.placeholders):
        raise PermanentRedirect(back_link)

    template.values = get_recipient_and_placeholders_from_session(
        template.template_type)
    return dict(
        template=template,
        back_link=back_link,
        help=get_help_argument(),
        **(get_template_error_dict(exception) if exception else {}),
    )
示例#3
0
def _check_messages(service_id,
                    template_id,
                    upload_id,
                    preview_row,
                    letters_as_pdf=False):

    try:
        # The happy path is that the job doesn’t already exist, so the
        # API will return a 404 and the client will raise HTTPError.
        job_api_client.get_job(service_id, upload_id)

        # the job exists already - so go back to the templates page
        # If we just return a `redirect` (302) object here, we'll get
        # errors when we try and unpack in the check_messages route.
        # Rasing a werkzeug.routing redirect means that doesn't happen.
        raise PermanentRedirect(
            url_for('.send_messages',
                    service_id=service_id,
                    template_id=template_id))
    except HTTPError as e:
        if e.status_code != 404:
            raise

    statistics = service_api_client.get_service_statistics(service_id,
                                                           today_only=True)
    remaining_messages = (current_service.message_limit -
                          sum(stat['requested']
                              for stat in statistics.values()))

    contents = s3download(service_id, upload_id)

    db_template = current_service.get_template_with_user_permission_or_403(
        template_id, current_user)

    email_reply_to = None
    sms_sender = None
    if db_template['template_type'] == 'email':
        email_reply_to = get_email_reply_to_address_from_session()
    elif db_template['template_type'] == 'sms':
        sms_sender = get_sms_sender_from_session()
    template = get_template(
        db_template,
        current_service,
        show_recipient=True,
        letter_preview_url=url_for(
            '.check_messages_preview',
            service_id=service_id,
            template_id=template_id,
            upload_id=upload_id,
            filetype='png',
            row_index=preview_row,
        ) if not letters_as_pdf else None,
        email_reply_to=email_reply_to,
        sms_sender=sms_sender,
        page_count=get_page_count_for_letter(db_template),
    )
    recipients = RecipientCSV(
        contents,
        template_type=template.template_type,
        placeholders=template.placeholders,
        max_initial_rows_shown=50,
        max_errors_shown=50,
        whitelist=itertools.chain.from_iterable(
            [user.name, user.mobile_number, user.email_address]
            for user in Users(service_id))
        if current_service.trial_mode else None,
        remaining_messages=remaining_messages,
        international_sms=current_service.has_permission('international_sms'),
    )

    if request.args.get('from_test'):
        # only happens if generating a letter preview test
        back_link = url_for('.send_test',
                            service_id=service_id,
                            template_id=template.id)
        choose_time_form = None
    else:
        back_link = url_for('.send_messages',
                            service_id=service_id,
                            template_id=template.id)
        choose_time_form = ChooseTimeForm()

    if preview_row < 2:
        abort(404)

    if preview_row < len(recipients) + 2:
        template.values = recipients[preview_row -
                                     2].recipient_and_personalisation
    elif preview_row > 2:
        abort(404)

    return dict(
        recipients=recipients,
        template=template,
        errors=recipients.has_errors,
        row_errors=get_errors_for_csv(recipients, template.template_type),
        count_of_recipients=len(recipients),
        count_of_displayed_recipients=len(list(recipients.displayed_rows)),
        original_file_name=request.args.get('original_file_name', ''),
        upload_id=upload_id,
        form=CsvUploadForm(),
        remaining_messages=remaining_messages,
        choose_time_form=choose_time_form,
        back_link=back_link,
        help=get_help_argument(),
        trying_to_send_letters_in_trial_mode=all((
            current_service.trial_mode,
            template.template_type == 'letter',
        )),
        required_recipient_columns=OrderedSet(
            recipients.recipient_column_headers) - optional_address_columns,
        preview_row=preview_row,
        sent_previously=job_api_client.has_sent_previously(
            service_id, template.id, db_template['version'],
            request.args.get('original_file_name', '')))
def services_or_dashboard():
    raise PermanentRedirect(url_for(".show_accounts_or_dashboard"))
def choose_service():
    raise PermanentRedirect(url_for(".choose_account"))