Derive markup dashboard from rendered partials

This commit extends the `ajax_block` component to take a `dict` of
partials, from which it can select the partial matching its `key`
argument and print its HTML to the page.

This means that the same markup is only rendered in one place, rather
than in two (individually in the JSON endpoint and as `include`s in the
parent template).
This commit is contained in:
Chris Hill-Scott
2016-06-28 11:23:43 +01:00
parent fa01c1bc5c
commit ccf520f446
5 changed files with 92 additions and 114 deletions

View File

@@ -1,6 +1,7 @@
from datetime import datetime, date, timedelta
from collections import namedtuple
from itertools import groupby
import json
from flask import (
render_template,
@@ -48,9 +49,8 @@ def service_dashboard(service_id):
return render_template(
'views/dashboard/dashboard.html',
templates=service_api_client.get_service_templates(service_id)['data'],
updates_url=url_for(".service_dashboard_updates", service_id=service_id),
**get_dashboard_statistics_for_service(service_id)
partials=get_dashboard_partials(service_id)
)
@@ -58,25 +58,7 @@ def service_dashboard(service_id):
@login_required
@user_has_permissions('view_activity', admin_override=True)
def service_dashboard_updates(service_id):
dashboard_statistics = get_dashboard_statistics_for_service(service_id)
return jsonify(**{
'totals': render_template(
'views/dashboard/_totals.html',
**dashboard_statistics
),
'template-statistics': render_template(
'views/dashboard/template-statistics.html',
**dashboard_statistics
),
'jobs': render_template(
'views/dashboard/_jobs.html',
**dashboard_statistics
),
'usage': render_template(
'views/dashboard/_usage.html',
**dashboard_statistics
),
})
return jsonify(**get_dashboard_partials(service_id))
@main.route("/services/<service_id>/template-activity")
@@ -101,7 +83,7 @@ def template_history(service_id):
def usage(service_id):
return render_template(
'views/usage.html',
**get_dashboard_statistics_for_service(service_id)
**calculate_usage(service_api_client.get_service_usage(service_id)['data'])
)
@@ -157,35 +139,54 @@ def aggregate_usage(template_statistics):
)
def get_dashboard_statistics_for_service(service_id):
usage = service_api_client.get_service_usage(service_id)
sms_free_allowance = 250000
sms_rate = 0.018
sms_sent = usage['data'].get('sms_count', 0)
emails_sent = usage['data'].get('email_count', 0)
def get_dashboard_partials(service_id):
template_statistics = aggregate_usage(
template_statistics_client.get_template_statistics_for_service(service_id, limit_days=7)
)
jobs = add_rate_to_jobs(filter(
lambda job: job['original_file_name'] != current_app.config['TEST_MESSAGE_FILENAME'],
job_api_client.get_job(service_id, limit_days=7)['data']
))
return {
'statistics': add_rates_to(sum_of_statistics(
statistics_api_client.get_statistics_for_service(service_id, limit_days=7)['data']
)),
'template_statistics': template_statistics,
'most_used_template_count': max(
[row['usage_count'] for row in template_statistics] or [0]
'totals': render_template(
'views/dashboard/_totals.html',
statistics=add_rates_to(sum_of_statistics(
statistics_api_client.get_statistics_for_service(service_id, limit_days=7)['data']
))
),
'template-statistics': render_template(
'views/dashboard/template-statistics.html',
template_statistics=template_statistics,
most_used_template_count=max(
[row['usage_count'] for row in template_statistics] or [0]
),
),
'has_template_statistics': bool(template_statistics),
'jobs': render_template(
'views/dashboard/_jobs.html',
jobs=jobs
),
'has_jobs': bool(jobs),
'usage': render_template(
'views/dashboard/_usage.html',
**calculate_usage(service_api_client.get_service_usage(service_id)['data'])
),
}
def calculate_usage(usage, sms_free_allowance=250000, sms_rate=0.018):
sms_sent = usage.get('sms_count', 0)
emails_sent = usage.get('email_count', 0)
return {
'emails_sent': emails_sent,
'sms_free_allowance': sms_free_allowance,
'sms_sent': sms_sent,
'sms_allowance_remaining': max(0, (sms_free_allowance - sms_sent)),
'sms_chargeable': max(0, sms_sent - sms_free_allowance),
'sms_rate': sms_rate,
'jobs': add_rate_to_jobs(filter(
lambda job: job['original_file_name'] != current_app.config['TEST_MESSAGE_FILENAME'],
job_api_client.get_job(service_id, limit_days=7)['data']
))
'sms_rate': sms_rate
}

View File

@@ -70,22 +70,13 @@ def view_jobs(service_id):
@login_required
@user_has_permissions('view_activity', admin_override=True)
def view_job(service_id, job_id):
job = job_api_client.get_job(service_id, job_id)['data']
template = service_api_client.get_service_template(service_id=service_id,
template_id=job['template'],
version=job['template_version'])['data']
job = job_api_client.get_job(service_id, job_id)['data']
filter_args = _parse_filter_args(request.args)
_set_status_filters(filter_args)
notifications = notification_api_client.get_notifications_for_service(
service_id, job_id, status=filter_args.get('status'),
)
finished = job['status'] == 'finished'
return render_template(
'views/jobs/job.html',
notifications=notifications['notifications'],
job=job,
uploaded_at=job['created_at'],
finished=job.get('notifications_sent', 0) and ((
job.get('notifications_sent', 0) -
job.get('notifications_delivered', 0) -
@@ -93,17 +84,21 @@ def view_job(service_id, job_id):
) == 0),
uploaded_file_name=job['original_file_name'],
template=Template(
template,
service_api_client.get_service_template(
service_id=service_id,
template_id=job['template'],
version=job['template_version']
)['data'],
prefix=current_service['name']
),
counts=_get_job_counts(job, request.args.get('help', 0)),
status=request.args.get('status', ''),
updates_url=url_for(
".view_job_updates",
service_id=service_id,
job_id=job['id'],
status=request.args.get('status', '')
)
),
partials=get_job_partials(job)
)
@@ -144,38 +139,9 @@ def view_job_csv(service_id, job_id):
@login_required
@user_has_permissions('view_activity', admin_override=True)
def view_job_updates(service_id, job_id):
job = job_api_client.get_job(service_id, job_id)['data']
filter_args = _parse_filter_args(request.args)
_set_status_filters(filter_args)
notifications = notification_api_client.get_notifications_for_service(
service_id, job_id, status=filter_args.get('status')
)
finished = (
job.get('notifications_sent', 0) -
job.get('notifications_delivered', 0) -
job.get('notifications_failed', 0)
) == 0
return jsonify(**{
'counts': render_template(
'partials/jobs/count.html',
job=job,
finished=finished,
counts=_get_job_counts(job, request.args.get('help', 0)),
status=request.args.get('status', '')
),
'notifications': render_template(
'partials/jobs/notifications.html',
job=job,
notifications=notifications['notifications'],
finished=finished,
status=request.args.get('status', '')
),
'status': render_template(
'partials/jobs/status.html',
job=job,
finished=finished
),
})
return jsonify(**get_job_partials(
job_api_client.get_job(service_id, job_id)['data']
))
@main.route('/services/<service_id>/notifications/<message_type>')
@@ -325,3 +291,30 @@ def _get_job_counts(job, help_argument):
]
]
]
def get_job_partials(job):
filter_args = _parse_filter_args(request.args)
_set_status_filters(filter_args)
return {
'counts': render_template(
'partials/jobs/count.html',
job=job,
counts=_get_job_counts(job, request.args.get('help', 0)),
status=request.args.get('status', '')
),
'notifications': render_template(
'partials/jobs/notifications.html',
job=job,
notifications=notification_api_client.get_notifications_for_service(
job['service'], job['id'], status=filter_args.get('status')
)['notifications'],
status=request.args.get('status', '')
),
'status': render_template(
'partials/jobs/status.html',
job=job
),
}

View File

@@ -1,4 +1,4 @@
{% macro ajax_block(url, key, interval=2, finished=False) %}
{% macro ajax_block(partials, url, key, interval=2, finished=False) %}
{% if not finished %}
<div
data-module="update-content"
@@ -8,7 +8,7 @@
aria-live="polite"
>
{% endif %}
{{ caller() }}
{{ partials[key]|safe }}
{% if not finished %}
</div>
{% endif %}

View File

@@ -30,28 +30,22 @@
In the last 7 days
</h2>
{% call ajax_block(updates_url, 'totals') %}
{% include 'views/dashboard/_totals.html' %}
{% endcall %}
{{ ajax_block(partials, updates_url, 'totals') }}
{{ show_more(
url_for('.weekly', service_id=current_service.id),
'Compare to previous weeks'
) }}
{% if template_statistics|length %}
{% call ajax_block(updates_url, 'template-statistics') %}
{% include 'views/dashboard/template-statistics.html' %}
{% endcall %}
{% if partials['has_template_statistics'] %}
{{ ajax_block(partials, updates_url, 'template-statistics') }}
{{ show_more(
url_for('.template_history', service_id=current_service.id),
'See all templates used this year'
) }}
{% endif %}
{% if jobs %}
{% call ajax_block(updates_url, 'jobs') %}
{% include 'views/dashboard/_jobs.html' %}
{% endcall %}
{% if partials['has_jobs'] %}
{{ ajax_block(partials, updates_url, 'jobs') }}
{{ show_more(
url_for('.view_jobs', service_id=current_service.id),
'See all uploaded files'
@@ -60,9 +54,7 @@
{% if current_user.has_permissions(['manage_settings'], admin_override=True) %}
<h2 class='heading-medium'>This year</h2>
{% call ajax_block(updates_url, 'usage') %}
{% include 'views/dashboard/_usage.html' %}
{% endcall %}
{{ ajax_block(partials, updates_url, 'usage') }}
{{ show_more(
url_for(".usage", service_id=current_service['id']),
'See usage breakdown'

View File

@@ -29,16 +29,8 @@
)}}
{% endif %}
{% call ajax_block(updates_url, 'status', finished=finished) %}
{% include 'partials/jobs/status.html' %}
{% endcall %}
{% call ajax_block(updates_url, 'counts', finished=finished) %}
{% include 'partials/jobs/count.html' %}
{% endcall %}
{% call ajax_block(updates_url, 'notifications', finished=finished) %}
{% include 'partials/jobs/notifications.html' %}
{% endcall %}
{{ ajax_block(partials, updates_url, 'status', finished=finished) }}
{{ ajax_block(partials, updates_url, 'counts', finished=finished) }}
{{ ajax_block(partials, updates_url, 'notifications', finished=finished) }}
{% endblock %}