diff --git a/app/main/views/activity.py b/app/main/views/activity.py index c4cd2d583..9dd60b35b 100644 --- a/app/main/views/activity.py +++ b/app/main/views/activity.py @@ -13,6 +13,32 @@ from app.utils.pagination import ( from app.utils.user import user_has_permissions +def get_download_availability(service_id): + """ + Check if there are jobs available for each download time period. + """ + jobs_1_day = job_api_client.get_page_of_jobs(service_id, page=1, limit_days=1) + jobs_3_days = job_api_client.get_page_of_jobs(service_id, page=1, limit_days=3) + jobs_5_days = job_api_client.get_page_of_jobs(service_id, page=1, limit_days=5) + jobs_7_days = job_api_client.get_immediate_jobs(service_id) + + has_1_day_data = len(generate_job_dict(jobs_1_day)) > 0 + has_3_day_data = len(generate_job_dict(jobs_3_days)) > 0 + has_5_day_data = len(generate_job_dict(jobs_5_days)) > 0 + has_7_day_data = len(jobs_7_days) > 0 + + return { + "has_1_day_data": has_1_day_data, + "has_3_day_data": has_3_day_data, + "has_5_day_data": has_5_day_data, + "has_7_day_data": has_7_day_data, + "has_any_download_data": has_1_day_data + or has_3_day_data + or has_5_day_data + or has_7_day_data, + } + + @main.route("/activity/services/") @user_has_permissions(ServicePermission.VIEW_ACTIVITY) def all_jobs_activity(service_id): @@ -22,6 +48,7 @@ def all_jobs_activity(service_id): all_jobs_dict = generate_job_dict(jobs) prev_page, next_page, pagination = handle_pagination(jobs, service_id, page) message_type = ("sms",) + download_availability = get_download_availability(service_id) return render_template( "views/activity/all-activity.html", all_jobs_dict=all_jobs_dict, @@ -29,6 +56,7 @@ def all_jobs_activity(service_id): next_page=next_page, prev_page=prev_page, pagination=pagination, + **download_availability, download_link_one_day=url_for( ".download_notifications_csv", service_id=current_service.id, @@ -68,9 +96,11 @@ def handle_pagination(jobs, service_id, page): if page > 1 else None ) + total_items = jobs.get("total", 0) + has_next_link = jobs.get("links", {}).get("next") is not None next_page = ( generate_next_dict("main.all_jobs_activity", service_id, page) - if jobs.get("links", {}).get("next") + if has_next_link and total_items > 50 else None ) pagination = generate_pagination_pages( diff --git a/app/main/views/jobs.py b/app/main/views/jobs.py index 596743af7..938a0849d 100644 --- a/app/main/views/jobs.py +++ b/app/main/views/jobs.py @@ -253,7 +253,12 @@ def get_notifications(service_id, message_type, status_override=None): # noqa ) next_page = None - if "links" in notifications and notifications["links"].get("next", None): + total_items = notifications.get("total", 0) + if ( + "links" in notifications + and notifications["links"].get("next", None) + and total_items > 50 + ): next_page = generate_next_dict( "main.view_notifications", service_id, page, url_args ) diff --git a/app/templates/views/activity/all-activity.html b/app/templates/views/activity/all-activity.html index d722414b3..94209a38c 100644 --- a/app/templates/views/activity/all-activity.html +++ b/app/templates/views/activity/all-activity.html @@ -126,20 +126,33 @@ {{show_pagination}} {% if current_user.has_permissions(ServicePermission.VIEW_ACTIVITY) %} -

Download recent reports

-

- Download all data last 24 hours (CSV) -

-

- Download all data last 3 days (CSV) -   -

-

- Download all data last 5 days (CSV) -

-

- Download all data last 7 days (CSV) -

+ {% if has_any_download_data %} +

Download recent reports

+ {% if has_1_day_data %} +

+ Download all data last 24 hours (CSV) +

+ {% endif %} + {% if has_3_day_data %} +

+ Download all data last 3 days (CSV) +   +

+ {% endif %} + {% if has_5_day_data %} +

+ Download all data last 5 days (CSV) +

+ {% endif %} + {% if has_7_day_data %} +

+ Download all data last 7 days (CSV) +

+ {% endif %} + {% else %} +

Download recent reports

+

No recent activity to download. Download links will appear when jobs are available.

+ {% endif %} {% endif %} {% endblock %} diff --git a/tests/app/main/views/test_activity.py b/tests/app/main/views/test_activity.py index 753931ce5..09e2ab4fd 100644 --- a/tests/app/main/views/test_activity.py +++ b/tests/app/main/views/test_activity.py @@ -9,6 +9,7 @@ from freezegun import freeze_time from app.main.views.jobs import get_status_filters, get_time_left from app.models.service import Service +from tests import notification_json from tests.conftest import ( SERVICE_ONE_ID, create_active_caseworking_user, @@ -465,12 +466,17 @@ def test_should_show_notifications_for_a_service_with_next_previous( client_request, service_one, active_user_with_permissions, - mock_get_notifications_with_previous_next, mock_get_service_statistics, mock_get_service_data_retention, mock_get_no_api_keys, mocker, ): + mocker.patch( + "app.notification_api_client.get_notifications_for_service", + return_value=notification_json( + service_one["id"], rows=50, with_links=True + ) | {"total": 100}, + ) page = client_request.get( "main.view_notifications", service_id=service_one["id"], @@ -504,16 +510,47 @@ def test_should_show_notifications_for_a_service_with_next_previous( assert "page 1" in prev_page_link.text.strip() -def test_doesnt_show_pagination_with_search_term( +def test_doesnt_show_pagination_when_50_or_fewer_items( client_request, service_one, active_user_with_permissions, - mock_get_notifications_with_previous_next, mock_get_service_statistics, mock_get_service_data_retention, mock_get_no_api_keys, mocker, ): + mocker.patch( + "app.notification_api_client.get_notifications_for_service", + return_value=notification_json( + service_one["id"], rows=50, with_links=False + ), + ) + page = client_request.get( + "main.view_notifications", + service_id=service_one["id"], + message_type="sms", + ) + + assert not page.find("a", {"rel": "next"}) + assert not page.find("a", {"rel": "previous"}) + assert not page.select_one(".table-show-more-link") + + +def test_doesnt_show_pagination_with_search_term( + client_request, + service_one, + active_user_with_permissions, + mock_get_service_statistics, + mock_get_service_data_retention, + mock_get_no_api_keys, + mocker, +): + mocker.patch( + "app.notification_api_client.get_notifications_for_service", + return_value=notification_json( + service_one["id"], rows=50, with_links=True + ) | {"total": 100}, + ) page = client_request.post( "main.view_notifications", service_id=service_one["id"],