Skip to content

Commit

Permalink
fix errors in session allocation with new login workflow if debug=True
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourpeas committed Jun 2, 2024
1 parent 86e518f commit c7e25ae
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion project/npda/models/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
23 changes: 8 additions & 15 deletions project/npda/views/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
13 changes: 11 additions & 2 deletions project/npda/views/npda_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,23 @@ 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"),
password=request.POST.get("auth-password"),
)
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
Expand All @@ -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:
Expand Down
17 changes: 5 additions & 12 deletions project/npda/views/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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
"""
Expand Down

0 comments on commit c7e25ae

Please sign in to comment.