diff --git a/project/npda/models/site.py b/project/npda/models/site.py index 6ae40b6a..fac3cf17 100644 --- a/project/npda/models/site.py +++ b/project/npda/models/site.py @@ -60,4 +60,4 @@ class Meta: ] def __str__(self) -> str: - return f"{self.patient} at {self.organisation_ods_code}({self.paediatric_diabetes_unit_pz_code})" + return f"Site record for ODS Code: {self.organisation_ods_code} (PZ Code: {self.paediatric_diabetes_unit_pz_code})" diff --git a/project/npda/views/home.py b/project/npda/views/home.py index 29e9c2e4..7e4901f9 100644 --- a/project/npda/views/home.py +++ b/project/npda/views/home.py @@ -15,21 +15,14 @@ def home(request): Only verified users can access this page. """ file_uploaded = False - if request.user.is_verified(): - if request.method == "POST": - form = UploadFileForm(request.POST, request.FILES) - file = request.FILES["csv_upload"] - pz_code = request.session.get("sibling_organisations", {}).get("pz_code", "") - file_uploaded = csv_upload(csv_file=file, organisation_ods_code=request.user.organisation_employer, pdu_pz_code=pz_code) - if file_uploaded["status"]==500: - messages.error(request=request,message=f"{file_uploaded["errors"]}") - return redirect('home') - else: - form = UploadFileForm() - context = {"file_uploaded": file_uploaded, "form": form} - template = "home.html" - return render(request=request, template_name=template, context=context) - + if request.method == "POST": + form = UploadFileForm(request.POST, request.FILES) + file = request.FILES["csv_upload"] + pz_code = request.session.get("sibling_organisations").get("pz_code") + file_uploaded = csv_upload(csv_file=file, organisation_ods_code=request.user.organisation_employer, pdu_pz_code=pz_code) + if file_uploaded["status"]==500: + messages.error(request=request,message=f"{file_uploaded["errors"]}") + return redirect('home') else: form = UploadFileForm() context = {"file_uploaded": file_uploaded, "form": form} diff --git a/project/npda/views/npda_users.py b/project/npda/views/npda_users.py index f265a927..ffa6ae0e 100644 --- a/project/npda/views/npda_users.py +++ b/project/npda/views/npda_users.py @@ -283,7 +283,6 @@ def post(self, *args, **kwargs): # the user without 2FA token if settings.DEBUG: request = self.request - user = authenticate( request, username=request.POST.get("auth-username"), @@ -291,6 +290,16 @@ def post(self, *args, **kwargs): ) if user is not None: login(request, user) + # successful login, get PDU and organisation details from user and store in session + current_user_ods_code = self.request.user.organisation_employer + if "sibling_organisations" not in self.request.session: + sibling_organisations = retrieve_pdu_from_organisation_ods_code( + current_user_ods_code + ) + # store the results in session + self.request.session["sibling_organisations"] = ( + sibling_organisations + ) return redirect("home") # Otherwise, continue with usual workflow @@ -299,9 +308,9 @@ def post(self, *args, **kwargs): # Override successful login redirect to org summary page def done(self, form_list, **kwargs): + # this will not be called if debug=True response = super().done(form_list) response_url = getattr(response, "url") - # successful login, get PDU and organisation details from user and store in session current_user_ods_code = self.request.user.organisation_employer if "sibling_organisations" not in self.request.session: diff --git a/project/npda/views/patient.py b/project/npda/views/patient.py index b792a22a..d4486dd4 100644 --- a/project/npda/views/patient.py +++ b/project/npda/views/patient.py @@ -33,9 +33,8 @@ def get_queryset(self): Scope to patient only in the same organisation as the user """ # filter patients to only those in the same organisation as the user - user_pz_code = self.request.session.get("sibling_organisations", {}).get( - "pz_code", None - ) + user_pz_code = self.request.session.get("sibling_organisations").get("pz_code") + logger.error(f"here is the pz code: {user_pz_code}") return ( Patient.objects.filter(site__paediatric_diabetes_unit_pz_code=user_pz_code) .annotate( @@ -72,9 +71,7 @@ def get_context_data(self, **kwargs): return context -class PatientCreateView( - LoginAndOTPRequiredMixin, SuccessMessageMixin, CreateView -): +class PatientCreateView(LoginAndOTPRequiredMixin, SuccessMessageMixin, CreateView): """ Handle creation of new patient in audit """ @@ -103,9 +100,7 @@ def form_valid(self, form: BaseForm) -> HttpResponse: return super().form_valid(form) -class PatientUpdateView( - LoginAndOTPRequiredMixin, SuccessMessageMixin, UpdateView -): +class PatientUpdateView(LoginAndOTPRequiredMixin, SuccessMessageMixin, UpdateView): """ Handle update of patient in audit """ @@ -133,9 +128,7 @@ def form_valid(self, form: BaseForm) -> HttpResponse: return super().form_valid(form) -class PatientDeleteView( - LoginAndOTPRequiredMixin, SuccessMessageMixin, DeleteView -): +class PatientDeleteView(LoginAndOTPRequiredMixin, SuccessMessageMixin, DeleteView): """ Handle deletion of child from audit """