Refactor summaries into model

This lets us encapsulate some of the logic that’s currently cluttering
up the view/template layer.
This commit is contained in:
Chris Hill-Scott
2020-02-12 14:03:51 +00:00
parent 5ba5ee2502
commit 74e70ed8bc
4 changed files with 18 additions and 13 deletions

View File

@@ -296,10 +296,6 @@ def get_dashboard_partials(service_id):
),
'inbox': render_template(
'views/dashboard/_inbox.html',
inbound_sms_summary=(
service_api_client.get_inbound_sms_summary(service_id)
if current_service.has_permission('inbound_sms') else None
),
),
'totals': render_template(
'views/dashboard/_totals.html',

View File

@@ -2,7 +2,7 @@ from collections import OrderedDict
from flask import render_template
from app import service_api_client
from app import current_service, service_api_client
from app.main import main
from app.utils import Spreadsheet, user_has_permissions
@@ -10,10 +10,9 @@ from app.utils import Spreadsheet, user_has_permissions
@main.route("/services/<uuid:service_id>/returned-letters")
@user_has_permissions('view_activity')
def returned_letter_summary(service_id):
summary = service_api_client.get_returned_letter_summary(service_id)
return render_template(
'views/returned-letter-summary.html',
data=summary,
data=current_service.returned_letter_summary,
)

View File

@@ -480,6 +480,12 @@ class Service(JSONModel):
def has_inbound_number(self):
return bool(self.inbound_number)
@cached_property
def inbound_sms_summary(self):
if not self.has_permission('inbound_sms'):
return None
return service_api_client.get_inbound_sms_summary(self.id)
@cached_property
def all_template_folders(self):
return sorted(
@@ -660,3 +666,7 @@ class Service(JSONModel):
):
if test:
yield BASE + '_incomplete' + tag
@cached_property
def returned_letter_summary(self):
return service_api_client.get_returned_letter_summary(self.id)

View File

@@ -2,17 +2,17 @@
{% from "components/message-count-label.html" import message_count_label %}
<div class="ajax-block">
{% if inbound_sms_summary != None %}
<a id="total-received" class="govuk-link govuk-link--no-visited-state banner-dashboard" href="{{ url_for('.inbox', service_id=current_service.id) }}">
{% if current_service.inbound_sms_summary != None %}
<a id="total-received" class="govuk-link govuk-link--no-visited-state banner-dashboard" class="banner-dashboard" href="{{ url_for('.inbox', service_id=current_service.id) }}">
<span class="banner-dashboard-count">
{{ inbound_sms_summary.count|format_thousands }}
{{ current_service.inbound_sms_summary.count|format_thousands }}
</span>
<span class="banner-dashboard-count-label">
{{ message_count_label(inbound_sms_summary.count, 'sms', suffix='received') }}
{{ message_count_label(current_service.inbound_sms_summary.count, 'sms', suffix='received') }}
</span>
{% if inbound_sms_summary.most_recent %}
{% if current_service.inbound_sms_summary.most_recent %}
<span class="banner-dashboard-meta">
latest message {{ inbound_sms_summary.most_recent | format_delta }}
latest message {{ current_service.inbound_sms_summary.most_recent | format_delta }}
</span>
{% endif %}
</a>