From 408fcf05eb6c6ec85db9f243a1aab32e835a6c60 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 28 Feb 2020 12:32:47 +0000 Subject: [PATCH] Add financial year filter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise this page will become less useful come April 1st… --- app/main/views/organisations.py | 16 ++++- app/models/organisation.py | 5 +- .../organisations/organisation/index.html | 58 +++++++++++-------- .../views/organisations/test_organisation.py | 37 ++++++++++++ 4 files changed, 88 insertions(+), 28 deletions(-) diff --git a/app/main/views/organisations.py b/app/main/views/organisations.py index 43959dabd..fd480b9e8 100644 --- a/app/main/views/organisations.py +++ b/app/main/views/organisations.py @@ -1,4 +1,5 @@ from collections import OrderedDict +from functools import partial from flask import flash, redirect, render_template, request, session, url_for from flask_login import current_user @@ -33,6 +34,10 @@ from app.main.forms import ( SetEmailBranding, SetLetterBranding, ) +from app.main.views.dashboard import ( + get_tuples_of_financial_years, + requested_and_current_financial_year, +) from app.main.views.service_settings import get_branding_as_value_and_label from app.models.organisation import Organisation, Organisations from app.models.user import InvitedOrgUser, User @@ -125,10 +130,19 @@ def add_organisation_from_nhs_local_service(service_id): @main.route("/organisations/", methods=['GET']) @user_has_permissions() def organisation_dashboard(org_id): - services = current_organisation.services_and_usage()['services'] + year, current_financial_year = requested_and_current_financial_year(request) + services = current_organisation.services_and_usage( + financial_year=year + )['services'] return render_template( 'views/organisations/organisation/index.html', services=services, + years=get_tuples_of_financial_years( + partial(url_for, '.organisation_dashboard', org_id=current_organisation.id), + start=current_financial_year - 1, + end=current_financial_year + 1, + ), + selected_year=year, **{ f'total_{key}': sum(service[key] for service in services) for key in ('emails_sent', 'sms_cost', 'letter_cost') diff --git a/app/models/organisation.py b/app/models/organisation.py index 07ceccb45..8d1390250 100644 --- a/app/models/organisation.py +++ b/app/models/organisation.py @@ -5,7 +5,6 @@ from app.models import JSONModel, ModelList from app.notify_client.email_branding_client import email_branding_client from app.notify_client.letter_branding_client import letter_branding_client from app.notify_client.organisations_api_client import organisations_client -from app.utils import get_current_financial_year class Organisation(JSONModel): @@ -199,8 +198,8 @@ class Organisation(JSONModel): self.id ) - def services_and_usage(self): - return organisations_client.get_services_and_usage(self.id, get_current_financial_year()) + def services_and_usage(self, financial_year): + return organisations_client.get_services_and_usage(self.id, financial_year) class Organisations(ModelList): diff --git a/app/templates/views/organisations/organisation/index.html b/app/templates/views/organisations/organisation/index.html index 501a80140..039ee9270 100644 --- a/app/templates/views/organisations/organisation/index.html +++ b/app/templates/views/organisations/organisation/index.html @@ -1,4 +1,5 @@ {% from "components/big-number.html" import big_number %} +{% from "components/pill.html" import pill %} {% extends "org_template.html" %} {% block org_page_title %} @@ -11,32 +12,41 @@ Usage -
-
- {{ big_number( - total_emails_sent, - label='emails sent', - smaller=True - ) }} -
-
- {{ big_number( - total_sms_cost, - 'spent on text messages', - currency="£", - smaller=True - ) }} -
-
- {{ big_number( - total_letter_cost, - 'spent on letters', - currency="£", - smaller=True - ) }} -
+
+ {{ pill(years, selected_year, big_number_args={'smallest': True}) }}
+
+
+
+ {{ big_number( + total_emails_sent, + label='emails sent', + smaller=True + ) }} +
+
+
+
+ {{ big_number( + total_sms_cost, + 'spent on text messages', + currency="£", + smaller=True + ) }} +
+
+
+
+ {{ big_number( + total_letter_cost, + 'spent on letters', + currency="£", + smaller=True + ) }} +
+
+
    {% for service in services %}
    diff --git a/tests/app/main/views/organisations/test_organisation.py b/tests/app/main/views/organisations/test_organisation.py index 0e2234ab2..3c7c6e2cd 100644 --- a/tests/app/main/views/organisations/test_organisation.py +++ b/tests/app/main/views/organisations/test_organisation.py @@ -3,6 +3,7 @@ from unittest.mock import ANY, Mock import pytest from bs4 import BeautifulSoup from flask import url_for +from freezegun import freeze_time from notifications_python_client.errors import HTTPError from tests import organisation_json, service_json @@ -389,6 +390,7 @@ def test_nhs_local_assigns_to_selected_organisation( mock_update_service_organisation.assert_called_once_with(SERVICE_ONE_ID, ORGANISATION_ID) +@freeze_time("2020-02-20 20:20") def test_organisation_services_shows_live_services_and_usage( client_request, mock_get_organisation, @@ -434,6 +436,41 @@ def test_organisation_services_shows_live_services_and_usage( assert normalize_spaces(usage_rows[8].text) == "£0.00 spent on letters" +@freeze_time("2020-02-20 20:20") +@pytest.mark.parametrize('financial_year, expected_selected', ( + (2018, '2018 to 2019 financial year'), + (2019, '2019 to 2020 financial year'), + (2020, '2020 to 2021 financial year'), +)) +def test_organisation_services_filters_by_financial_year( + client_request, + mock_get_organisation, + mocker, + active_user_with_permissions, + fake_uuid, + financial_year, + expected_selected, +): + mock = mocker.patch( + 'app.organisations_client.get_services_and_usage', + return_value={"services": []} + ) + page = client_request.get( + '.organisation_dashboard', + org_id=ORGANISATION_ID, + year=financial_year, + ) + mock.assert_called_once_with(ORGANISATION_ID, financial_year) + assert normalize_spaces(page.select_one('.pill').text) == ( + '2018 to 2019 financial year ' + '2019 to 2020 financial year ' + '2020 to 2021 financial year' + ) + assert normalize_spaces(page.select_one('.pill-selected-item').text) == ( + expected_selected + ) + + def test_organisation_trial_mode_services_shows_all_non_live_services( client_request, platform_admin_user,