Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix audit year when logging in Jan to April #482

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions project/npda/general_functions/audit_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
from dateutil.relativedelta import relativedelta


# 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.

Expand All @@ -10,15 +20,15 @@ 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.
"""

if input_date < date(2024, 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}"
)
Expand All @@ -37,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]]:
Expand Down
24 changes: 14 additions & 10 deletions project/npda/general_functions/session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from asgiref.sync import sync_to_async
from datetime import datetime, date
import logging

from django.core.exceptions import PermissionDenied
Expand All @@ -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__)
Expand All @@ -31,21 +33,21 @@ def create_session_object(user):
)
)

audit_years = [year for year in range(date.today().year - 5, date.today().year + 1)]

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
Expand All @@ -62,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
Expand All @@ -82,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
Expand All @@ -97,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:
Expand Down Expand Up @@ -137,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -30,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(
Expand All @@ -50,12 +50,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))
Expand Down
Loading