Пример #1
0
def trial_services_csv():
    results = service_api_client.get_trial_services_data()["data"]
    trial_services_columns = [
        "Service ID", "Created date", "Organisation", "Organisation type",
        "Domains", "Service name", "SMS sent this year",
        "Emails sent this year", "Letters sent this year"
    ]
    trial_services_data = []
    trial_services_data.append(trial_services_columns)
    for row in results:
        trial_services_data.append([
            row["service_id"],
            datetime.strptime(row["created_date"],
                              '%a, %d %b %Y %X %Z').strftime("%d-%m-%Y"),
            row["organisation_name"],
            row.get("organisation_type", "TODO"),
            ', '.join(row["domains"]),
            row["service_name"],
            row["sms_totals"],
            row["email_totals"],
            row["letter_totals"],
        ])

    return Spreadsheet.from_rows(trial_services_data).as_csv_data, 200, {
        'Content-Type':
        'text/csv; charset=utf-8',
        'Content-Disposition':
        'inline; filename="{} trial services report.csv"'.format(
            format_date_numeric(
                datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")), )
    }
Пример #2
0
def download_notifications_csv(service_id):
    filter_args = parse_filter_args(request.args)
    filter_args['status'] = set_status_filters(filter_args)

    service_data_retention_days = current_service.get_days_of_retention(
        filter_args.get('message_type')[0])
    return Response(
        stream_with_context(
            generate_notifications_csv(
                service_id=service_id,
                job_id=None,
                status=filter_args.get('status'),
                page=request.args.get('page', 1),
                page_size=10000,
                format_for_csv=True,
                template_type=filter_args.get('message_type'),
                limit_days=service_data_retention_days,
            )),
        mimetype='text/csv',
        headers={
            'Content-Disposition':
            'inline; filename="{} - {} - {} report.csv"'.format(
                format_date_numeric(
                    datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")),
                filter_args['message_type'][0], current_service.name)
        })
Пример #3
0
def _chunk_events_by_day(events):

    days = defaultdict(list)

    for event in sorted(events, key=attrgetter('time'), reverse=True):
        days[format_date_numeric(event.time)].append(event)

    return sorted(days.items(), reverse=True)
Пример #4
0
def _chunk_versions_by_day(versions):

    days = defaultdict(list)

    for version in sorted(versions, key=lambda version: version['updated_at'] or '', reverse=True):
        days[
            format_date_numeric(version['updated_at'])
        ].append(version)

    return sorted(days.items(), reverse=True)
Пример #5
0
def inbox_download(service_id):
    return Response(
        Spreadsheet.from_rows([[
            'Phone number',
            'Message',
            'Received',
        ]] + [[
            message['user_number'],
            message['content'].lstrip(('=+-@')),
            format_datetime_numeric(message['created_at']),
        ] for message in service_api_client.get_inbound_sms(service_id)]
                              ).as_csv_data,
        mimetype='text/csv',
        headers={
            'Content-Disposition':
            'inline; filename="Received text messages {}.csv"'.format(
                format_date_numeric(datetime.utcnow().isoformat()))
        })
Пример #6
0
def live_services_csv():
    results = service_api_client.get_live_services_data()["data"]
    live_services_columns = [
        "Service ID", "Organisation", "Organisation type", "Domains",
        "Service name", "Consent to research", "Main contact", "Contact email",
        "Contact mobile", "Live date", "Created date", "SMS volume intent",
        "Email volume intent", "Letter volume intent", "SMS sent this year",
        "Emails sent this year", "Letters sent this year"
    ]
    live_services_data = []
    live_services_data.append(live_services_columns)
    for row in results:
        live_services_data.append([
            row["service_id"],
            row["organisation_name"],
            row.get("organisation_type", "TODO"),
            ', '.join(row["domains"]),
            row["service_name"],
            row.get("consent_to_research", "TODO"),
            row["contact_name"],
            row["contact_email"],
            row["contact_mobile"],
            datetime.strptime(row["live_date"],
                              '%a, %d %b %Y %X %Z').strftime("%d-%m-%Y")
            if row["live_date"] else None,
            datetime.strptime(row["created_date"],
                              '%a, %d %b %Y %X %Z').strftime("%d-%m-%Y"),
            row.get("sms_volume_intent", "TODO"),
            row.get("email_volume_intent", "TODO"),
            row.get("letter_volume_intent", "TODO"),
            row["sms_totals"],
            row["email_totals"],
            row["letter_totals"],
        ])

    return Spreadsheet.from_rows(live_services_data).as_csv_data, 200, {
        'Content-Type':
        'text/csv; charset=utf-8',
        'Content-Disposition':
        'inline; filename="{} live services report.csv"'.format(
            format_date_numeric(
                datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")), )
    }
Пример #7
0
def live_services_csv():
    results = service_api_client.get_live_services_data()["data"]

    column_names = OrderedDict(
        [
            ("service_id", "Service ID"),
            ("organisation_name", "Organisation"),
            ("organisation_type", "Organisation type"),
            ("service_name", "Service name"),
            ("consent_to_research", "Consent to research"),
            ("contact_name", "Main contact"),
            ("contact_email", "Contact email"),
            ("contact_mobile", "Contact mobile"),
            ("live_date", "Live date"),
            ("sms_volume_intent", "SMS volume intent"),
            ("email_volume_intent", "Email volume intent"),
            ("letter_volume_intent", "Letter volume intent"),
            ("sms_totals", "SMS sent this year"),
            ("email_totals", "Emails sent this year"),
            ("letter_totals", "Letters sent this year"),
            ("free_sms_fragment_limit", "Free sms allowance"),
        ]
    )

    # initialise with header row
    live_services_data = [[x for x in column_names.values()]]

    for row in results:
        if row["live_date"]:
            row["live_date"] = datetime.strptime(row["live_date"], "%a, %d %b %Y %X %Z").strftime("%d-%m-%Y")

        live_services_data.append([row[api_key] for api_key in column_names.keys()])

    return (
        Spreadsheet.from_rows(live_services_data).as_csv_data,
        200,
        {
            "Content-Type": "text/csv; charset=utf-8",
            "Content-Disposition": 'inline; filename="{} live services report.csv"'.format(
                format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")),
            ),
        },
    )
Пример #8
0
def live_services_csv():
    results = service_api_client.get_live_services_data()["data"]

    column_names = OrderedDict([
        ('service_id', 'Service ID'),
        ('organisation_name', 'Organisation'),
        ('organisation_type', 'Organisation type'),
        ('service_name', 'Service name'),
        ('consent_to_research', 'Consent to research'),
        ('contact_name', 'Main contact'),
        ('contact_email', 'Contact email'),
        ('contact_mobile', 'Contact mobile'),
        ('live_date', 'Live date'),
        ('sms_volume_intent', 'SMS volume intent'),
        ('email_volume_intent', 'Email volume intent'),
        ('letter_volume_intent', 'Letter volume intent'),
        ('sms_totals', 'SMS sent this year'),
        ('email_totals', 'Emails sent this year'),
        ('letter_totals', 'Letters sent this year'),
        ('free_sms_fragment_limit', 'Free sms allowance'),
    ])

    # initialise with header row
    live_services_data = [[x for x in column_names.values()]]

    for row in results:
        if row['live_date']:
            row['live_date'] = datetime.strptime(
                row["live_date"], '%a, %d %b %Y %X %Z').strftime("%d-%m-%Y")

        live_services_data.append(
            [row[api_key] for api_key in column_names.keys()])

    return Spreadsheet.from_rows(live_services_data).as_csv_data, 200, {
        'Content-Type':
        'text/csv; charset=utf-8',
        'Content-Disposition':
        'inline; filename="{} live services report.csv"'.format(
            format_date_numeric(
                datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")), )
    }
Пример #9
0
def trial_report_csv():
    data = platform_stats_api_client.usage_for_trial_services()
    headers = [
        "service_id",
        "service_name",
        "creation_date",
        "created_by_name",
        "created_by_email",
        "notification_type",
        "notification_sum",
    ]

    return (
        Spreadsheet.from_rows([headers] + data).as_csv_data,
        200,
        {
            "Content-Type": "text/csv; charset=utf-8",
            "Content-Disposition": 'inline; filename="{} trial report.csv"'.format(
                format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")),
            ),
        },
    )
Пример #10
0
def performance_platform_xlsx():
    results = service_api_client.get_live_services_data()["data"]
    live_services_columns = ["service_id", "agency", "service_name", "_timestamp", "service", "count"]
    live_services_data = []
    live_services_data.append(live_services_columns)
    for row in results:
        live_services_data.append([
            row["service_id"],
            row["organisation_name"],
            row["service_name"],
            datetime.strptime(
                row["live_date"], '%a, %d %b %Y %X %Z'
            ).strftime("%Y-%m-%dT%H:%M:%S") + "Z" if row["live_date"] else None,
            "notification",
            1
        ])

    return Spreadsheet.from_rows(live_services_data).as_excel_file, 200, {
        'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'Content-Disposition': 'attachment; filename="{} performance platform report.xlsx"'.format(
            format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")),
        )
    }