Merge branch 'master' into styleguide-l

This commit is contained in:
Chris Hill-Scott
2018-01-15 14:12:07 +00:00
committed by GitHub
12 changed files with 235 additions and 84 deletions

View File

@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from orderedset import OrderedSet
from itertools import chain
from flask import (
render_template,
@@ -18,7 +16,6 @@ from notifications_utils.template import (
Template,
WithSubjectTemplate,
)
from werkzeug.datastructures import MultiDict
from app import (
job_api_client,
@@ -35,39 +32,12 @@ from app.utils import (
user_has_permissions,
generate_notifications_csv,
get_time_left,
REQUESTED_STATUSES,
FAILURE_STATUSES,
SENDING_STATUSES,
DELIVERED_STATUSES,
get_letter_timings,
parse_filter_args, set_status_filters
)
from app.statistics_utils import add_rate_to_job
def _parse_filter_args(filter_dict):
if not isinstance(filter_dict, MultiDict):
filter_dict = MultiDict(filter_dict)
return MultiDict(
(
key,
(','.join(filter_dict.getlist(key))).split(',')
)
for key in filter_dict.keys()
if ''.join(filter_dict.getlist(key))
)
def _set_status_filters(filter_args):
status_filters = filter_args.get('status', [])
return list(OrderedSet(chain(
(status_filters or REQUESTED_STATUSES),
DELIVERED_STATUSES if 'delivered' in status_filters else [],
SENDING_STATUSES if 'sending' in status_filters else [],
FAILURE_STATUSES if 'failed' in status_filters else []
)))
@main.route("/services/<service_id>/jobs")
@login_required
@user_has_permissions('view_activity', admin_override=True)
@@ -104,8 +74,8 @@ def view_job(service_id, job_id):
if job['job_status'] == 'cancelled':
abort(404)
filter_args = _parse_filter_args(request.args)
filter_args['status'] = _set_status_filters(filter_args)
filter_args = parse_filter_args(request.args)
filter_args['status'] = set_status_filters(filter_args)
total_notifications = job.get('notification_count', 0)
processed_notifications = job.get('notifications_delivered', 0) + job.get('notifications_failed', 0)
@@ -146,8 +116,8 @@ def view_job_csv(service_id, job_id):
template_id=job['template'],
version=job['template_version']
)['data']
filter_args = _parse_filter_args(request.args)
filter_args['status'] = _set_status_filters(filter_args)
filter_args = parse_filter_args(request.args)
filter_args['status'] = set_status_filters(filter_args)
return Response(
stream_with_context(
@@ -206,6 +176,12 @@ def view_notifications(service_id, message_type):
page=request.args.get('page', 1),
to=request.form.get('to', ''),
search_form=SearchNotificationsForm(to=request.form.get('to', '')),
download_link=url_for(
'.download_notifications_csv',
service_id=current_service['id'],
message_type=message_type,
status=request.args.get('status')
)
)
@@ -226,8 +202,8 @@ def get_notifications(service_id, message_type, status_override=None):
abort(404, "Invalid page argument ({}) reverting to page 1.".format(request.args['page'], None))
if message_type not in ['email', 'sms', 'letter']:
abort(404)
filter_args = _parse_filter_args(request.args)
filter_args['status'] = _set_status_filters(filter_args)
filter_args = parse_filter_args(request.args)
filter_args['status'] = set_status_filters(filter_args)
if request.path.endswith('csv'):
return Response(
generate_notifications_csv(
@@ -250,7 +226,6 @@ def get_notifications(service_id, message_type, status_override=None):
limit_days=current_app.config['ACTIVITY_STATS_LIMIT_DAYS'],
to=request.form.get('to', ''),
)
url_args = {
'message_type': message_type,
'status': request.args.get('status')
@@ -361,8 +336,8 @@ def _get_job_counts(job):
def get_job_partials(job, template):
filter_args = _parse_filter_args(request.args)
filter_args['status'] = _set_status_filters(filter_args)
filter_args = parse_filter_args(request.args)
filter_args['status'] = set_status_filters(filter_args)
notifications = notification_api_client.get_notifications_for_service(
job['service'], job['id'], status=filter_args['status']
)

View File

@@ -1,18 +1,20 @@
# -*- coding: utf-8 -*-
from datetime import datetime
from flask import (
abort,
render_template,
jsonify,
request,
url_for,
)
Response, stream_with_context)
from flask_login import login_required
from app import (
notification_api_client,
job_api_client,
current_service
)
current_service,
format_date_numeric)
from app.main import main
from app.template_previews import TemplatePreview, get_page_count_for_letter
from app.utils import (
@@ -23,7 +25,7 @@ from app.utils import (
get_letter_timings,
FAILURE_STATUSES,
DELIVERED_STATUSES,
)
generate_notifications_csv, parse_filter_args, set_status_filters)
@main.route("/services/<service_id>/notification/<uuid:notification_id>")
@@ -138,3 +140,31 @@ def get_all_personalisation_from_notification(notification):
notification['personalisation']['phone_number'] = notification['to']
return notification['personalisation']
@main.route("/services/<service_id>/download-notifications.csv")
@login_required
@user_has_permissions('view_activity', admin_override=True)
def download_notifications_csv(service_id):
filter_args = parse_filter_args(request.args)
filter_args['status'] = set_status_filters(filter_args)
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=5000,
format_for_csv=True
)
),
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'])
}
)

View File

@@ -30,4 +30,8 @@
{% endcall %}
{% endcall %}
</div>
{{ page_footer(
secondary_link=url_for('.api_integration', service_id=current_service.id),
secondary_link_text='Back to API integration'
) }}
{% endblock %}

View File

@@ -42,8 +42,16 @@
<input type="hidden" name="to" value="{{ search_form.to.data }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
</form>
{% else %}
<form id="search-form" method="post">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
</form>
{% endif %}
<!--<p class="bottom-gutter">-->
<!--<a href="{{ download_link }}" download="download" class="heading-small">Download this report</a>-->
<!--&emsp;-->
<!--Data available for 7 days-->
<!--</p>-->
{{ ajax_block(
partials,
url_for('.get_notifications_as_json', service_id=current_service.id, message_type=message_type, status=status, page=page),

View File

@@ -10,13 +10,16 @@
<h1 class="heading-large">Change your service name</h1>
<div class="form-group">
<p>Users will see your service name:</p>
<ul class="list-bullet">
<li>at the start of every text message, eg Vehicle tax: we received your payment, thank you</li>
<li>as your email sender name</li>
</ul>
{% if current_service.prefix_sms %}
<p>Users will see your service name:</p>
<ul class="list-bullet">
<li>at the start of every text message, eg {{ current_service.name }}: This is an example message</li>
<li>as your email sender name</li>
</ul>
{% else %}
<p>Users will see your service name as your email sender name.</p>
{% endif %}
</div>
<form method="post">

View File

@@ -19,6 +19,12 @@
If you want to turn this feature off,
<a href="{{ url_for('.support') }}">get in touch with the GOV.UK Notify team</a>.
</p>
{% if current_user.has_permissions(['manage_api_keys'], admin_override=True) %}
<p>
You can set up callbacks for received text messages on the
<a href="{{ url_for('.api_callbacks', service_id=current_service.id) }}">API integration page</a>.
</p>
{% endif %}
{% else %}
<p>
Receiving text messages from your users is an

View File

@@ -1,5 +1,7 @@
import re
import csv
from itertools import chain
import pytz
from io import StringIO
from os import path
@@ -28,7 +30,8 @@ from notifications_utils.template import (
LetterImageTemplate,
LetterPreviewTemplate,
)
from orderedset._orderedset import OrderedSet
from werkzeug.datastructures import MultiDict
SENDING_STATUSES = ['created', 'pending', 'sending']
DELIVERED_STATUSES = ['delivered', 'sent']
@@ -125,28 +128,47 @@ def get_errors_for_csv(recipients, template_type):
def generate_notifications_csv(**kwargs):
from app import notification_api_client
if 'page' not in kwargs:
kwargs['page'] = 1
fieldnames = ['Row number', 'Recipient', 'Template', 'Type', 'Job', 'Status', 'Time']
if kwargs['job_id']:
fieldnames = ['Row number', 'Recipient', 'Template', 'Type', 'Job', 'Status', 'Time']
else:
fieldnames = ['Recipient', 'Template', 'Type', 'Job', 'Status', 'Time']
yield ','.join(fieldnames) + '\n'
while kwargs['page']:
# if job_id then response looks different
notifications_resp = notification_api_client.get_notifications_for_service(**kwargs)
notifications = notifications_resp['notifications']
for notification in notifications:
values = [
notification['row_number'],
notification['recipient'],
notification['template_name'],
notification['template_type'],
notification['job_name'],
notification['status'],
notification['created_at']
]
line = ','.join(str(i) for i in values) + '\n'
yield line
if kwargs['job_id']:
for notification in notifications:
values = [
notification['row_number'],
notification['recipient'],
notification['template_name'],
notification['template_type'],
notification['job_name'],
notification['status'],
notification['created_at']
]
line = ','.join(str(i) for i in values) + '\n'
yield line
else:
# Change here
for notification in notifications:
values = [
notification['to'],
notification['template']['name'],
notification['template']['template_type'],
notification.get('job_name', None),
notification['status'],
notification['created_at'],
notification['updated_at']
]
line = ','.join(str(i) for i in values) + '\n'
yield line
if notifications_resp['links'].get('next'):
kwargs['page'] += 1
else:
@@ -382,3 +404,27 @@ def get_cdn_domain():
domain = parsed_uri.netloc[len(subdomain + '.'):]
return "static-logos.{}".format(domain)
def parse_filter_args(filter_dict):
if not isinstance(filter_dict, MultiDict):
filter_dict = MultiDict(filter_dict)
return MultiDict(
(
key,
(','.join(filter_dict.getlist(key))).split(',')
)
for key in filter_dict.keys()
if ''.join(filter_dict.getlist(key))
)
def set_status_filters(filter_args):
status_filters = filter_args.get('status', [])
return list(OrderedSet(chain(
(status_filters or REQUESTED_STATUSES),
DELIVERED_STATUSES if 'delivered' in status_filters else [],
SENDING_STATUSES if 'sending' in status_filters else [],
FAILURE_STATUSES if 'failed' in status_filters else []
)))