Example #1
0
def send_messages(service_id, template_id):
    session['sender_id'] = None
    db_template = service_api_client.get_service_template(service_id, template_id)['data']

    if email_or_sms_not_enabled(db_template['template_type'], current_service['permissions']):
        return redirect(url_for(
            '.action_blocked',
            service_id=service_id,
            notification_type=db_template['template_type'],
            return_to='view_template',
            template_id=template_id
        ))

    template = get_template(
        db_template,
        current_service,
        show_recipient=True,
        expand_emails=True,
        letter_preview_url=url_for(
            '.view_letter_template_preview',
            service_id=service_id,
            template_id=template_id,
            filetype='png',
            page_count=get_page_count_for_letter(db_template),
        ),
    )

    form = CsvUploadForm()
    if form.validate_on_submit():
        try:
            upload_id = s3upload(
                service_id,
                Spreadsheet.from_file(form.file.data, filename=form.file.data.filename).as_dict,
                current_app.config['AWS_REGION']
            )
            session['upload_data'] = {
                "template_id": template_id,
                "original_file_name": form.file.data.filename
            }
            return redirect(url_for('.check_messages',
                                    service_id=service_id,
                                    upload_id=upload_id,
                                    template_type=template.template_type))
        except (UnicodeDecodeError, BadZipFile, XLRDError):
            flash('Couldn’t read {}. Try using a different file format.'.format(
                form.file.data.filename
            ))

    column_headings = first_column_headings[template.template_type] + list(template.placeholders)

    return render_template(
        'views/send.html',
        template=template,
        column_headings=list(ascii_uppercase[:len(column_headings)]),
        example=[column_headings, get_example_csv_rows(template)],
        form=form
    )
Example #2
0
def make_and_upload_csv_file(service_id, template):
    upload_id = s3upload(
        service_id,
        Spreadsheet.from_dict(
            session['placeholders'],
            filename=current_app.config['TEST_MESSAGE_FILENAME']).as_dict,
        current_app.config['AWS_REGION'],
    )
    return redirect(
        url_for('.check_messages',
                upload_id=upload_id,
                service_id=service_id,
                template_id=template.id,
                from_test=True,
                help=2 if get_help_argument() else 0))
Example #3
0
def send_messages(service_id, template_id):
    # if there's lots of data in the session, lets log it for debugging purposes
    # TODO: Remove this once we're confident we have session size under control
    if len(session.get('file_uploads', {}).keys()) > 2:
        current_app.logger.info(
            'session contains large file_uploads - json_len {}, keys: {}'.
            format(len(json.dumps(session['file_uploads'])),
                   session['file_uploads'].keys()))

    session['sender_id'] = None
    db_template = service_api_client.get_service_template(
        service_id, template_id)['data']

    if email_or_sms_not_enabled(db_template['template_type'],
                                current_service['permissions']):
        return redirect(
            url_for('.action_blocked',
                    service_id=service_id,
                    notification_type=db_template['template_type'],
                    return_to='view_template',
                    template_id=template_id))

    template = get_template(
        db_template,
        current_service,
        show_recipient=True,
        expand_emails=True,
        letter_preview_url=url_for(
            '.view_letter_template_preview',
            service_id=service_id,
            template_id=template_id,
            filetype='png',
            page_count=get_page_count_for_letter(db_template),
        ),
    )

    form = CsvUploadForm()
    if form.validate_on_submit():
        try:
            upload_id = s3upload(
                service_id,
                Spreadsheet.from_file(
                    form.file.data, filename=form.file.data.filename).as_dict,
                current_app.config['AWS_REGION'])
            return redirect(
                url_for(
                    '.check_messages',
                    service_id=service_id,
                    upload_id=upload_id,
                    template_id=template.id,
                    original_file_name=form.file.data.filename,
                ))
        except (UnicodeDecodeError, BadZipFile, XLRDError):
            flash(
                'Couldn’t read {}. Try using a different file format.'.format(
                    form.file.data.filename))
        except (XLDateError):
            flash((
                '{} contains numbers or dates that Notify can’t understand. '
                'Try formatting all columns as ‘text’ or export your file as CSV.'
            ).format(form.file.data.filename))

    column_headings = get_spreadsheet_column_headings_from_template(template)

    return render_template(
        'views/send.html',
        template=template,
        column_headings=list(ascii_uppercase[:len(column_headings)]),
        example=[column_headings,
                 get_example_csv_rows(template)],
        form=form)