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 controls for CSV submission #608

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
56 changes: 16 additions & 40 deletions project/npda/general_functions/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,26 @@ def create_session_object(user):
return session


def get_new_session_fields(user, pz_code):
"""
Get the new session fields for the user, based on the PZ code they have selected.
If they are a member of the RCPCH audit team, they can see all organisations and they can upload CSVs as well as complete the questionnaire.
If they are a member of the organisation, they can see their own organisation and they can either upload CSVs or complete the questionnaire
until they have chosen one option or the other, after which they can only do one.
"""
ret = {}
def refresh_session_filters(request, pz_code=None, audit_year=None):
session = {}

Submission = apps.get_model("npda", "Submission")
PaediatricDiabetesUnit = apps.get_model("npda", "PaediatricDiabetesUnit")

can_upload_csv = True
can_complete_questionnaire = True

audit_year = get_current_audit_year()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the buggy line - it didn't respect selected_audit_year

pz_code = pz_code or request.session.get("pz_code")

audit_years = SUPPORTED_AUDIT_YEARS
audit_year = audit_year or request.session.get("selected_audit_year")

session["audit_years"] = audit_years
session["selected_audit_year"] = audit_year

if pz_code:
user = request.user

can_see_organisations = (
user.is_rcpch_audit_team_member
or user.organisation_employers.filter(pz_code=pz_code).exists()
Expand All @@ -100,44 +103,17 @@ def get_new_session_fields(user, pz_code):
)
raise PermissionDenied()

ret["pz_code"] = pz_code
ret["lead_organisation"] = PaediatricDiabetesUnit.objects.get(
session["pz_code"] = pz_code
session["lead_organisation"] = PaediatricDiabetesUnit.objects.get(
pz_code=pz_code
).lead_organisation_name
ret["pdu_choices"] = list(
session["pdu_choices"] = list(
organisations_adapter.paediatric_diabetes_units_to_populate_select_field(
requesting_user=user, user_instance=None
)
)

ret |= get_submission_actions(pz_code, audit_year)

return ret


def refresh_audit_years_in_session(request, selected_audit_year):
"""
Refresh the audit years in the session object.
"""
audit_years = SUPPORTED_AUDIT_YEARS
request.session["audit_years"] = audit_years
request.session["selected_audit_year"] = selected_audit_year
request.session.modified = True


async def refresh_session_object_asynchronously(request, user, pz_code):
"""
Refresh the session object and save the new fields.
"""
session = await sync_to_async(get_new_session_fields)(user, pz_code)
request.session.update(session)
request.session.modified = True

session |= get_submission_actions(pz_code, audit_year)

def refresh_session_object_synchronously(request, user, pz_code):
"""
Refresh the session object and save the new fields.
"""
session = get_new_session_fields(user, pz_code)
request.session.update(session)
request.session.modified = True
5 changes: 0 additions & 5 deletions project/npda/views/dashboard/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
from project.npda.models.patient import Patient
from .helpers import *


from project.npda.general_functions.session import (
refresh_session_object_synchronously,
)
from project.npda.kpi_class.kpis import CalculateKPIS

from project.npda.views.decorators import login_and_otp_required
Expand Down Expand Up @@ -82,7 +78,6 @@ def dashboard(request):
if request.htmx:
template = "dashboard/dashboard_base.html"
pz_code = request.session.get("pz_code")
refresh_session_object_synchronously(request=request, user=request.user, pz_code=pz_code)

PaediatricDiabetesUnit: PaediatricDiabetesUnitClass = apps.get_model(
"npda", "PaediatricDiabetesUnit"
Expand Down
32 changes: 9 additions & 23 deletions project/npda/views/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@

from project.npda.general_functions.csv import csv_upload, csv_parse, csv_header
from ..forms.upload import UploadFileForm
from ..general_functions.session import (
get_new_session_fields,
refresh_audit_years_in_session,
refresh_session_object_asynchronously,
)
from ..general_functions.session import refresh_session_filters
from ..general_functions.view_preference import get_or_update_view_preference

# RCPCH imports
Expand Down Expand Up @@ -109,9 +105,8 @@ async def home(request):
logger.error(f"Failed to log user activity: {e}")

# update the session fields - this stores that the user has uploaded a csv and disables the ability to use the questionnaire
await refresh_session_object_asynchronously(
request=request, user=request.user, pz_code=pz_code
)
await sync_to_async(refresh_session_filters)(request)

if errors_by_row_index:
messages.error(
request=request,
Expand Down Expand Up @@ -163,20 +158,10 @@ def view_preference(request):
view_preference = get_or_update_view_preference(
request.user, view_preference_selection
)
pz_code = request.POST.get("pz_code_select_name", None)

if pz_code is not None:
new_session_fields = get_new_session_fields(
user=request.user, pz_code=pz_code
) # includes a validation step
else:
new_session = request.session
pz_code = new_session["pz_code"]
new_session_fields = get_new_session_fields(
request.user, pz_code
) # includes a validation step

request.session.update(new_session_fields)
selected_pz_code = request.POST.get("pz_code_select_name", None)

# includes a validation step
refresh_session_filters(request, pz_code=selected_pz_code)

# Reload the page to apply the new view preference
return HttpResponse(status=204, headers={"HX-Refresh": "true"})
Expand All @@ -189,7 +174,8 @@ def audit_year(request):
"""
if request.method == "POST":
audit_year = request.POST.get("audit_year_select_name", None)
refresh_audit_years_in_session(request, audit_year)

refresh_session_filters(request, audit_year=audit_year)

# Reload the page to apply the new view preference
return HttpResponse(status=204, headers={"HX-Refresh": "true"})
Expand Down
9 changes: 4 additions & 5 deletions project/npda/views/npda_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
send_email_to_recipients,
group_for_role,
organisations_adapter,
get_new_session_fields,
get_or_update_view_preference,
refresh_session_filters
)
from .mixins import CheckPDUInstanceMixin, CheckPDUListMixin, LoginAndOTPRequiredMixin
from project.constants import VIEW_PREFERENCES
Expand Down Expand Up @@ -113,14 +113,13 @@ def get(self, request, *args: str, **kwargs) -> HttpResponse:

def post(self, request, *args: str, **kwargs) -> HttpResponse:
"""
Override POST method to requery the database for the list of patients if view preference changes
Override POST method to requery the database for the list of users if view preference changes
"""
if request.htmx:
view_preference = request.POST.get("view_preference", None)
pz_code = request.POST.get("npdauser_pz_code_select_name", None)
selected_pz_code = request.POST.get("npdauser_pz_code_select_name", None)

new_session_fields = get_new_session_fields(self.request.user, pz_code)
self.request.session.update(new_session_fields)
refresh_session_filters(self.request, pz_code=selected_pz_code)

view_preference = get_or_update_view_preference(
self.request.user, view_preference
Expand Down
10 changes: 3 additions & 7 deletions project/npda/views/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
CheckPDUListMixin,
LoginAndOTPRequiredMixin,
)
from ..general_functions.session import refresh_session_object_synchronously
from ..general_functions.session import refresh_session_filters

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -358,12 +358,8 @@ def form_valid(self, form: BaseForm) -> HttpResponse:
)
submission.patients.add(patient)
submission.save()
# update the session
refresh_session_object_synchronously(
request=self.request,
user=self.request.user,
pz_code=self.request.session.get("pz_code"),
)
# update the session - this stores that the user has used the questionnaire and disables csv upload
refresh_session_filters(request)

else:
logger.error(
Expand Down
Loading