From 993ed0774d3b362e6b76cbadb9acd346cb07c4ad Mon Sep 17 00:00:00 2001 From: Michael Barton Date: Mon, 13 Jan 2025 11:11:04 +0000 Subject: [PATCH 1/3] Add 23-24 audit year We intend to test the new platform by uploading the data from the last audit year --- project/npda/general_functions/audit_period.py | 3 ++- project/npda/general_functions/session.py | 9 ++++++++- .../test_get_audit_period_for_date.py | 12 ++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/project/npda/general_functions/audit_period.py b/project/npda/general_functions/audit_period.py index 87fe94d5..4bc9489b 100644 --- a/project/npda/general_functions/audit_period.py +++ b/project/npda/general_functions/audit_period.py @@ -2,6 +2,7 @@ from dateutil.relativedelta import relativedelta +# TODO MRB: How will we add new audit years (https://github.com/rcpch/national-paediatric-diabetes-audit/issues/481) def get_audit_period_for_date(input_date: date) -> tuple[date, date]: """Get the start and end date of the audit period for the given date. @@ -18,7 +19,7 @@ def get_audit_period_for_date(input_date: date) -> tuple[date, date]: a ValueError as undefined. """ - if input_date < date(2024, 4, 1) or input_date > date(2027, 3, 31): + if input_date < date(2023, 4, 1) or input_date > date(2027, 3, 31): raise ValueError( f"Audit period is only available for the years 2024 to 2027. Provided date: {input_date}" ) diff --git a/project/npda/general_functions/session.py b/project/npda/general_functions/session.py index 1b89785d..bb66a840 100644 --- a/project/npda/general_functions/session.py +++ b/project/npda/general_functions/session.py @@ -31,7 +31,14 @@ def create_session_object(user): ) ) - audit_years = [year for year in range(date.today().year - 5, date.today().year + 1)] + # TODO MRB: How will we add new audit years (https://github.com/rcpch/national-paediatric-diabetes-audit/issues/481) + audit_years = [ + # submitted on the old platform, re-uploaded to test this one + 2023, + 2024, + # first year submitted on this platform + 2025 + ] can_upload_csv = True can_complete_questionnaire = True diff --git a/project/npda/tests/general_functions_tests/test_get_audit_period_for_date.py b/project/npda/tests/general_functions_tests/test_get_audit_period_for_date.py index 8cb41a8f..b0babb96 100644 --- a/project/npda/tests/general_functions_tests/test_get_audit_period_for_date.py +++ b/project/npda/tests/general_functions_tests/test_get_audit_period_for_date.py @@ -14,6 +14,14 @@ @pytest.mark.parametrize( "input_date,expected_date_bounds", [ + ( + date(2023, 4, 1), + (date(2023, 4, 1), date(2024, 3, 31)), + ), # Start of the 2023 audit year + ( + date(2024, 3, 31), + (date(2023, 4, 1), date(2024, 3, 31)), + ), # End of the 2023 audit year ( date(2024, 4, 1), (date(2024, 4, 1), date(2025, 3, 31)), @@ -50,12 +58,12 @@ def test_ensure_error_raised_for_date_outside_audit_period(): """ Test that a ValueError is raised when a date outside the audit period is passed to the function: - - date(2024, 3, 31) is before + - date(2023, 3, 31) is before - date(2027, 4, 1) is after """ with pytest.raises(ValueError): - get_audit_period_for_date(date(2024, 3, 31)) + get_audit_period_for_date(date(2023, 3, 31)) with pytest.raises(ValueError): get_audit_period_for_date(date(2028, 4, 1)) From 6ad42ab0dad80cb2c46cd3e242d13ef3679e4260 Mon Sep 17 00:00:00 2001 From: Michael Barton Date: Mon, 13 Jan 2025 11:26:18 +0000 Subject: [PATCH 2/3] Set correct audit year on login It's not the current year as the audit year goes April -> March --- .../npda/general_functions/audit_period.py | 16 +++++++++- project/npda/general_functions/session.py | 31 +++++++++---------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/project/npda/general_functions/audit_period.py b/project/npda/general_functions/audit_period.py index 4bc9489b..9c92c10d 100644 --- a/project/npda/general_functions/audit_period.py +++ b/project/npda/general_functions/audit_period.py @@ -3,6 +3,15 @@ # TODO MRB: How will we add new audit years (https://github.com/rcpch/national-paediatric-diabetes-audit/issues/481) +SUPPORTED_AUDIT_YEARS = [ + # submitted on the old platform, re-uploaded to test this one + 2023, + 2024, + # first year submitted on this platform + 2025, + 2026 +] + def get_audit_period_for_date(input_date: date) -> tuple[date, date]: """Get the start and end date of the audit period for the given date. @@ -19,7 +28,7 @@ def get_audit_period_for_date(input_date: date) -> tuple[date, date]: a ValueError as undefined. """ - if input_date < date(2023, 4, 1) or input_date > date(2027, 3, 31): + if input_date < date(SUPPORTED_AUDIT_YEARS[0], 4, 1) or input_date > date(SUPPORTED_AUDIT_YEARS[-1], 3, 31): raise ValueError( f"Audit period is only available for the years 2024 to 2027. Provided date: {input_date}" ) @@ -38,6 +47,11 @@ def get_audit_period_for_date(input_date: date) -> tuple[date, date]: return audit_start_date, audit_end_date +def get_current_audit_year() -> int: + (start_date, _) = get_audit_period_for_date(date.today()) + return start_date.year + + def get_quarters_for_audit_period( audit_start_date: date, audit_end_date: date ) -> list[tuple[date, date]]: diff --git a/project/npda/general_functions/session.py b/project/npda/general_functions/session.py index bb66a840..d9bba47d 100644 --- a/project/npda/general_functions/session.py +++ b/project/npda/general_functions/session.py @@ -1,5 +1,4 @@ from asgiref.sync import sync_to_async -from datetime import datetime, date import logging from django.core.exceptions import PermissionDenied @@ -8,6 +7,9 @@ # NPDA Imports from project.npda.general_functions import ( organisations_adapter, + get_audit_period_for_date, + get_current_audit_year, + SUPPORTED_AUDIT_YEARS ) logger = logging.getLogger(__name__) @@ -31,28 +33,21 @@ def create_session_object(user): ) ) - # TODO MRB: How will we add new audit years (https://github.com/rcpch/national-paediatric-diabetes-audit/issues/481) - audit_years = [ - # submitted on the old platform, re-uploaded to test this one - 2023, - 2024, - # first year submitted on this platform - 2025 - ] - can_upload_csv = True can_complete_questionnaire = True + audit_year = get_current_audit_year() + if Submission.objects.filter( paediatric_diabetes_unit=primary_organisation.paediatric_diabetes_unit, submission_active=True, - audit_year=datetime.now().year, + audit_year=audit_year, ).exists(): # a submission exists for this PDU for this year submission = Submission.objects.filter( paediatric_diabetes_unit=primary_organisation.paediatric_diabetes_unit, submission_active=True, - audit_year=datetime.now().year, + audit_year=audit_year, ).get() if submission.csv_file and submission.csv_file.name: can_upload_csv = True @@ -69,8 +64,8 @@ def create_session_object(user): "pdu_choices": list(pdu_choices), "can_upload_csv": can_upload_csv, "can_complete_questionnaire": can_complete_questionnaire, - "selected_audit_year": datetime.now().year, - "audit_years": audit_years, + "selected_audit_year": audit_year, + "audit_years": SUPPORTED_AUDIT_YEARS, } return session @@ -89,6 +84,8 @@ def get_new_session_fields(user, pz_code): can_upload_csv = True can_complete_questionnaire = True + audit_year = get_current_audit_year() + if pz_code: can_see_organisations = ( user.is_rcpch_audit_team_member @@ -104,13 +101,13 @@ def get_new_session_fields(user, pz_code): if Submission.objects.filter( paediatric_diabetes_unit__pz_code=pz_code, submission_active=True, - audit_year=datetime.now().year, + audit_year=audit_year, ).exists(): # a submission exists for this PDU for this year submission = Submission.objects.filter( paediatric_diabetes_unit__pz_code=pz_code, submission_active=True, - audit_year=datetime.now().year, + audit_year=audit_year, ).get() if submission.csv_file and submission.csv_file.name: @@ -144,7 +141,7 @@ def refresh_audit_years_in_session(request, selected_audit_year): """ Refresh the audit years in the session object. """ - audit_years = [year for year in range(date.today().year - 5, date.today().year + 1)] + audit_years = SUPPORTED_AUDIT_YEARS request.session["audit_years"] = audit_years request.session["selected_audit_year"] = selected_audit_year request.session.modified = True From b0bf4a199982e8c55d298e1a738bf76052c40dbd Mon Sep 17 00:00:00 2001 From: Michael Barton Date: Mon, 13 Jan 2025 11:35:53 +0000 Subject: [PATCH 3/3] Whoops forgot to clear out 26/27 --- project/npda/general_functions/audit_period.py | 2 +- .../test_get_audit_period_for_date.py | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/project/npda/general_functions/audit_period.py b/project/npda/general_functions/audit_period.py index 9c92c10d..6d044506 100644 --- a/project/npda/general_functions/audit_period.py +++ b/project/npda/general_functions/audit_period.py @@ -20,9 +20,9 @@ def get_audit_period_for_date(input_date: date) -> tuple[date, date]: Data: Audit Period Audit period start Audit period end + 2023 - 2024 1-Apr-23 31-Mar-24 2024 - 2025 1-Apr-24 31-Mar-25 2025 - 2026 1-Apr-25 31-Mar-26 - 2026 - 2027 1-Apr-26 31-Mar-27 NOTE: dates outside of the audit period will raise a ValueError as undefined. diff --git a/project/npda/tests/general_functions_tests/test_get_audit_period_for_date.py b/project/npda/tests/general_functions_tests/test_get_audit_period_for_date.py index b0babb96..fc37d29d 100644 --- a/project/npda/tests/general_functions_tests/test_get_audit_period_for_date.py +++ b/project/npda/tests/general_functions_tests/test_get_audit_period_for_date.py @@ -38,14 +38,6 @@ date(2026, 3, 31), (date(2025, 4, 1), date(2026, 3, 31)), ), # End of the 2025 audit year - ( - date(2026, 4, 1), - (date(2026, 4, 1), date(2027, 3, 31)), - ), # Start of the 2026 audit year - ( - date(2027, 3, 31), - (date(2026, 4, 1), date(2027, 3, 31)), - ), # End of the 2026 audit year ], ) def test_get_audit_period_for_date_returns_correct_start_and_end_date_bounds(